Skip to content

Commit

Permalink
Show error on unescaped () in ignoreErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 14, 2020
1 parent 37afe12 commit 8479d40
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Analyser/IgnoredErrorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function initialize(): IgnoredErrorHelperResult
}

if ($validationResult->areAllErrorsIgnored()) {
$errors[] = sprintf("Ignored error %s has an unescaped '||' which leads to ignoring all errors. Use '\\|\\|' instead.", $ignoreMessage);
$errors[] = sprintf("Ignored error %s has an unescaped '%s' which leads to ignoring all errors. Use '%s' instead.", $ignoreMessage, $validationResult->getWrongSequence(), $validationResult->getEscapedWrongSequence());
}
} else {
$otherIgnoreErrors[] = [
Expand All @@ -117,7 +117,7 @@ public function initialize(): IgnoredErrorHelperResult
}

if ($validationResult->areAllErrorsIgnored()) {
$errors[] = sprintf("Ignored error %s has an unescaped '||' which leads to ignoring all errors. Use '\\|\\|' instead.", $ignoreMessage);
$errors[] = sprintf("Ignored error %s has an unescaped '%s' which leads to ignoring all errors. Use '%s' instead.", $ignoreMessage, $validationResult->getWrongSequence(), $validationResult->getEscapedWrongSequence());
}
}
} catch (\Nette\Utils\RegexpException $e) {
Expand Down
8 changes: 7 additions & 1 deletion src/Command/IgnoredRegexValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ public function validate(string $regex): IgnoredRegexValidatorResult
$ast = $this->parser->parse($regex);
} catch (\Hoa\Exception\Exception $e) {
if (strpos($e->getMessage(), 'Unexpected token "|" (alternation) at line 1') === 0) {
return new IgnoredRegexValidatorResult([], false, true);
return new IgnoredRegexValidatorResult([], false, true, '||', '\|\|');
}
if (
strpos($regex, '()') !== false
&& strpos($e->getMessage(), 'Unexpected token ")" (_capturing) at line 1') === 0
) {
return new IgnoredRegexValidatorResult([], false, true, '()', '\(\)');
}
return new IgnoredRegexValidatorResult([], false, false);
}
Expand Down
20 changes: 19 additions & 1 deletion src/Command/IgnoredRegexValidatorResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class IgnoredRegexValidatorResult

private bool $allErrorsIgnored;

private ?string $wrongSequence;

private ?string $escapedWrongSequence;

/**
* @param array<string, string> $ignoredTypes
* @param bool $anchorsInTheMiddle
Expand All @@ -20,12 +24,16 @@ class IgnoredRegexValidatorResult
public function __construct(
array $ignoredTypes,
bool $anchorsInTheMiddle,
bool $allErrorsIgnored
bool $allErrorsIgnored,
?string $wrongSequence = null,
?string $escapedWrongSequence = null
)
{
$this->ignoredTypes = $ignoredTypes;
$this->anchorsInTheMiddle = $anchorsInTheMiddle;
$this->allErrorsIgnored = $allErrorsIgnored;
$this->wrongSequence = $wrongSequence;
$this->escapedWrongSequence = $escapedWrongSequence;
}

/**
Expand All @@ -46,4 +54,14 @@ public function areAllErrorsIgnored(): bool
return $this->allErrorsIgnored;
}

public function getWrongSequence(): ?string
{
return $this->wrongSequence;
}

public function getEscapedWrongSequence(): ?string
{
return $this->escapedWrongSequence;
}

}
6 changes: 6 additions & 0 deletions tests/PHPStan/Command/IgnoredRegexValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ public function dataValidate(): array
false,
true,
],
[
'#Method PragmaRX\Notified\Data\Repositories\Notified::firstOrCreateByEvent() should return PragmaRX\Notified\Data\Models\Notified but returns Illuminate\Database\Eloquent\Model|null#',
[],
false,
true,
],
];
}

Expand Down

0 comments on commit 8479d40

Please sign in to comment.