From dcd0e894140e7575bca950095643ea7918122a58 Mon Sep 17 00:00:00 2001 From: Markus Staab <47448731+clxmstaab@users.noreply.github.com> Date: Tue, 3 Jun 2025 09:58:13 +0200 Subject: [PATCH 1/2] Run analyzer only once --- extension.neon | 2 -- src/ComposerCollector.php | 12 +++++++----- src/ComposerRule.php | 12 +++++++++--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/extension.neon b/extension.neon index 0c46ce2..b73cdce 100644 --- a/extension.neon +++ b/extension.neon @@ -4,8 +4,6 @@ services: arguments: cwd: %currentWorkingDirectory% options: %composerAnalysis% - tags: - - phpstan.collector rules: - ComposerAnalyzer\ComposerRule diff --git a/src/ComposerCollector.php b/src/ComposerCollector.php index b29ba31..1397bd6 100644 --- a/src/ComposerCollector.php +++ b/src/ComposerCollector.php @@ -16,11 +16,8 @@ use ShipMonk\ComposerDependencyAnalyser\Result\SymbolUsage; use ShipMonk\ComposerDependencyAnalyser\Stopwatch; -class ComposerCollector implements Collector +class ComposerCollector { - /** @var RuleError[] */ - public static array $results; - private string $cwd; /** @var string[] */ @@ -58,9 +55,14 @@ public function __construct(string $cwd, array $options) $this->ignoreAllUnusedDeps = boolval($options['ignoreAllUnusedDeps'] ?? false); $this->disableExtensionsAnalysis = boolval($options['disableExtensionsAnalysis'] ?? false); $this->ignoreSpecificUnusedDeps = $options['ignoreSpecificUnusedDeps'] ?? []; + } + /** + * @return RuleError[] + */ + public function analyze(): array { $results = $this->runComposerDependencyAnalyser(); - self::$results = $this->reformatResults($results); + return $this->reformatResults($results); } private function runComposerDependencyAnalyser(): AnalysisResult diff --git a/src/ComposerRule.php b/src/ComposerRule.php index 3e918a9..5a12b1b 100644 --- a/src/ComposerRule.php +++ b/src/ComposerRule.php @@ -8,6 +8,9 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleError; +/** + * @implements Rule + */ class ComposerRule implements Rule { private ComposerCollector $depAnalyzer; @@ -19,6 +22,8 @@ public function __construct(ComposerCollector $depAnalyzer) public function getNodeType(): string { + // use collector rule, to make sure we run the underlying analyzer only once + // see https://github.com/pb30/phpstan-composer-analysis/issues/10 return CollectedDataNode::class; } @@ -27,9 +32,10 @@ public function getNodeType(): string */ public function processNode(Node $node, Scope $scope): array { - $errors = $this->depAnalyzer::$results; - $this->depAnalyzer::$results = []; + if ($node->isOnlyFilesAnalysis()) { + return []; + } - return $errors; + return $this->depAnalyzer->analyze(); } } From 56351b2b613836d48f6e833cbcab3eb1a48124c1 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 3 Jun 2025 10:02:57 +0200 Subject: [PATCH 2/2] Update phpstan.neon --- phpstan.neon | 4 ---- src/ComposerCollector.php | 4 +++- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index a2797da..1487407 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -6,10 +6,6 @@ parameters: paths: - src - ignoreErrors: - - - identifier: missingType.generics - composerAnalysis: ignoreAllShadowDeps: false ignoreAllDevDepsInProd: false diff --git a/src/ComposerCollector.php b/src/ComposerCollector.php index 1397bd6..e28e2d8 100644 --- a/src/ComposerCollector.php +++ b/src/ComposerCollector.php @@ -60,8 +60,10 @@ public function __construct(string $cwd, array $options) /** * @return RuleError[] */ - public function analyze(): array { + public function analyze(): array + { $results = $this->runComposerDependencyAnalyser(); + return $this->reformatResults($results); }