From 257eb04160677c6ef2cdb7dc9366f8d318898837 Mon Sep 17 00:00:00 2001 From: Farhad Safarov Date: Wed, 5 Jun 2024 09:31:06 +0300 Subject: [PATCH] Fix static analysis errors & CS (#338) --- src/Handler/ConsoleHandler.php | 8 ++++---- src/Handler/ContainerHandler.php | 4 ++-- src/Handler/HeaderBagHandler.php | 3 +-- src/Plugin.php | 2 +- src/Symfony/ContainerMeta.php | 11 +++++------ src/Twig/TemplateFileAnalyzer.php | 11 ++++------- 6 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/Handler/ConsoleHandler.php b/src/Handler/ConsoleHandler.php index c18f511..def0894 100644 --- a/src/Handler/ConsoleHandler.php +++ b/src/Handler/ConsoleHandler.php @@ -57,7 +57,7 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve break; case 'Symfony\Component\Console\Input\InputInterface::getargument': $identifier = self::getNodeIdentifier($args[0]->value); - if (!$identifier) { + if (null === $identifier) { break; } @@ -70,7 +70,7 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve break; case 'Symfony\Component\Console\Input\InputInterface::getoption': $identifier = self::getNodeIdentifier($args[0]->value); - if (!$identifier) { + if (null === $identifier) { break; } @@ -126,7 +126,7 @@ private static function analyseArgument(array $args, StatementsSource $statement $normalizedParams = self::normalizeArgumentParams($args); $identifier = self::getNodeIdentifier($normalizedParams['name']->value); - if (!$identifier) { + if (null === $identifier) { return; } @@ -170,7 +170,7 @@ private static function analyseOption(array $args, StatementsSource $statements_ $normalizedParams = self::normalizeOptionParams($args); $identifier = self::getNodeIdentifier($normalizedParams['name']->value); - if (!$identifier) { + if (null === $identifier) { return; } diff --git a/src/Handler/ContainerHandler.php b/src/Handler/ContainerHandler.php index c22ae57..849e300 100644 --- a/src/Handler/ContainerHandler.php +++ b/src/Handler/ContainerHandler.php @@ -105,7 +105,7 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve if ('self' === $className) { $className = $event->getStatementsSource()->getSource()->getFQCLN(); } - if (!$idArgument->name instanceof Identifier || !$className) { + if (!$idArgument->name instanceof Identifier || null === $className) { return; } @@ -141,7 +141,7 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve if (!$service->isPublic()) { /** @var class-string $kernelTestCaseClass */ $kernelTestCaseClass = 'Symfony\Bundle\FrameworkBundle\Test\KernelTestCase'; - $isTestContainer = $context->parent + $isTestContainer = null !== $context->parent && ($kernelTestCaseClass === $context->parent || is_subclass_of($context->parent, $kernelTestCaseClass) ); diff --git a/src/Handler/HeaderBagHandler.php b/src/Handler/HeaderBagHandler.php index e582708..401a17a 100644 --- a/src/Handler/HeaderBagHandler.php +++ b/src/Handler/HeaderBagHandler.php @@ -6,7 +6,6 @@ use PhpParser\Node\Scalar\String_; use Psalm\Plugin\EventHandler\Event\MethodReturnTypeProviderEvent; use Psalm\Plugin\EventHandler\MethodReturnTypeProviderInterface; -use Psalm\Type; use Psalm\Type\Atomic\TArray; use Psalm\Type\Atomic\TInt; use Psalm\Type\Atomic\TNull; @@ -24,7 +23,7 @@ public static function getClassLikeNames(): array ]; } - public static function getMethodReturnType(MethodReturnTypeProviderEvent $event): ?Type\Union + public static function getMethodReturnType(MethodReturnTypeProviderEvent $event): ?Union { $fq_classlike_name = $event->getFqClasslikeName(); $method_name_lowercase = $event->getMethodNameLowercase(); diff --git a/src/Plugin.php b/src/Plugin.php index ab61d40..d329c74 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -29,7 +29,7 @@ */ class Plugin implements PluginEntryPointInterface { - public function __invoke(RegistrationInterface $api, \SimpleXMLElement $config = null): void + public function __invoke(RegistrationInterface $api, ?\SimpleXMLElement $config = null): void { require_once __DIR__.'/Handler/HeaderBagHandler.php'; require_once __DIR__.'/Handler/ContainerHandler.php'; diff --git a/src/Symfony/ContainerMeta.php b/src/Symfony/ContainerMeta.php index 7dfee1d..e055ac8 100644 --- a/src/Symfony/ContainerMeta.php +++ b/src/Symfony/ContainerMeta.php @@ -44,9 +44,9 @@ public function __construct(array $containerXmlPaths) /** * @throws ServiceNotFoundException */ - public function get(string $id, string $contextClass = null): Definition + public function get(string $id, ?string $contextClass = null): Definition { - if ($contextClass && isset($this->classLocators[$contextClass]) && isset($this->serviceLocators[$this->classLocators[$contextClass]]) && isset($this->serviceLocators[$this->classLocators[$contextClass]][$id])) { + if (null !== $contextClass && isset($this->classLocators[$contextClass]) && isset($this->serviceLocators[$this->classLocators[$contextClass]]) && isset($this->serviceLocators[$this->classLocators[$contextClass]][$id])) { $id = $this->serviceLocators[$this->classLocators[$contextClass]][$id]; try { @@ -91,12 +91,12 @@ private function init(array $containerXmlPaths): void $containerXmlPath = null; foreach ($containerXmlPaths as $filePath) { $containerXmlPath = realpath((string) $filePath); - if ($containerXmlPath) { + if (false !== $containerXmlPath) { break; } } - if (!$containerXmlPath) { + if (!is_string($containerXmlPath)) { throw new ConfigException('Container xml file(s) not found!'); } @@ -117,8 +117,7 @@ private function init(array $containerXmlPaths): void } foreach ($this->container->findTaggedServiceIds('container.service_locator') as $key => $_) { - $definition = $this->container->getDefinition($key); - foreach ($definition->getArgument(0) as $id => $argument) { + foreach ($this->container->getDefinition($key)->getArgument(0) as $id => $argument) { if ($argument instanceof Reference) { $this->addServiceLocator($key, $id, $argument); } elseif ($argument instanceof ServiceClosureArgument) { diff --git a/src/Twig/TemplateFileAnalyzer.php b/src/Twig/TemplateFileAnalyzer.php index cbb3251..76b968c 100644 --- a/src/Twig/TemplateFileAnalyzer.php +++ b/src/Twig/TemplateFileAnalyzer.php @@ -18,15 +18,12 @@ */ class TemplateFileAnalyzer extends FileAnalyzer { - /** - * @var string - */ - private static $rootPath = 'templates'; + private static string $rootPath = 'templates'; /** * @var list */ - private static $extensionClasses = []; + private static array $extensionClasses = []; /** * @param list $extensionClasses @@ -37,8 +34,8 @@ public static function initExtensions(array $extensionClasses): void } public function analyze( - PsalmContext $file_context = null, - PsalmContext $global_context = null + ?PsalmContext $file_context = null, + ?PsalmContext $global_context = null ): void { $codebase = $this->project_analyzer->getCodebase(); $taint = $codebase->taint_flow_graph;