From 1b3a673f6d0d1875b232ae65ede5371eb6eeff6c Mon Sep 17 00:00:00 2001 From: Farhad Safarov Date: Mon, 22 Jun 2020 11:00:30 +0300 Subject: [PATCH 1/2] cleanup & inline suppressible plugin issue --- src/Handler/ClassHandler.php | 73 ------------------- src/Handler/ContainerDependencyHandler.php | 38 ++++++++++ src/Handler/ContainerHandler.php | 6 +- src/Handler/HeaderBagHandler.php | 43 +++++++++++ src/Plugin.php | 9 ++- .../acceptance/ContainerDependency.feature | 16 ++++ 6 files changed, 106 insertions(+), 79 deletions(-) delete mode 100644 src/Handler/ClassHandler.php create mode 100644 src/Handler/ContainerDependencyHandler.php create mode 100644 src/Handler/HeaderBagHandler.php diff --git a/src/Handler/ClassHandler.php b/src/Handler/ClassHandler.php deleted file mode 100644 index caebdfa7..00000000 --- a/src/Handler/ClassHandler.php +++ /dev/null @@ -1,73 +0,0 @@ -stmts as $stmt) { - 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, $stmt)), - $statements_source->getSuppressedIssues() - ); - } - } - } - } - - return null; - } - - /** - * {@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 - ) { - switch ($declaring_method_id) { - case 'Symfony\Component\HttpFoundation\HeaderBag::get': - 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()]); - } - } - break; - } - } -} diff --git a/src/Handler/ContainerDependencyHandler.php b/src/Handler/ContainerDependencyHandler.php new file mode 100644 index 00000000..fa15c275 --- /dev/null +++ b/src/Handler/ContainerDependencyHandler.php @@ -0,0 +1,38 @@ +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() + ); + } + } + } + } +} diff --git a/src/Handler/ContainerHandler.php b/src/Handler/ContainerHandler.php index 29507037..bba4ec0e 100644 --- a/src/Handler/ContainerHandler.php +++ b/src/Handler/ContainerHandler.php @@ -109,7 +109,7 @@ public static function afterMethodCallAnalysis( } /** - * {@inheritDoc} + * {@inheritdoc} */ public static function afterClassLikeVisit( ClassLike $class_node, @@ -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 ), diff --git a/src/Handler/HeaderBagHandler.php b/src/Handler/HeaderBagHandler.php new file mode 100644 index 00000000..c3a42311 --- /dev/null +++ b/src/Handler/HeaderBagHandler.php @@ -0,0 +1,43 @@ +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()]); + } + } + } + } +} diff --git a/src/Plugin.php b/src/Plugin.php index 477cd1f3..31238140 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -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; @@ -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 */ diff --git a/tests/acceptance/acceptance/ContainerDependency.feature b/tests/acceptance/acceptance/ContainerDependency.feature index 6ff4e917..4beb2f3a 100644 --- a/tests/acceptance/acceptance/ContainerDependency.feature +++ b/tests/acceptance/acceptance/ContainerDependency.feature @@ -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 + """ + Date: Mon, 22 Jun 2020 11:09:33 +0300 Subject: [PATCH 2/2] no message --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 4c95e3ec..3888d298 100644 --- a/composer.json +++ b/composer.json @@ -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",