Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/Analyser/AnalyserResultFinalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Analyser;

use Closure;
use PHPStan\AnalysedCodeException;
use PHPStan\BetterReflection\NodeCompiler\Exception\UnableToCompileNode;
use PHPStan\BetterReflection\Reflection\Exception\CircularReference;
Expand Down Expand Up @@ -38,7 +39,17 @@ public function __construct(
{
}

public function finalize(AnalyserResult $analyserResult, bool $onlyFiles, bool $debug): FinalizerResult
/**
* @param Closure(string $ruleClass): void|null $preRuleCallback
* @param Closure(): void|null $postRuleCallback called when the rule's processNode() has returned or thrown, before its errors are processed
*/
public function finalize(
AnalyserResult $analyserResult,
bool $onlyFiles,
bool $debug,
?Closure $preRuleCallback = null,
?Closure $postRuleCallback = null,
): FinalizerResult
{
if (count($analyserResult->getCollectedData()) === 0) {
return $this->addUnmatchedIgnoredErrors($this->mergeFilteredPhpErrors($analyserResult), [], []);
Expand All @@ -57,6 +68,10 @@ public function finalize(AnalyserResult $analyserResult, bool $onlyFiles, bool $
$tempCollectorErrors = [];
$internalErrors = $analyserResult->getInternalErrors();
foreach ($this->ruleRegistry->getRules($nodeType) as $rule) {
if ($preRuleCallback !== null) {
$preRuleCallback(get_class($rule));
}

try {
$ruleErrors = $rule->processNode($node, $scope);
} catch (AnalysedCodeException $e) {
Expand Down Expand Up @@ -96,6 +111,10 @@ public function finalize(AnalyserResult $analyserResult, bool $onlyFiles, bool $
shouldReportBug: true,
);
continue;
} finally {
if ($postRuleCallback !== null) {
$postRuleCallback();
}
}

foreach ($ruleErrors as $ruleError) {
Expand Down
47 changes: 47 additions & 0 deletions src/Command/AnalyseApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Command;

use Closure;
use PHPStan\Analyser\AnalyserResult;
use PHPStan\Analyser\AnalyserResultFinalizer;
use PHPStan\Analyser\Error;
Expand Down Expand Up @@ -135,10 +136,13 @@ public function analyse(
$processedFiles = $intermediateAnalyserResult->getProcessedFiles();

$resultCacheResult = $resultCacheManager->process($intermediateAnalyserResult, $resultCache, $errorOutput, $onlyFiles, true);
[$preRuleCallback, $postRuleCallback] = $this->createRuleCallbacks($debug, $stdOutput);
$analyserResult = $this->analyserResultFinalizer->finalize(
$this->switchTmpFileInAnalyserResult($resultCacheResult->getAnalyserResult(), $insteadOfFile, $tmpFile),
$onlyFiles,
$debug,
$preRuleCallback,
$postRuleCallback,
)->getAnalyserResult();
$internalErrors = $analyserResult->getInternalErrors();
$errors = array_merge(
Expand Down Expand Up @@ -291,6 +295,49 @@ private function runAnalyser(
return $analyserResult;
}

/**
* Prints the rules on CollectedDataNode the way --debug prints the analysed files. They run after
* all files and can take longer than the analysis itself.
*
* @return array{Closure(string $ruleClass): void|null, Closure(): void|null}
*/
private function createRuleCallbacks(bool $debug, Output $stdOutput): array
{
if (!$debug) {
return [
null,
null,
];
}

$startTime = null;
$preRuleCallback = static function (string $ruleClass) use ($stdOutput, &$startTime): void {
$stdOutput->writeLineFormatted(sprintf('Running %s on the collected data', $ruleClass));
$startTime = microtime(true);
};
if (!$stdOutput->isDebug()) {
return [
$preRuleCallback,
null,
];
}

$previousMemory = memory_get_peak_usage(true);
$postRuleCallback = static function () use ($stdOutput, &$previousMemory, &$startTime): void {
if ($startTime === null) {
throw new ShouldNotHappenException();
}
$currentTotalMemory = memory_get_peak_usage(true);
$stdOutput->writeLineFormatted(sprintf('--- consumed %s, total %s, took %.2f s', BytesHelper::bytes($currentTotalMemory - $previousMemory), BytesHelper::bytes($currentTotalMemory), microtime(true) - $startTime));
$previousMemory = $currentTotalMemory;
};

return [
$preRuleCallback,
$postRuleCallback,
];
}

private function switchTmpFileInAnalyserResult(
AnalyserResult $analyserResult,
?string $insteadOfFile,
Expand Down
157 changes: 157 additions & 0 deletions tests/PHPStan/Analyser/AnalyserResultFinalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PhpParser\Node;
use PHPStan\DependencyInjection\DirectExtensionsCollection;
use PHPStan\Node\CollectedDataNode;
use PHPStan\Rules\DirectRegistry as DirectRuleRegistry;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\Traits\TraitDeclarationCollector;
use PHPStan\Testing\PHPStanTestCase;
use RuntimeException;
use function get_class;

class AnalyserResultFinalizerTest extends PHPStanTestCase
{

public function testRuleCallbacksRunAroundEveryCollectedDataRule(): void
{
$reportingRule = $this->createReportingRule();
$throwingRule = $this->createThrowingRule();

$events = [];
$result = $this->createFinalizer([$reportingRule, $throwingRule])->finalize(
$this->createAnalyserResult(),
false,
false,
static function (string $ruleClass) use (&$events): void {
$events[] = 'pre ' . $ruleClass;
},
static function () use (&$events): void {
$events[] = 'post';
},
)->getAnalyserResult();

$this->assertSame([
'pre ' . get_class($reportingRule),
'post',
'pre ' . get_class($throwingRule),
'post',
], $events);
$this->assertCount(1, $result->getErrors());
$this->assertSame('Reported.', $result->getErrors()[0]->getMessage());
$this->assertCount(1, $result->getInternalErrors());
$this->assertSame('Failed.', $result->getInternalErrors()[0]->getMessage());
}

public function testPostRuleCallbackRunsWhenDebugRethrows(): void
{
$throwingRule = $this->createThrowingRule();

$events = [];
try {
$this->createFinalizer([$throwingRule])->finalize(
$this->createAnalyserResult(),
false,
true,
static function (string $ruleClass) use (&$events): void {
$events[] = 'pre ' . $ruleClass;
},
static function () use (&$events): void {
$events[] = 'post';
},
);
$this->fail('The exception should have been rethrown in debug mode.');
} catch (RuntimeException $e) {
$this->assertSame('Failed.', $e->getMessage());
}

$this->assertSame([
'pre ' . get_class($throwingRule),
'post',
], $events);
}

/**
* @param list<Rule<CollectedDataNode>> $rules
*/
private function createFinalizer(array $rules): AnalyserResultFinalizer
{
return new AnalyserResultFinalizer(
new DirectRuleRegistry($rules),
new DirectExtensionsCollection([]),
self::getContainer()->getByType(RuleErrorTransformer::class),
self::createScopeFactory(
self::createReflectionProvider(),
self::getContainer()->getService('typeSpecifier'),
),
new LocalIgnoresProcessor(),
false,
);
}

private function createAnalyserResult(): AnalyserResult
{
return new AnalyserResult(
unorderedErrors: [],
filteredPhpErrors: [],
allPhpErrors: [],
locallyIgnoredErrors: [],
linesToIgnore: [],
unmatchedLineIgnores: [],
internalErrors: [],
collectedData: [__FILE__ => [TraitDeclarationCollector::class => ['data']]],
dependencies: null,
usedTraitDependencies: null,
valueDependencies: null,
packageDependencies: null,
exportedNodes: [],
reachedInternalErrorsCountLimit: false,
peakMemoryUsageBytes: 0,
processedFiles: [],
);
}

/**
* @return Rule<CollectedDataNode>
*/
private function createReportingRule(): Rule
{
return new /** @implements Rule<CollectedDataNode> */class implements Rule {

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

public function processNode(Node $node, Scope $scope): array
{
return [RuleErrorBuilder::message('Reported.')->identifier('tests.reported')->file(__FILE__)->line(1)->build()];
}

};
}

/**
* @return Rule<CollectedDataNode>
*/
private function createThrowingRule(): Rule
{
return new /** @implements Rule<CollectedDataNode> */class implements Rule {

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

public function processNode(Node $node, Scope $scope): array
{
throw new RuntimeException('Failed.');
}

};
}

}
Loading