Skip to content

Commit 9e10b4c

Browse files
committed
Result cache contains all errors, filtering through ignoreErrors each time
1 parent a90efd5 commit 9e10b4c

15 files changed

Lines changed: 139 additions & 177 deletions

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ jobs:
310310
uses: actions/cache@v1
311311
with:
312312
path: ./tmp
313-
key: "result-cache-v2"
313+
key: "result-cache-v3"
314314

315315
- name: "PHPStan with result cache"
316316
if: matrix.operating-system == 'ubuntu-latest'

conf/config.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ services:
347347
class: PHPStan\Command\AnalyseApplication
348348
arguments:
349349
memoryLimitFile: %memoryLimitFile%
350+
internalErrorsCountLimit: %internalErrorsCountLimit%
350351

351352
-
352353
class: PHPStan\Command\IgnoredRegexValidator

src/Analyser/Analyser.php

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ class Analyser
1616
/** @var \PHPStan\Analyser\NodeScopeResolver */
1717
private $nodeScopeResolver;
1818

19-
/** @var IgnoredErrorHelper */
20-
private $ignoredErrorHelper;
21-
2219
/** @var int */
2320
private $internalErrorsCountLimit;
2421

@@ -29,20 +26,17 @@ public function __construct(
2926
FileAnalyser $fileAnalyser,
3027
Registry $registry,
3128
NodeScopeResolver $nodeScopeResolver,
32-
IgnoredErrorHelper $ignoredErrorHelper,
3329
int $internalErrorsCountLimit
3430
)
3531
{
3632
$this->fileAnalyser = $fileAnalyser;
3733
$this->registry = $registry;
3834
$this->nodeScopeResolver = $nodeScopeResolver;
39-
$this->ignoredErrorHelper = $ignoredErrorHelper;
4035
$this->internalErrorsCountLimit = $internalErrorsCountLimit;
4136
}
4237

4338
/**
4439
* @param string[] $files
45-
* @param bool $onlyFiles
4640
* @param \Closure(string $file): void|null $preFileCallback
4741
* @param \Closure(int): void|null $postFileCallback
4842
* @param bool $debug
@@ -51,18 +45,12 @@ public function __construct(
5145
*/
5246
public function analyse(
5347
array $files,
54-
bool $onlyFiles,
5548
?\Closure $preFileCallback = null,
5649
?\Closure $postFileCallback = null,
5750
bool $debug = false,
5851
?array $allAnalysedFiles = null
5952
): AnalyserResult
6053
{
61-
$ignoredErrorHelperResult = $this->ignoredErrorHelper->initialize();
62-
if (count($ignoredErrorHelperResult->getErrors()) > 0) {
63-
return new AnalyserResult($ignoredErrorHelperResult->getErrors(), false, null);
64-
}
65-
6654
if ($allAnalysedFiles === null) {
6755
$allAnalysedFiles = $files;
6856
}
@@ -120,15 +108,13 @@ public function analyse(
120108
$this->restoreCollectErrorsHandler();
121109

122110
$errors = array_merge($errors, $this->collectedErrors);
123-
$errors = $ignoredErrorHelperResult->process($errors, $onlyFiles, $reachedInternalErrorsCountLimit);
124-
if ($reachedInternalErrorsCountLimit) {
125-
$errors[] = sprintf('Reached internal errors count limit of %d, exiting...', $this->internalErrorsCountLimit);
126-
}
127111

128112
return new AnalyserResult(
129-
array_merge($errors, $ignoredErrorHelperResult->getWarnings()),
113+
$errors,
114+
[],
130115
$inferrablePropertyTypesFromConstructorHelper->hasInferrablePropertyTypesFromConstructor(),
131-
$internalErrorsCount === 0 ? $dependencies : null
116+
$internalErrorsCount === 0 ? $dependencies : null,
117+
$reachedInternalErrorsCountLimit
132118
);
133119
}
134120

src/Analyser/AnalyserResult.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,59 @@
55
class AnalyserResult
66
{
77

8-
/** @var string[]|\PHPStan\Analyser\Error[] */
8+
/** @var \PHPStan\Analyser\Error[] */
99
private $errors;
1010

11+
/** @var string[] */
12+
private $internalErrors;
13+
1114
/** @var bool */
1215
private $hasInferrablePropertyTypesFromConstructor;
1316

1417
/** @var array<string, array<string>>|null */
1518
private $dependencies;
1619

20+
/** @var bool */
21+
private $reachedInternalErrorsCountLimit;
22+
1723
/**
18-
* @param string[]|\PHPStan\Analyser\Error[] $errors
24+
* @param \PHPStan\Analyser\Error[] $errors
25+
* @param string[] $internalErrors
1926
* @param bool $hasInferrablePropertyTypesFromConstructor
2027
* @param array<string, array<string>>|null $dependencies
28+
* @param bool $reachedInternalErrorsCountLimit
2129
*/
2230
public function __construct(
2331
array $errors,
32+
array $internalErrors,
2433
bool $hasInferrablePropertyTypesFromConstructor,
25-
?array $dependencies
34+
?array $dependencies,
35+
bool $reachedInternalErrorsCountLimit
2636
)
2737
{
2838
$this->errors = $errors;
39+
$this->internalErrors = $internalErrors;
2940
$this->hasInferrablePropertyTypesFromConstructor = $hasInferrablePropertyTypesFromConstructor;
3041
$this->dependencies = $dependencies;
42+
$this->reachedInternalErrorsCountLimit = $reachedInternalErrorsCountLimit;
3143
}
3244

3345
/**
34-
* @return string[]|\PHPStan\Analyser\Error[]
46+
* @return \PHPStan\Analyser\Error[]
3547
*/
3648
public function getErrors(): array
3749
{
3850
return $this->errors;
3951
}
4052

53+
/**
54+
* @return string[]
55+
*/
56+
public function getInternalErrors(): array
57+
{
58+
return $this->internalErrors;
59+
}
60+
4161
public function hasInferrablePropertyTypesFromConstructor(): bool
4262
{
4363
return $this->hasInferrablePropertyTypesFromConstructor;
@@ -51,4 +71,9 @@ public function getDependencies(): ?array
5171
return $this->dependencies;
5272
}
5373

74+
public function hasReachedInternalErrorsCountLimit(): bool
75+
{
76+
return $this->reachedInternalErrorsCountLimit;
77+
}
78+
5479
}

src/Analyser/Error.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ class Error implements \JsonSerializable
2626
/** @var string|null */
2727
private $tip;
2828

29-
/** @var bool */
30-
private $warning;
31-
3229
/** @var int|null */
3330
private $nodeLine;
3431

@@ -51,7 +48,6 @@ class Error implements \JsonSerializable
5148
* @param string|null $filePath
5249
* @param string|null $traitFilePath
5350
* @param string|null $tip
54-
* @param bool $warning
5551
* @param int|null $nodeLine
5652
* @param class-string<\PhpParser\Node>|null $nodeType
5753
* @param string|null $identifier
@@ -65,7 +61,6 @@ public function __construct(
6561
?string $filePath = null,
6662
?string $traitFilePath = null,
6763
?string $tip = null,
68-
bool $warning = false,
6964
?int $nodeLine = null,
7065
?string $nodeType = null,
7166
?string $identifier = null,
@@ -79,7 +74,6 @@ public function __construct(
7974
$this->filePath = $filePath;
8075
$this->traitFilePath = $traitFilePath;
8176
$this->tip = $tip;
82-
$this->warning = $warning;
8377
$this->nodeLine = $nodeLine;
8478
$this->nodeType = $nodeType;
8579
$this->identifier = $identifier;
@@ -139,17 +133,11 @@ public function withoutTip(): self
139133
$this->filePath,
140134
$this->traitFilePath,
141135
null,
142-
$this->warning,
143136
$this->nodeLine,
144137
$this->nodeType
145138
);
146139
}
147140

148-
public function isWarning(): bool
149-
{
150-
return $this->warning;
151-
}
152-
153141
public function getNodeLine(): ?int
154142
{
155143
return $this->nodeLine;
@@ -189,7 +177,6 @@ public function jsonSerialize()
189177
'filePath' => $this->filePath,
190178
'traitFilePath' => $this->traitFilePath,
191179
'tip' => $this->tip,
192-
'warning' => $this->warning,
193180
'nodeLine' => $this->nodeLine,
194181
'nodeType' => $this->nodeType,
195182
'identifier' => $this->identifier,
@@ -211,7 +198,6 @@ public static function decode(array $json): self
211198
$json['filePath'],
212199
$json['traitFilePath'],
213200
$json['tip'],
214-
$json['warning'],
215201
$json['nodeLine'] ?? null,
216202
$json['nodeType'] ?? null,
217203
$json['identifier'] ?? null,
@@ -233,7 +219,6 @@ public static function __set_state(array $properties): self
233219
$properties['filePath'],
234220
$properties['traitFilePath'],
235221
$properties['tip'],
236-
$properties['warning'],
237222
$properties['nodeLine'] ?? null,
238223
$properties['nodeType'] ?? null,
239224
$properties['identifier'] ?? null,

src/Analyser/FileAnalyser.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ public function analyseFile(
143143
$filePath,
144144
$traitFilePath,
145145
$tip,
146-
false,
147146
$nodeLine,
148147
$nodeType,
149148
$identifier,

src/Analyser/IgnoredErrorHelper.php

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -134,36 +134,25 @@ public function initialize(): IgnoredErrorHelperResult
134134
/**
135135
* @param string $regex
136136
* @param array<string, string> $ignoredTypes
137-
* @return Error
137+
* @return string
138138
*/
139-
private function createIgnoredTypesWarning(string $regex, array $ignoredTypes): Error
139+
private function createIgnoredTypesWarning(string $regex, array $ignoredTypes): string
140140
{
141-
return new Error(
142-
sprintf("Ignored error %s has an unescaped '|' which leads to ignoring more errors than intended. Use '\\|' instead.\n%s", $regex, sprintf("It ignores all errors containing the following types:\n%s", implode("\n", array_map(static function (string $typeDescription): string {
143-
return sprintf('* %s', $typeDescription);
144-
}, array_keys($ignoredTypes))))),
145-
'placeholder', // this value will never get used
146-
null,
147-
false,
148-
null,
149-
null,
150-
null,
151-
true
141+
return sprintf(
142+
"Ignored error %s has an unescaped '|' which leads to ignoring more errors than intended. Use '\\|' instead.\n%s",
143+
$regex,
144+
sprintf(
145+
"It ignores all errors containing the following types:\n%s",
146+
implode("\n", array_map(static function (string $typeDescription): string {
147+
return sprintf('* %s', $typeDescription);
148+
}, array_keys($ignoredTypes)))
149+
)
152150
);
153151
}
154152

155-
private function createAnchorInTheMiddleWarning(string $regex): Error
153+
private function createAnchorInTheMiddleWarning(string $regex): string
156154
{
157-
return new Error(
158-
sprintf("Ignored error %s has an unescaped anchor '$' in the middle. This leads to unintended behavior. Use '\\$' instead.", $regex),
159-
'placeholder', // this value will never get used
160-
null,
161-
false,
162-
null,
163-
null,
164-
null,
165-
true
166-
);
155+
return sprintf("Ignored error %s has an unescaped anchor '$' in the middle. This leads to unintended behavior. Use '\\$' instead.", $regex);
167156
}
168157

169158
}

src/Analyser/IgnoredErrorHelperResult.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class IgnoredErrorHelperResult
1313
/** @var string[] */
1414
private $errors;
1515

16-
/** @var Error[] */
16+
/** @var string[] */
1717
private $warnings;
1818

1919
/** @var array<array<mixed>> */
@@ -31,7 +31,7 @@ class IgnoredErrorHelperResult
3131
/**
3232
* @param FileHelper $fileHelper
3333
* @param string[] $errors
34-
* @param Error[] $warnings
34+
* @param string[] $warnings
3535
* @param array<array<mixed>> $otherIgnoreErrors
3636
* @param array<string, array<array<mixed>>> $ignoreErrorsByFile
3737
* @param (string|mixed[])[] $ignoreErrors
@@ -65,7 +65,7 @@ public function getErrors(): array
6565
}
6666

6767
/**
68-
* @return Error[]
68+
* @return string[]
6969
*/
7070
public function getWarnings(): array
7171
{

src/Analyser/ResultCache/ResultCacheManager.php

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class ResultCacheManager
1212
{
1313

14-
private const CACHE_VERSION = 'v2-hashes';
14+
private const CACHE_VERSION = 'v3-all-errors';
1515

1616
/** @var string */
1717
private $cacheFilePath;
@@ -159,31 +159,18 @@ public function restore(array $allAnalysedFiles, bool $debug): ResultCache
159159

160160
public function process(AnalyserResult $analyserResult, ResultCache $resultCache): AnalyserResult
161161
{
162-
$notFileSpecificErrors = [];
162+
$internalErrors = $analyserResult->getInternalErrors();
163163
$freshErrorsByFile = [];
164-
$warnings = [];
165164
foreach ($analyserResult->getErrors() as $error) {
166-
if (is_string($error)) {
167-
$notFileSpecificErrors[] = $error;
168-
} else {
169-
if ($error->isWarning()) {
170-
$warnings[] = $error->getMessage();
171-
continue;
172-
}
173-
$freshErrorsByFile[$error->getFilePath()][] = $error;
174-
}
165+
$freshErrorsByFile[$error->getFilePath()][] = $error;
175166
}
176167

177-
$save = function (array $errorsByFile, ?array $dependencies) use ($notFileSpecificErrors, $warnings, $resultCache): void {
168+
$save = function (array $errorsByFile, ?array $dependencies) use ($internalErrors, $resultCache): void {
178169
if ($dependencies === null) {
179170
return;
180171
}
181172

182-
if (count($notFileSpecificErrors) > 0) {
183-
return;
184-
}
185-
186-
if (count($warnings) > 0) {
173+
if (count($internalErrors) > 0) {
187174
return;
188175
}
189176

@@ -219,9 +206,11 @@ public function process(AnalyserResult $analyserResult, ResultCache $resultCache
219206
}
220207

221208
return new AnalyserResult(
222-
array_merge($flatErrors, $warnings, $notFileSpecificErrors),
209+
$flatErrors,
210+
$internalErrors,
223211
$analyserResult->hasInferrablePropertyTypesFromConstructor(),
224-
$dependencies
212+
$dependencies,
213+
$analyserResult->hasReachedInternalErrorsCountLimit()
225214
);
226215
}
227216

0 commit comments

Comments
 (0)