Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup & inline suppressible plugin issue #46

Merged
merged 2 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"php": "^7.1",
"ext-simplexml": "*",
"symfony/framework-bundle": "^3.0 || ^4.0 || ^5.0",
"vimeo/psalm": "^3.11"
"vimeo/psalm": "^3.11.4"
},
"require-dev": {
"codeception/base": "^2.5",
Expand Down
73 changes: 0 additions & 73 deletions src/Handler/ClassHandler.php

This file was deleted.

38 changes: 38 additions & 0 deletions src/Handler/ContainerDependencyHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Psalm\SymfonyPsalmPlugin\Handler;

use PhpParser\Node;
use Psalm\Codebase;
use Psalm\CodeLocation;
use Psalm\IssueBuffer;
use Psalm\Plugin\Hook\AfterFunctionLikeAnalysisInterface;
use Psalm\StatementsSource;
use Psalm\Storage\FunctionLikeStorage;
use Psalm\SymfonyPsalmPlugin\Issue\ContainerDependency;
use Symfony\Component\DependencyInjection\ContainerInterface;

class ContainerDependencyHandler implements AfterFunctionLikeAnalysisInterface
{
/**
* {@inheritdoc}
*/
public static function afterStatementAnalysis(
Node\FunctionLike $stmt,
FunctionLikeStorage $classlike_storage,
StatementsSource $statements_source,
Codebase $codebase,
array &$file_replacements = []
) {
if ($stmt instanceof Node\Stmt\ClassMethod && '__construct' === $stmt->name->name) {
foreach ($stmt->params as $param) {
if ($param->type instanceof Node\Name && ContainerInterface::class === $param->type->getAttributes()['resolvedName']) {
IssueBuffer::accepts(
new ContainerDependency(new CodeLocation($statements_source, $param)),
$statements_source->getSuppressedIssues()
);
}
}
}
}
}
6 changes: 3 additions & 3 deletions src/Handler/ContainerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static function afterMethodCallAnalysis(
}

/**
* {@inheritDoc}
* {@inheritdoc}
*/
public static function afterClassLikeVisit(
ClassLike $class_node,
Expand All @@ -136,8 +136,8 @@ private static function isContainerGet(string $declaring_method_id): bool
return in_array(
$declaring_method_id,
array_map(
function($c) {
return $c . '::get';
function ($c) {
return $c.'::get';
},
self::GET_CLASSLIKES
),
Expand Down
43 changes: 43 additions & 0 deletions src/Handler/HeaderBagHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Psalm\SymfonyPsalmPlugin\Handler;

use PhpParser\Node\Expr;
use Psalm\Codebase;
use Psalm\Context;
use Psalm\Plugin\Hook\AfterMethodCallAnalysisInterface;
use Psalm\StatementsSource;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TString;
use Psalm\Type\Union;

class HeaderBagHandler implements AfterMethodCallAnalysisInterface
{
/**
* {@inheritdoc}
*/
public static function afterMethodCallAnalysis(
Expr $expr,
string $method_id,
string $appearing_method_id,
string $declaring_method_id,
Context $context,
StatementsSource $statements_source,
Codebase $codebase,
array &$file_replacements = [],
Union &$return_type_candidate = null
) {
if ('Symfony\Component\HttpFoundation\HeaderBag::get' === $declaring_method_id) {
if ($return_type_candidate) {
/** @psalm-suppress MixedArrayAccess */
if (isset($expr->args[2]->value->name->parts[0]) && 'false' === $expr->args[2]->value->name->parts[0]) {
$return_type_candidate = new Union([new TArray([new Union([new TInt()]), new Union([new TString()])])]);
} else {
$return_type_candidate = new Union([new TString(), new TNull()]);
}
}
}
}
}
9 changes: 6 additions & 3 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
use Doctrine\Common\Annotations\AnnotationRegistry;
use Psalm\Plugin\PluginEntryPointInterface;
use Psalm\Plugin\RegistrationInterface;
use Psalm\SymfonyPsalmPlugin\Handler\ClassHandler;
use Psalm\SymfonyPsalmPlugin\Handler\ConsoleHandler;
use Psalm\SymfonyPsalmPlugin\Handler\ContainerDependencyHandler;
use Psalm\SymfonyPsalmPlugin\Handler\ContainerHandler;
use Psalm\SymfonyPsalmPlugin\Handler\DoctrineRepositoryHandler;
use Psalm\SymfonyPsalmPlugin\Handler\HeaderBagHandler;
use Psalm\SymfonyPsalmPlugin\Symfony\ContainerMeta;
use SimpleXMLElement;

Expand All @@ -19,13 +20,15 @@ class Plugin implements PluginEntryPointInterface
*/
public function __invoke(RegistrationInterface $api, SimpleXMLElement $config = null)
{
require_once __DIR__.'/Handler/ClassHandler.php';
require_once __DIR__.'/Handler/HeaderBagHandler.php';
require_once __DIR__.'/Handler/ContainerHandler.php';
require_once __DIR__.'/Handler/ConsoleHandler.php';
require_once __DIR__.'/Handler/DoctrineRepositoryHandler.php';
require_once __DIR__.'/Handler/ContainerDependencyHandler.php';

$api->registerHooksFromClass(ClassHandler::class);
$api->registerHooksFromClass(HeaderBagHandler::class);
$api->registerHooksFromClass(ConsoleHandler::class);
$api->registerHooksFromClass(ContainerDependencyHandler::class);

if (class_exists(AnnotationRegistry::class)) {
/** @psalm-suppress DeprecatedMethod */
Expand Down
16 changes: 16 additions & 0 deletions tests/acceptance/acceptance/ContainerDependency.feature
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,19 @@ Feature: ContainerDependency
| Type | Message |
| ContainerDependency | Container must not inject into services as dependency! |
And I see no other errors

Scenario: Asserting container dependency issue can be suppressed inline
Given I have the following code
"""
<?php
use Symfony\Component\DependencyInjection\ContainerInterface;
class SomeService
{
/** @psalm-suppress ContainerDependency */
public function __construct(ContainerInterface $container)
{
}
}
"""
When I run Psalm
Then I see no errors