Skip to content

Commit

Permalink
NotAnalysedTraitRule - do not report for analysis of files-only paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 15, 2023
1 parent e2b2584 commit 5f59d58
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions e2e/only-files-not-analysed-trait/ignore.neon
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions e2e/only-files-not-analysed-trait/no-ignore.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
includes:
- ../../conf/bleedingEdge.neon

parameters:
level: 8
8 changes: 8 additions & 0 deletions e2e/only-files-not-analysed-trait/src/BarTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace OnlyFilesNotAnalysedTrait;

trait BarTrait
{

}
8 changes: 8 additions & 0 deletions e2e/only-files-not-analysed-trait/src/Foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace OnlyFilesNotAnalysedTrait;

class Foo
{

}
6 changes: 3 additions & 3 deletions src/Command/AnalyseApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function analyse(
$memoryUsageBytes = $analyserResult->getPeakMemoryUsageBytes();

if (!$hasInternalErrors) {
foreach ($this->getCollectedDataErrors($analyserResult->getCollectedData()) as $error) {
foreach ($this->getCollectedDataErrors($analyserResult->getCollectedData(), $onlyFiles) as $error) {
$errors[] = $error;
}
}
Expand Down Expand Up @@ -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 = [];
Expand Down
6 changes: 3 additions & 3 deletions src/Command/FixerWorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 11 additions & 1 deletion src/Node/CollectedDataNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
}
Expand Down Expand Up @@ -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';
Expand Down
4 changes: 4 additions & 0 deletions src/Rules/Traits/NotAnalysedTraitRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Testing/RuleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

1 comment on commit 5f59d58

@janedbal
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This basically fix phpstan/phpstan#8403, thank you!

Please sign in to comment.