Skip to content
Merged
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
14 changes: 8 additions & 6 deletions src/Analyser/FileAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use function count;
use function error_reporting;
use function get_class;
use function hash;
use function is_dir;
use function is_file;
use function restore_error_handler;
Expand All @@ -49,10 +50,10 @@
final class FileAnalyser
{

/** @var list<Error> */
/** @var array<string, Error> */
private array $allPhpErrors = [];

/** @var list<Error> */
/** @var array<string, Error> */
private array $filteredPhpErrors = [];

public function __construct(
Expand Down Expand Up @@ -326,8 +327,8 @@ public function analyseFile(

return new FileAnalyserResult(
$fileErrors,
$this->filteredPhpErrors,
$this->allPhpErrors,
array_values($this->filteredPhpErrors),
array_values($this->allPhpErrors),
$locallyIgnoredErrors,
$fileCollectedData,
array_values(array_unique($fileDependencies)),
Expand Down Expand Up @@ -366,8 +367,9 @@ private function collectErrors(array $analysedFiles): void
}

$errorMessage = sprintf('%s: %s', $this->getErrorLabel($errno), $errstr);
$errorSignature = hash('sha256', sprintf('%s:%s::%s', $errfile, $errline, $errorMessage));

$this->allPhpErrors[] = (new Error($errorMessage, $errfile, $errline, false))->withIdentifier('phpstan.php');
$this->allPhpErrors[$errorSignature] = (new Error($errorMessage, $errfile, $errline, false))->withIdentifier('phpstan.php');

if ($errno === E_DEPRECATED) {
return true;
Expand All @@ -377,7 +379,7 @@ private function collectErrors(array $analysedFiles): void
return true;
}

$this->filteredPhpErrors[] = (new Error($errorMessage, $errfile, $errline, $errno === E_USER_DEPRECATED))->withIdentifier('phpstan.php');
$this->filteredPhpErrors[$errorSignature] = (new Error($errorMessage, $errfile, $errline, $errno === E_USER_DEPRECATED))->withIdentifier('phpstan.php');

return true;
});
Expand Down
61 changes: 61 additions & 0 deletions tests/PHPStan/Analyser/Bug13813IntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PHPStan\Testing\PHPStanTestCase;
use PHPUnit\Framework\Attributes\CoversNothing;
use function array_map;
use function array_merge;
use function array_unique;
use function error_reporting;
use const E_ALL;

#[CoversNothing]
class Bug13813IntegrationTest extends PHPStanTestCase
{

public function testBug13813(): void
{
error_reporting(E_ALL);
$analyzerResult = $this->runAnalyse([
__DIR__ . '/data/bug-13813.php',
__DIR__ . '/Bug13813Rule.php',
]);
$this->assertCount(2, $analyzerResult->getAllPhpErrors());
$this->assertCount(2, $analyzerResult->getFilteredPhpErrors());

$this->assertSame(
'Warning: Undefined variable $x',
$analyzerResult->getAllPhpErrors()[0]->getMessage()
);
$this->assertSame(
'Warning: Undefined variable $x',
$analyzerResult->getAllPhpErrors()[1]->getMessage()
);
}

/**
* @param string[] $files
*/
private function runAnalyse(array $files): AnalyserResult
{
$files = array_map(fn (string $file): string => $this->getFileHelper()->normalizePath($file), $files);
/** @var Analyser $analyser */
$analyser = self::getContainer()->getByType(Analyser::class);

return $analyser->analyse($files);
}

public static function getAdditionalConfigFiles(): array
{
return array_unique(
array_merge(
parent::getAdditionalConfigFiles(),
[
__DIR__ . '/bug13813.neon',
],
),
);
}

}
30 changes: 30 additions & 0 deletions tests/PHPStan/Analyser/Bug13813Rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PHPStan\Rules\Rule;

/**
* @implements Rule<Node\Expr\Variable>
*/
class Bug13813Rule implements Rule
{

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

public function processNode(Node $node, Scope $scope): array
{
for ($i = 0; $i < 100; $i++) {
// @phpstan-ignore variable.undefined
echo $x; // force emit a PHP warning at runtime
}

return [];
}

}
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/bug13813.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rules:
- PHPStan\Analyser\Bug13813Rule
7 changes: 7 additions & 0 deletions tests/PHPStan/Analyser/data/bug-13813.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Bug13813;

function doFoo($x) {
echo $x;
}
Loading