diff --git a/.github/workflows/e2e-tests.yml b/.github/workflows/e2e-tests.yml index 91f29afc74..4158790ac5 100644 --- a/.github/workflows/e2e-tests.yml +++ b/.github/workflows/e2e-tests.yml @@ -175,6 +175,10 @@ jobs: extensions: "" - script: "bin/phpstan analyse -l 8 e2e/phpstan-phpunit-190/test.php -c e2e/phpstan-phpunit-190/test.neon" extensions: "" + - script: "bin/phpstan analyse e2e/only-files-not-analysed-trait/src -c e2e/only-files-not-analysed-trait/ignore.neon" + extensions: "" + - script: "bin/phpstan analyse e2e/only-files-not-analysed-trait/src/Foo.php e2e/only-files-not-analysed-trait/src/BarTrait.php -c e2e/only-files-not-analysed-trait/no-ignore.neon" + extensions: "" steps: - name: "Checkout" diff --git a/e2e/only-files-not-analysed-trait/ignore.neon b/e2e/only-files-not-analysed-trait/ignore.neon new file mode 100644 index 0000000000..e0257ac498 --- /dev/null +++ b/e2e/only-files-not-analysed-trait/ignore.neon @@ -0,0 +1,10 @@ +includes: + - ../../conf/bleedingEdge.neon + +parameters: + level: 8 + ignoreErrors: + - + message: "#^Trait OnlyFilesNotAnalysedTrait\\\\BarTrait is used zero times and is not analysed\\.$#" + count: 1 + path: src/BarTrait.php diff --git a/e2e/only-files-not-analysed-trait/no-ignore.neon b/e2e/only-files-not-analysed-trait/no-ignore.neon new file mode 100644 index 0000000000..899fee922c --- /dev/null +++ b/e2e/only-files-not-analysed-trait/no-ignore.neon @@ -0,0 +1,5 @@ +includes: + - ../../conf/bleedingEdge.neon + +parameters: + level: 8 diff --git a/e2e/only-files-not-analysed-trait/src/BarTrait.php b/e2e/only-files-not-analysed-trait/src/BarTrait.php new file mode 100644 index 0000000000..efb6e5abb5 --- /dev/null +++ b/e2e/only-files-not-analysed-trait/src/BarTrait.php @@ -0,0 +1,8 @@ +getPeakMemoryUsageBytes(); if (!$hasInternalErrors) { - foreach ($this->getCollectedDataErrors($analyserResult->getCollectedData()) as $error) { + foreach ($this->getCollectedDataErrors($analyserResult->getCollectedData(), $onlyFiles) as $error) { $errors[] = $error; } } @@ -149,10 +149,10 @@ public function analyse( * @param CollectedData[] $collectedData * @return Error[] */ - private function getCollectedDataErrors(array $collectedData): array + private function getCollectedDataErrors(array $collectedData, bool $onlyFiles): array { $nodeType = CollectedDataNode::class; - $node = new CollectedDataNode($collectedData); + $node = new CollectedDataNode($collectedData, $onlyFiles); $file = 'N/A'; $scope = $this->scopeFactory->create(ScopeContext::create($file)); $errors = []; diff --git a/src/Command/FixerWorkerCommand.php b/src/Command/FixerWorkerCommand.php index f0742aca68..08a8cc250c 100644 --- a/src/Command/FixerWorkerCommand.php +++ b/src/Command/FixerWorkerCommand.php @@ -145,7 +145,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $hasInternalErrors, ); if (!$hasInternalErrors) { - foreach ($this->getCollectedDataErrors($container, $result->getCollectedData()) as $error) { + foreach ($this->getCollectedDataErrors($container, $result->getCollectedData(), $isOnlyFiles) as $error) { $intermediateErrors[] = $error; } } @@ -173,10 +173,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int * @param CollectedData[] $collectedData * @return Error[] */ - private function getCollectedDataErrors(Container $container, array $collectedData): array + private function getCollectedDataErrors(Container $container, array $collectedData, bool $onlyFiles): array { $nodeType = CollectedDataNode::class; - $node = new CollectedDataNode($collectedData); + $node = new CollectedDataNode($collectedData, $onlyFiles); $file = 'N/A'; $scope = $container->getByType(ScopeFactory::class)->create(ScopeContext::create($file)); $ruleRegistry = $container->getByType(RuleRegistry::class); diff --git a/src/Node/CollectedDataNode.php b/src/Node/CollectedDataNode.php index 7ebc61eb5d..8f7684e1ec 100644 --- a/src/Node/CollectedDataNode.php +++ b/src/Node/CollectedDataNode.php @@ -15,7 +15,7 @@ class CollectedDataNode extends NodeAbstract /** * @param CollectedData[] $collectedData */ - public function __construct(private array $collectedData) + public function __construct(private array $collectedData, private bool $onlyFiles) { parent::__construct([]); } @@ -45,6 +45,16 @@ public function get(string $collectorType): array return $result; } + /** + * Indicates that only files were passed to the analyser, not directory paths. + * + * True being returned strongly suggests that it's a partial analysis, not full project analysis. + */ + public function isOnlyFilesAnalysis(): bool + { + return $this->onlyFiles; + } + public function getType(): string { return 'PHPStan_Node_CollectedDataNode'; diff --git a/src/Rules/Traits/NotAnalysedTraitRule.php b/src/Rules/Traits/NotAnalysedTraitRule.php index 2c8d9c00ce..e5468e59f1 100644 --- a/src/Rules/Traits/NotAnalysedTraitRule.php +++ b/src/Rules/Traits/NotAnalysedTraitRule.php @@ -23,6 +23,10 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { + if ($node->isOnlyFilesAnalysis()) { + return []; + } + $traitDeclarationData = $node->get(TraitDeclarationCollector::class); $traitUseData = $node->get(TraitUseCollector::class); diff --git a/src/Testing/RuleTestCase.php b/src/Testing/RuleTestCase.php index 8f388d9cd5..8630295437 100644 --- a/src/Testing/RuleTestCase.php +++ b/src/Testing/RuleTestCase.php @@ -181,7 +181,7 @@ public function gatherAnalyserErrors(array $files): array ]); $nodeType = CollectedDataNode::class; - $node = new CollectedDataNode($analyserResult->getCollectedData()); + $node = new CollectedDataNode($analyserResult->getCollectedData(), false); $scopeFactory = $this->createScopeFactory($this->createReflectionProvider(), $this->getTypeSpecifier()); $scope = $scopeFactory->create(ScopeContext::create('irrelevant')); foreach ($ruleRegistry->getRules($nodeType) as $rule) {