Skip to content

Commit

Permalink
Move collecting errors from error handler from Analyser to FileAnalyser
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 28, 2022
1 parent f1734dc commit 7dd699f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 45 deletions.
45 changes: 0 additions & 45 deletions src/Analyser/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,11 @@
use function array_fill_keys;
use function array_merge;
use function count;
use function error_reporting;
use function in_array;
use function restore_error_handler;
use function set_error_handler;
use function sprintf;
use const E_DEPRECATED;

class Analyser
{

/** @var Error[] */
private array $collectedErrors = [];

public function __construct(
private FileAnalyser $fileAnalyser,
private Registry $registry,
Expand Down Expand Up @@ -51,8 +43,6 @@ public function analyse(
$this->nodeScopeResolver->setAnalysedFiles($allAnalysedFiles);
$allAnalysedFiles = array_fill_keys($allAnalysedFiles, true);

$this->collectErrors($files);

$errors = [];
$internalErrorsCount = 0;
$reachedInternalErrorsCountLimit = false;
Expand Down Expand Up @@ -103,10 +93,6 @@ public function analyse(
$postFileCallback(1);
}

$this->restoreCollectErrorsHandler();

$errors = array_merge($errors, $this->collectedErrors);

return new AnalyserResult(
$errors,
[],
Expand All @@ -116,35 +102,4 @@ public function analyse(
);
}

/**
* @param string[] $analysedFiles
*/
private function collectErrors(array $analysedFiles): void
{
$this->collectedErrors = [];
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) use ($analysedFiles): bool {
if ((error_reporting() & $errno) === 0) {
// silence @ operator
return true;
}

if ($errno === E_DEPRECATED) {
return true;
}

if (!in_array($errfile, $analysedFiles, true)) {
return true;
}

$this->collectedErrors[] = new Error($errstr, $errfile, $errline, true);

return true;
});
}

private function restoreCollectErrorsHandler(): void
{
restore_error_handler();
}

}
44 changes: 44 additions & 0 deletions src/Analyser/FileAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,26 @@
use PHPStan\Rules\TipRuleError;
use function array_key_exists;
use function array_keys;
use function array_merge;
use function array_unique;
use function array_values;
use function error_reporting;
use function get_class;
use function is_dir;
use function is_file;
use function is_string;
use function restore_error_handler;
use function set_error_handler;
use function sprintf;
use function strpos;
use const E_DEPRECATED;

class FileAnalyser
{

/** @var Error[] */
private array $collectedErrors = [];

public function __construct(
private ScopeFactory $scopeFactory,
private NodeScopeResolver $nodeScopeResolver,
Expand All @@ -60,6 +68,7 @@ public function analyseFile(
$exportedNodes = [];
if (is_file($file)) {
try {
$this->collectErrors($analysedFiles);
$parserNodes = $this->parser->parseFile($file);
$linesToIgnore = $this->getLinesToIgnoreFromTokens($file, $parserNodes);
$temporaryFileErrors = [];
Expand Down Expand Up @@ -257,6 +266,10 @@ public function analyseFile(
$fileErrors[] = new Error(sprintf('File %s does not exist.', $file), $file, null, false);
}

$this->restoreCollectErrorsHandler();

$fileErrors = array_merge($fileErrors, $this->collectedErrors);

return new FileAnalyserResult($fileErrors, array_values(array_unique($fileDependencies)), $exportedNodes);
}

Expand Down Expand Up @@ -328,4 +341,35 @@ private function findLineToIgnoreComment(Comment $comment): ?int
return null;
}

/**
* @param array<string, true> $analysedFiles
*/
private function collectErrors(array $analysedFiles): void
{
$this->collectedErrors = [];
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) use ($analysedFiles): bool {
if ((error_reporting() & $errno) === 0) {
// silence @ operator
return true;
}

if ($errno === E_DEPRECATED) {
return true;
}

if (!isset($analysedFiles[$errfile])) {
return true;
}

$this->collectedErrors[] = new Error($errstr, $errfile, $errline, true);

return true;
});
}

private function restoreCollectErrorsHandler(): void
{
restore_error_handler();
}

}

0 comments on commit 7dd699f

Please sign in to comment.