Skip to content

Commit

Permalink
Result cache contains all errors, filtering through ignoreErrors each…
Browse files Browse the repository at this point in the history
… time
  • Loading branch information
ondrejmirtes committed Mar 15, 2020
1 parent a90efd5 commit 9e10b4c
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 177 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ jobs:
uses: actions/cache@v1
with:
path: ./tmp
key: "result-cache-v2"
key: "result-cache-v3"

- name: "PHPStan with result cache"
if: matrix.operating-system == 'ubuntu-latest'
Expand Down
1 change: 1 addition & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ services:
class: PHPStan\Command\AnalyseApplication
arguments:
memoryLimitFile: %memoryLimitFile%
internalErrorsCountLimit: %internalErrorsCountLimit%

-
class: PHPStan\Command\IgnoredRegexValidator
Expand Down
22 changes: 4 additions & 18 deletions src/Analyser/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ class Analyser
/** @var \PHPStan\Analyser\NodeScopeResolver */
private $nodeScopeResolver;

/** @var IgnoredErrorHelper */
private $ignoredErrorHelper;

/** @var int */
private $internalErrorsCountLimit;

Expand All @@ -29,20 +26,17 @@ public function __construct(
FileAnalyser $fileAnalyser,
Registry $registry,
NodeScopeResolver $nodeScopeResolver,
IgnoredErrorHelper $ignoredErrorHelper,
int $internalErrorsCountLimit
)
{
$this->fileAnalyser = $fileAnalyser;
$this->registry = $registry;
$this->nodeScopeResolver = $nodeScopeResolver;
$this->ignoredErrorHelper = $ignoredErrorHelper;
$this->internalErrorsCountLimit = $internalErrorsCountLimit;
}

/**
* @param string[] $files
* @param bool $onlyFiles
* @param \Closure(string $file): void|null $preFileCallback
* @param \Closure(int): void|null $postFileCallback
* @param bool $debug
Expand All @@ -51,18 +45,12 @@ public function __construct(
*/
public function analyse(
array $files,
bool $onlyFiles,
?\Closure $preFileCallback = null,
?\Closure $postFileCallback = null,
bool $debug = false,
?array $allAnalysedFiles = null
): AnalyserResult
{
$ignoredErrorHelperResult = $this->ignoredErrorHelper->initialize();
if (count($ignoredErrorHelperResult->getErrors()) > 0) {
return new AnalyserResult($ignoredErrorHelperResult->getErrors(), false, null);
}

if ($allAnalysedFiles === null) {
$allAnalysedFiles = $files;
}
Expand Down Expand Up @@ -120,15 +108,13 @@ public function analyse(
$this->restoreCollectErrorsHandler();

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

return new AnalyserResult(
array_merge($errors, $ignoredErrorHelperResult->getWarnings()),
$errors,
[],
$inferrablePropertyTypesFromConstructorHelper->hasInferrablePropertyTypesFromConstructor(),
$internalErrorsCount === 0 ? $dependencies : null
$internalErrorsCount === 0 ? $dependencies : null,
$reachedInternalErrorsCountLimit
);
}

Expand Down
33 changes: 29 additions & 4 deletions src/Analyser/AnalyserResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,59 @@
class AnalyserResult
{

/** @var string[]|\PHPStan\Analyser\Error[] */
/** @var \PHPStan\Analyser\Error[] */
private $errors;

/** @var string[] */
private $internalErrors;

/** @var bool */
private $hasInferrablePropertyTypesFromConstructor;

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

/** @var bool */
private $reachedInternalErrorsCountLimit;

/**
* @param string[]|\PHPStan\Analyser\Error[] $errors
* @param \PHPStan\Analyser\Error[] $errors
* @param string[] $internalErrors
* @param bool $hasInferrablePropertyTypesFromConstructor
* @param array<string, array<string>>|null $dependencies
* @param bool $reachedInternalErrorsCountLimit
*/
public function __construct(
array $errors,
array $internalErrors,
bool $hasInferrablePropertyTypesFromConstructor,
?array $dependencies
?array $dependencies,
bool $reachedInternalErrorsCountLimit
)
{
$this->errors = $errors;
$this->internalErrors = $internalErrors;
$this->hasInferrablePropertyTypesFromConstructor = $hasInferrablePropertyTypesFromConstructor;
$this->dependencies = $dependencies;
$this->reachedInternalErrorsCountLimit = $reachedInternalErrorsCountLimit;
}

/**
* @return string[]|\PHPStan\Analyser\Error[]
* @return \PHPStan\Analyser\Error[]
*/
public function getErrors(): array
{
return $this->errors;
}

/**
* @return string[]
*/
public function getInternalErrors(): array
{
return $this->internalErrors;
}

public function hasInferrablePropertyTypesFromConstructor(): bool
{
return $this->hasInferrablePropertyTypesFromConstructor;
Expand All @@ -51,4 +71,9 @@ public function getDependencies(): ?array
return $this->dependencies;
}

public function hasReachedInternalErrorsCountLimit(): bool
{
return $this->reachedInternalErrorsCountLimit;
}

}
15 changes: 0 additions & 15 deletions src/Analyser/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ class Error implements \JsonSerializable
/** @var string|null */
private $tip;

/** @var bool */
private $warning;

/** @var int|null */
private $nodeLine;

Expand All @@ -51,7 +48,6 @@ class Error implements \JsonSerializable
* @param string|null $filePath
* @param string|null $traitFilePath
* @param string|null $tip
* @param bool $warning
* @param int|null $nodeLine
* @param class-string<\PhpParser\Node>|null $nodeType
* @param string|null $identifier
Expand All @@ -65,7 +61,6 @@ public function __construct(
?string $filePath = null,
?string $traitFilePath = null,
?string $tip = null,
bool $warning = false,
?int $nodeLine = null,
?string $nodeType = null,
?string $identifier = null,
Expand All @@ -79,7 +74,6 @@ public function __construct(
$this->filePath = $filePath;
$this->traitFilePath = $traitFilePath;
$this->tip = $tip;
$this->warning = $warning;
$this->nodeLine = $nodeLine;
$this->nodeType = $nodeType;
$this->identifier = $identifier;
Expand Down Expand Up @@ -139,17 +133,11 @@ public function withoutTip(): self
$this->filePath,
$this->traitFilePath,
null,
$this->warning,
$this->nodeLine,
$this->nodeType
);
}

public function isWarning(): bool
{
return $this->warning;
}

public function getNodeLine(): ?int
{
return $this->nodeLine;
Expand Down Expand Up @@ -189,7 +177,6 @@ public function jsonSerialize()
'filePath' => $this->filePath,
'traitFilePath' => $this->traitFilePath,
'tip' => $this->tip,
'warning' => $this->warning,
'nodeLine' => $this->nodeLine,
'nodeType' => $this->nodeType,
'identifier' => $this->identifier,
Expand All @@ -211,7 +198,6 @@ public static function decode(array $json): self
$json['filePath'],
$json['traitFilePath'],
$json['tip'],
$json['warning'],
$json['nodeLine'] ?? null,
$json['nodeType'] ?? null,
$json['identifier'] ?? null,
Expand All @@ -233,7 +219,6 @@ public static function __set_state(array $properties): self
$properties['filePath'],
$properties['traitFilePath'],
$properties['tip'],
$properties['warning'],
$properties['nodeLine'] ?? null,
$properties['nodeType'] ?? null,
$properties['identifier'] ?? null,
Expand Down
1 change: 0 additions & 1 deletion src/Analyser/FileAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ public function analyseFile(
$filePath,
$traitFilePath,
$tip,
false,
$nodeLine,
$nodeType,
$identifier,
Expand Down
37 changes: 13 additions & 24 deletions src/Analyser/IgnoredErrorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,36 +134,25 @@ public function initialize(): IgnoredErrorHelperResult
/**
* @param string $regex
* @param array<string, string> $ignoredTypes
* @return Error
* @return string
*/
private function createIgnoredTypesWarning(string $regex, array $ignoredTypes): Error
private function createIgnoredTypesWarning(string $regex, array $ignoredTypes): string
{
return new Error(
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 {
return sprintf('* %s', $typeDescription);
}, array_keys($ignoredTypes))))),
'placeholder', // this value will never get used
null,
false,
null,
null,
null,
true
return 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 {
return sprintf('* %s', $typeDescription);
}, array_keys($ignoredTypes)))
)
);
}

private function createAnchorInTheMiddleWarning(string $regex): Error
private function createAnchorInTheMiddleWarning(string $regex): string
{
return new Error(
sprintf("Ignored error %s has an unescaped anchor '$' in the middle. This leads to unintended behavior. Use '\\$' instead.", $regex),
'placeholder', // this value will never get used
null,
false,
null,
null,
null,
true
);
return sprintf("Ignored error %s has an unescaped anchor '$' in the middle. This leads to unintended behavior. Use '\\$' instead.", $regex);
}

}
6 changes: 3 additions & 3 deletions src/Analyser/IgnoredErrorHelperResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class IgnoredErrorHelperResult
/** @var string[] */
private $errors;

/** @var Error[] */
/** @var string[] */
private $warnings;

/** @var array<array<mixed>> */
Expand All @@ -31,7 +31,7 @@ class IgnoredErrorHelperResult
/**
* @param FileHelper $fileHelper
* @param string[] $errors
* @param Error[] $warnings
* @param string[] $warnings
* @param array<array<mixed>> $otherIgnoreErrors
* @param array<string, array<array<mixed>>> $ignoreErrorsByFile
* @param (string|mixed[])[] $ignoreErrors
Expand Down Expand Up @@ -65,7 +65,7 @@ public function getErrors(): array
}

/**
* @return Error[]
* @return string[]
*/
public function getWarnings(): array
{
Expand Down
29 changes: 9 additions & 20 deletions src/Analyser/ResultCache/ResultCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class ResultCacheManager
{

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

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

public function process(AnalyserResult $analyserResult, ResultCache $resultCache): AnalyserResult
{
$notFileSpecificErrors = [];
$internalErrors = $analyserResult->getInternalErrors();
$freshErrorsByFile = [];
$warnings = [];
foreach ($analyserResult->getErrors() as $error) {
if (is_string($error)) {
$notFileSpecificErrors[] = $error;
} else {
if ($error->isWarning()) {
$warnings[] = $error->getMessage();
continue;
}
$freshErrorsByFile[$error->getFilePath()][] = $error;
}
$freshErrorsByFile[$error->getFilePath()][] = $error;
}

$save = function (array $errorsByFile, ?array $dependencies) use ($notFileSpecificErrors, $warnings, $resultCache): void {
$save = function (array $errorsByFile, ?array $dependencies) use ($internalErrors, $resultCache): void {
if ($dependencies === null) {
return;
}

if (count($notFileSpecificErrors) > 0) {
return;
}

if (count($warnings) > 0) {
if (count($internalErrors) > 0) {
return;
}

Expand Down Expand Up @@ -219,9 +206,11 @@ public function process(AnalyserResult $analyserResult, ResultCache $resultCache
}

return new AnalyserResult(
array_merge($flatErrors, $warnings, $notFileSpecificErrors),
$flatErrors,
$internalErrors,
$analyserResult->hasInferrablePropertyTypesFromConstructor(),
$dependencies
$dependencies,
$analyserResult->hasReachedInternalErrorsCountLimit()
);
}

Expand Down

0 comments on commit 9e10b4c

Please sign in to comment.