Skip to content

Commit 7dd699f

Browse files
committed
Move collecting errors from error handler from Analyser to FileAnalyser
1 parent f1734dc commit 7dd699f

File tree

2 files changed

+44
-45
lines changed

2 files changed

+44
-45
lines changed

src/Analyser/Analyser.php

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,11 @@
88
use function array_fill_keys;
99
use function array_merge;
1010
use function count;
11-
use function error_reporting;
12-
use function in_array;
13-
use function restore_error_handler;
14-
use function set_error_handler;
1511
use function sprintf;
16-
use const E_DEPRECATED;
1712

1813
class Analyser
1914
{
2015

21-
/** @var Error[] */
22-
private array $collectedErrors = [];
23-
2416
public function __construct(
2517
private FileAnalyser $fileAnalyser,
2618
private Registry $registry,
@@ -51,8 +43,6 @@ public function analyse(
5143
$this->nodeScopeResolver->setAnalysedFiles($allAnalysedFiles);
5244
$allAnalysedFiles = array_fill_keys($allAnalysedFiles, true);
5345

54-
$this->collectErrors($files);
55-
5646
$errors = [];
5747
$internalErrorsCount = 0;
5848
$reachedInternalErrorsCountLimit = false;
@@ -103,10 +93,6 @@ public function analyse(
10393
$postFileCallback(1);
10494
}
10595

106-
$this->restoreCollectErrorsHandler();
107-
108-
$errors = array_merge($errors, $this->collectedErrors);
109-
11096
return new AnalyserResult(
11197
$errors,
11298
[],
@@ -116,35 +102,4 @@ public function analyse(
116102
);
117103
}
118104

119-
/**
120-
* @param string[] $analysedFiles
121-
*/
122-
private function collectErrors(array $analysedFiles): void
123-
{
124-
$this->collectedErrors = [];
125-
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) use ($analysedFiles): bool {
126-
if ((error_reporting() & $errno) === 0) {
127-
// silence @ operator
128-
return true;
129-
}
130-
131-
if ($errno === E_DEPRECATED) {
132-
return true;
133-
}
134-
135-
if (!in_array($errfile, $analysedFiles, true)) {
136-
return true;
137-
}
138-
139-
$this->collectedErrors[] = new Error($errstr, $errfile, $errline, true);
140-
141-
return true;
142-
});
143-
}
144-
145-
private function restoreCollectErrorsHandler(): void
146-
{
147-
restore_error_handler();
148-
}
149-
150105
}

src/Analyser/FileAnalyser.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,26 @@
2222
use PHPStan\Rules\TipRuleError;
2323
use function array_key_exists;
2424
use function array_keys;
25+
use function array_merge;
2526
use function array_unique;
2627
use function array_values;
28+
use function error_reporting;
2729
use function get_class;
2830
use function is_dir;
2931
use function is_file;
3032
use function is_string;
33+
use function restore_error_handler;
34+
use function set_error_handler;
3135
use function sprintf;
3236
use function strpos;
37+
use const E_DEPRECATED;
3338

3439
class FileAnalyser
3540
{
3641

42+
/** @var Error[] */
43+
private array $collectedErrors = [];
44+
3745
public function __construct(
3846
private ScopeFactory $scopeFactory,
3947
private NodeScopeResolver $nodeScopeResolver,
@@ -60,6 +68,7 @@ public function analyseFile(
6068
$exportedNodes = [];
6169
if (is_file($file)) {
6270
try {
71+
$this->collectErrors($analysedFiles);
6372
$parserNodes = $this->parser->parseFile($file);
6473
$linesToIgnore = $this->getLinesToIgnoreFromTokens($file, $parserNodes);
6574
$temporaryFileErrors = [];
@@ -257,6 +266,10 @@ public function analyseFile(
257266
$fileErrors[] = new Error(sprintf('File %s does not exist.', $file), $file, null, false);
258267
}
259268

269+
$this->restoreCollectErrorsHandler();
270+
271+
$fileErrors = array_merge($fileErrors, $this->collectedErrors);
272+
260273
return new FileAnalyserResult($fileErrors, array_values(array_unique($fileDependencies)), $exportedNodes);
261274
}
262275

@@ -328,4 +341,35 @@ private function findLineToIgnoreComment(Comment $comment): ?int
328341
return null;
329342
}
330343

344+
/**
345+
* @param array<string, true> $analysedFiles
346+
*/
347+
private function collectErrors(array $analysedFiles): void
348+
{
349+
$this->collectedErrors = [];
350+
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) use ($analysedFiles): bool {
351+
if ((error_reporting() & $errno) === 0) {
352+
// silence @ operator
353+
return true;
354+
}
355+
356+
if ($errno === E_DEPRECATED) {
357+
return true;
358+
}
359+
360+
if (!isset($analysedFiles[$errfile])) {
361+
return true;
362+
}
363+
364+
$this->collectedErrors[] = new Error($errstr, $errfile, $errline, true);
365+
366+
return true;
367+
});
368+
}
369+
370+
private function restoreCollectErrorsHandler(): void
371+
{
372+
restore_error_handler();
373+
}
374+
331375
}

0 commit comments

Comments
 (0)