Skip to content

Commit 5c0fb46

Browse files
committed
Analyser - refactoring
1 parent edecfc2 commit 5c0fb46

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

src/Analyser/Analyser.php

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,14 @@ public function analyse(
131131
$internalErrorsCount = 0;
132132
$reachedInternalErrorsCountLimit = false;
133133
foreach ($files as $file) {
134-
try {
135-
$fileErrors = [];
136-
if ($preFileCallback !== null) {
137-
$preFileCallback($file);
138-
}
134+
if ($preFileCallback !== null) {
135+
$preFileCallback($file);
136+
}
137+
138+
$fileErrors = [];
139139

140-
if (is_file($file)) {
140+
if (is_file($file)) {
141+
try {
141142
$parserNodes = $this->parser->parseFile($file);
142143
$nodeCallback = function (\PhpParser\Node $node, Scope $scope) use (&$fileErrors, $file, $outerNodeCallback): void {
143144
if ($outerNodeCallback !== null) {
@@ -212,42 +213,42 @@ public function analyse(
212213
$scope,
213214
$nodeCallback
214215
);
215-
} elseif (is_dir($file)) {
216-
$fileErrors[] = new Error(sprintf('File %s is a directory.', $file), $file, null, false);
217-
} else {
218-
$fileErrors[] = new Error(sprintf('File %s does not exist.', $file), $file, null, false);
219-
}
220-
if ($postFileCallback !== null) {
221-
$postFileCallback($file);
222-
}
223-
224-
$errors = array_merge($errors, $fileErrors);
225-
} catch (\PhpParser\Error $e) {
226-
$errors[] = new Error($e->getMessage(), $file, $e->getStartLine() !== -1 ? $e->getStartLine() : null, false);
227-
} catch (\PHPStan\Parser\ParserErrorsException $e) {
228-
foreach ($e->getErrors() as $error) {
229-
$errors[] = new Error($error->getMessage(), $file, $error->getStartLine() !== -1 ? $error->getStartLine() : null, false);
230-
}
231-
} catch (\PHPStan\AnalysedCodeException $e) {
232-
$errors[] = new Error($e->getMessage(), $file, null, false);
233-
} catch (\Throwable $t) {
234-
if ($debug) {
235-
throw $t;
236-
}
237-
$internalErrorsCount++;
238-
$internalErrorMessage = sprintf('Internal error: %s', $t->getMessage());
239-
$internalErrorMessage .= sprintf(
240-
'%sRun PHPStan with --debug option and post the stack trace to:%s%s',
241-
"\n",
242-
"\n",
243-
'https://github.com/phpstan/phpstan/issues/new'
244-
);
245-
$errors[] = new Error($internalErrorMessage, $file);
246-
if ($internalErrorsCount >= $this->internalErrorsCountLimit) {
247-
$reachedInternalErrorsCountLimit = true;
248-
break;
216+
} catch (\PhpParser\Error $e) {
217+
$errors[] = new Error($e->getMessage(), $file, $e->getStartLine() !== -1 ? $e->getStartLine() : null, false);
218+
} catch (\PHPStan\Parser\ParserErrorsException $e) {
219+
foreach ($e->getErrors() as $error) {
220+
$errors[] = new Error($error->getMessage(), $file, $error->getStartLine() !== -1 ? $error->getStartLine() : null, false);
221+
}
222+
} catch (\PHPStan\AnalysedCodeException $e) {
223+
$errors[] = new Error($e->getMessage(), $file, null, false);
224+
} catch (\Throwable $t) {
225+
if ($debug) {
226+
throw $t;
227+
}
228+
$internalErrorsCount++;
229+
$internalErrorMessage = sprintf('Internal error: %s', $t->getMessage());
230+
$internalErrorMessage .= sprintf(
231+
'%sRun PHPStan with --debug option and post the stack trace to:%s%s',
232+
"\n",
233+
"\n",
234+
'https://github.com/phpstan/phpstan/issues/new'
235+
);
236+
$errors[] = new Error($internalErrorMessage, $file);
237+
if ($internalErrorsCount >= $this->internalErrorsCountLimit) {
238+
$reachedInternalErrorsCountLimit = true;
239+
break;
240+
}
249241
}
242+
} elseif (is_dir($file)) {
243+
$fileErrors[] = new Error(sprintf('File %s is a directory.', $file), $file, null, false);
244+
} else {
245+
$fileErrors[] = new Error(sprintf('File %s does not exist.', $file), $file, null, false);
250246
}
247+
if ($postFileCallback !== null) {
248+
$postFileCallback($file);
249+
}
250+
251+
$errors = array_merge($errors, $fileErrors);
251252
}
252253

253254
$this->restoreCollectErrorsHandler();

0 commit comments

Comments
 (0)