Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.10.x' into 1.11.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 15, 2023
2 parents 5813d92 + 6c941ae commit 26625c3
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 16 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"nikic/php-parser": "^4.17.1",
"ondram/ci-detector": "^3.4.0",
"ondrejmirtes/better-reflection": "6.12.0.2",
"phpstan/php-8-stubs": "0.3.72",
"phpstan/php-8-stubs": "0.3.73",
"phpstan/phpdoc-parser": "1.23.1",
"react/async": "^3",
"react/child-process": "^0.6.4",
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
{

}
1 change: 1 addition & 0 deletions issue-bot/playground.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
rules:
- PHPStan\Rules\Playground\FunctionNeverRule
- PHPStan\Rules\Playground\MethodNeverRule
- PHPStan\Rules\Playground\NotAnalysedTraitRule
- PHPStan\Rules\Playground\NoPhpCodeRule
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
62 changes: 62 additions & 0 deletions src/Rules/Playground/NotAnalysedTraitRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Playground;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\CollectedDataNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\Traits\TraitDeclarationCollector;
use PHPStan\Rules\Traits\TraitUseCollector;
use function sprintf;
use function strtolower;

/**
* @implements Rule<CollectedDataNode>
*/
class NotAnalysedTraitRule implements Rule
{

public function getNodeType(): string
{
return CollectedDataNode::class;
}

public function processNode(Node $node, Scope $scope): array
{
$traitDeclarationData = $node->get(TraitDeclarationCollector::class);
$traitUseData = $node->get(TraitUseCollector::class);

$declaredTraits = [];
foreach ($traitDeclarationData as $file => $declaration) {
foreach ($declaration as [$name, $line]) {
$declaredTraits[strtolower($name)] = [$file, $name, $line];
}
}

foreach ($traitUseData as $usedNamesData) {
foreach ($usedNamesData as $usedNames) {
foreach ($usedNames as $usedName) {
unset($declaredTraits[strtolower($usedName)]);
}
}
}

$errors = [];
foreach ($declaredTraits as [$file, $name, $line]) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Trait %s is used zero times and is not analysed.',
$name,
))
->identifier('phpstanPlayground.traitUnused')
->file($file)
->line($line)
->tip('See: https://phpstan.org/blog/how-phpstan-analyses-traits')
->build();
}

return $errors;
}

}
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

0 comments on commit 26625c3

Please sign in to comment.