Skip to content

Commit 4d2d088

Browse files
committed
Introduce unified AnalyserResult
1 parent 4a890d6 commit 4d2d088

8 files changed

Lines changed: 69 additions & 31 deletions

File tree

src/Analyser/Analyser.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,19 @@ public function __construct(
4646
* @param \Closure(string $file): void|null $preFileCallback
4747
* @param \Closure(int): void|null $postFileCallback
4848
* @param bool $debug
49-
* @param callable(\PhpParser\Node $node, Scope $scope): void|null $outerNodeCallback
50-
* @return string[]|\PHPStan\Analyser\Error[] errors
49+
* @return AnalyserResult
5150
*/
5251
public function analyse(
5352
array $files,
5453
bool $onlyFiles,
5554
?\Closure $preFileCallback = null,
5655
?\Closure $postFileCallback = null,
57-
bool $debug = false,
58-
?callable $outerNodeCallback = null
59-
): array
56+
bool $debug = false
57+
): AnalyserResult
6058
{
6159
$ignoredErrorHelperResult = $this->ignoredErrorHelper->initialize();
6260
if (count($ignoredErrorHelperResult->getErrors()) > 0) {
63-
return $ignoredErrorHelperResult->getErrors();
61+
return new AnalyserResult($ignoredErrorHelperResult->getErrors(), false);
6462
}
6563

6664
$this->nodeScopeResolver->setAnalysedFiles($files);
@@ -70,6 +68,7 @@ public function analyse(
7068
$errors = [];
7169
$internalErrorsCount = 0;
7270
$reachedInternalErrorsCountLimit = false;
71+
$inferrablePropertyTypesFromConstructorHelper = new InferrablePropertyTypesFromConstructorHelper();
7372
foreach ($files as $file) {
7473
if ($preFileCallback !== null) {
7574
$preFileCallback($file);
@@ -79,7 +78,7 @@ public function analyse(
7978
$errors = array_merge($errors, $this->fileAnalyser->analyseFile(
8079
$file,
8180
$this->registry,
82-
$outerNodeCallback
81+
$inferrablePropertyTypesFromConstructorHelper
8382
));
8483
} catch (\Throwable $t) {
8584
if ($debug) {
@@ -115,7 +114,7 @@ public function analyse(
115114
$errors[] = sprintf('Reached internal errors count limit of %d, exiting...', $this->internalErrorsCountLimit);
116115
}
117116

118-
return array_merge($errors, $ignoredErrorHelperResult->getWarnings());
117+
return new AnalyserResult(array_merge($errors, $ignoredErrorHelperResult->getWarnings()), $inferrablePropertyTypesFromConstructorHelper->hasInferrablePropertyTypesFromConstructor());
119118
}
120119

121120
/**

src/Analyser/AnalyserResult.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser;
4+
5+
class AnalyserResult
6+
{
7+
8+
/** @var string[]|\PHPStan\Analyser\Error[] */
9+
private $errors;
10+
11+
/** @var bool */
12+
private $hasInferrablePropertyTypesFromConstructor;
13+
14+
/**
15+
* @param string[]|\PHPStan\Analyser\Error[] $errors
16+
* @param bool $hasInferrablePropertyTypesFromConstructor
17+
*/
18+
public function __construct(
19+
array $errors,
20+
bool $hasInferrablePropertyTypesFromConstructor
21+
)
22+
{
23+
$this->errors = $errors;
24+
$this->hasInferrablePropertyTypesFromConstructor = $hasInferrablePropertyTypesFromConstructor;
25+
}
26+
27+
/**
28+
* @return string[]|\PHPStan\Analyser\Error[]
29+
*/
30+
public function getErrors(): array
31+
{
32+
return $this->errors;
33+
}
34+
35+
public function hasInferrablePropertyTypesFromConstructor(): bool
36+
{
37+
return $this->hasInferrablePropertyTypesFromConstructor;
38+
}
39+
40+
}

src/Command/AnalyseApplication.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PHPStan\Command;
44

55
use PHPStan\Analyser\Analyser;
6-
use PHPStan\Analyser\InferrablePropertyTypesFromConstructorHelper;
76
use PHPStan\Command\ErrorFormatter\ErrorFormatter;
87
use PHPStan\Parallel\ParallelAnalyser;
98
use PHPStan\Parallel\Scheduler;
@@ -137,19 +136,18 @@ public function analyse(
137136
) {
138137
$runningInParallel = true;
139138
$parallelAnalyserResult = $this->parallelAnalyser->analyse($schedule, $mainScript, $onlyFiles, $postFileCallback, $projectConfigFile, $input);
140-
$errors = array_merge($errors, $parallelAnalyserResult['errors']);
141-
$hasInferrablePropertyTypesFromConstructor = $parallelAnalyserResult['hasInferrablePropertyTypesFromConstructor'];
139+
$errors = array_merge($errors, $parallelAnalyserResult->getErrors());
140+
$hasInferrablePropertyTypesFromConstructor = $parallelAnalyserResult->hasInferrablePropertyTypesFromConstructor();
142141
} else {
143-
$inferrablePropertyTypesFromConstructorHelper = new InferrablePropertyTypesFromConstructorHelper();
144-
$errors = array_merge($errors, $this->analyser->analyse(
142+
$analyserResult = $this->analyser->analyse(
145143
$files,
146144
$onlyFiles,
147145
$preFileCallback,
148146
$postFileCallback,
149-
$debug,
150-
$inferrablePropertyTypesFromConstructorHelper
151-
));
152-
$hasInferrablePropertyTypesFromConstructor = $inferrablePropertyTypesFromConstructorHelper->hasInferrablePropertyTypesFromConstructor();
147+
$debug
148+
);
149+
$errors = array_merge($errors, $analyserResult->getErrors());
150+
$hasInferrablePropertyTypesFromConstructor = $analyserResult->hasInferrablePropertyTypesFromConstructor();
153151
}
154152

155153
if (isset($progressStarted) && $progressStarted) {

src/Parallel/ParallelAnalyser.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Clue\React\NDJson\Decoder;
66
use Clue\React\NDJson\Encoder;
77
use Nette\Utils\Random;
8+
use PHPStan\Analyser\AnalyserResult;
89
use PHPStan\Analyser\Error;
910
use PHPStan\Analyser\IgnoredErrorHelper;
1011
use PHPStan\Command\AnalyseCommand;
@@ -46,7 +47,7 @@ public function __construct(
4647
* @param bool $onlyFiles
4748
* @param \Closure(int): void|null $postFileCallback
4849
* @param string|null $projectConfigFile
49-
* @return array{errors: (string[]|\PHPStan\Analyser\Error[]), hasInferrablePropertyTypesFromConstructor: bool}
50+
* @return AnalyserResult
5051
*/
5152
public function analyse(
5253
Schedule $schedule,
@@ -55,14 +56,14 @@ public function analyse(
5556
?\Closure $postFileCallback,
5657
?string $projectConfigFile,
5758
InputInterface $input
58-
): array
59+
): AnalyserResult
5960
{
6061
$ignoredErrorHelperResult = $this->ignoredErrorHelper->initialize();
6162
if (count($ignoredErrorHelperResult->getErrors()) > 0) {
62-
return [
63-
'errors' => $ignoredErrorHelperResult->getErrors(),
64-
'hasInferrablePropertyTypesFromConstructor' => false,
65-
];
63+
return new AnalyserResult(
64+
$ignoredErrorHelperResult->getErrors(),
65+
false
66+
);
6667
}
6768

6869
$jobs = array_reverse($schedule->getJobs());
@@ -174,10 +175,10 @@ public function analyse(
174175
$internalErrors[] = sprintf('Reached internal errors count limit of %d, exiting...', $this->internalErrorsCountLimit);
175176
}
176177

177-
return [
178-
'errors' => array_merge($ignoredErrorHelperResult->process($errors, $onlyFiles, $reachedInternalErrorsCountLimit), $internalErrors, $ignoredErrorHelperResult->getWarnings()),
179-
'hasInferrablePropertyTypesFromConstructor' => $hasInferrablePropertyTypesFromConstructor,
180-
];
178+
return new AnalyserResult(
179+
array_merge($ignoredErrorHelperResult->process($errors, $onlyFiles, $reachedInternalErrorsCountLimit), $internalErrors, $ignoredErrorHelperResult->getWarnings()),
180+
$hasInferrablePropertyTypesFromConstructor
181+
);
181182
}
182183

183184
private function getWorkerCommand(

src/Testing/RuleTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ protected function getStaticMethodTypeSpecifyingExtensions(): array
123123
public function analyse(array $files, array $expectedErrors): void
124124
{
125125
$files = array_map([$this->getFileHelper(), 'normalizePath'], $files);
126-
$actualErrors = $this->getAnalyser()->analyse($files, false);
126+
$actualErrors = $this->getAnalyser()->analyse($files, false)->getErrors();
127127

128128
$strictlyTypedSprintf = static function (int $line, string $message, ?string $tip): string {
129129
$message = sprintf('%02d: %s', $line, $message);

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ private function runAnalyse(string $file): array
213213
/** @var \PHPStan\File\FileHelper $fileHelper */
214214
$fileHelper = self::getContainer()->getByType(FileHelper::class);
215215
/** @var \PHPStan\Analyser\Error[] $errors */
216-
$errors = $analyser->analyse([$file], false);
216+
$errors = $analyser->analyse([$file], false)->getErrors();
217217
foreach ($errors as $error) {
218218
$this->assertSame($fileHelper->normalizePath($file), $error->getFile());
219219
}

tests/PHPStan/Analyser/AnalyserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ private function runAnalyser(
332332

333333
return $analyser->analyse(array_map(function (string $path): string {
334334
return $this->getFileHelper()->normalizePath($path);
335-
}, $filePaths), $onlyFiles);
335+
}, $filePaths), $onlyFiles)->getErrors();
336336
}
337337

338338
/**

tests/PHPStan/Analyser/AnalyserTraitsIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ private function runAnalyse(array $files): array
175175
/** @var \PHPStan\Analyser\Analyser $analyser */
176176
$analyser = self::getContainer()->getByType(Analyser::class);
177177
/** @var \PHPStan\Analyser\Error[] $errors */
178-
$errors = $analyser->analyse($files, false);
178+
$errors = $analyser->analyse($files, false)->getErrors();
179179
return $errors;
180180
}
181181

0 commit comments

Comments
 (0)