Skip to content

Commit

Permalink
Ignored error regex with unescaped '||' is an unavoidable error
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 29, 2020
1 parent 7a1b030 commit 3b71993
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/Analyser/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ public function analyse(
if ($validationResult->hasAnchorsInTheMiddle()) {
$warnings[] = $this->createAnchorInTheMiddleWarning($ignoreMessage);
}

if ($validationResult->areAllErrorsIgnored()) {
$errors[] = sprintf("Ignored error %s has an unescaped '||' which leads to ignoring all errors. Use '\\|\\|' instead.", $ignoreMessage);
}
} else {
$otherIgnoreErrors[] = [
'index' => $i,
Expand All @@ -160,6 +164,10 @@ public function analyse(
if ($validationResult->hasAnchorsInTheMiddle()) {
$warnings[] = $this->createAnchorInTheMiddleWarning($ignoreMessage);
}

if ($validationResult->areAllErrorsIgnored()) {
$errors[] = sprintf("Ignored error %s has an unescaped '||' which leads to ignoring all errors. Use '\\|\\|' instead.", $ignoreMessage);
}
}

\Nette\Utils\Strings::match('', $ignoreMessage);
Expand Down
8 changes: 6 additions & 2 deletions src/Command/IgnoredRegexValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ public function validate(string $regex): IgnoredRegexValidatorResult
/** @var TreeNode $ast */
$ast = $this->parser->parse($regex);
} catch (\Hoa\Exception\Exception $e) {
return new IgnoredRegexValidatorResult([], false);
if (strpos($e->getMessage(), 'Unexpected token "|" (alternation) at line 1') === 0) {
return new IgnoredRegexValidatorResult([], false, true);
}
return new IgnoredRegexValidatorResult([], false, false);
}

return new IgnoredRegexValidatorResult(
$this->getIgnoredTypes($ast),
$this->hasAnchorsInTheMiddle($ast)
$this->hasAnchorsInTheMiddle($ast),
false
);
}

Expand Down
13 changes: 12 additions & 1 deletion src/Command/IgnoredRegexValidatorResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,23 @@ class IgnoredRegexValidatorResult
/** @var bool */
private $anchorsInTheMiddle;

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

/**
* @param array<string, string> $ignoredTypes
* @param bool $anchorsInTheMiddle
* @param bool $allErrorsIgnored
*/
public function __construct(
array $ignoredTypes,
bool $anchorsInTheMiddle
bool $anchorsInTheMiddle,
bool $allErrorsIgnored
)
{
$this->ignoredTypes = $ignoredTypes;
$this->anchorsInTheMiddle = $anchorsInTheMiddle;
$this->allErrorsIgnored = $allErrorsIgnored;
}

/**
Expand All @@ -37,4 +43,9 @@ public function hasAnchorsInTheMiddle(): bool
return $this->anchorsInTheMiddle;
}

public function areAllErrorsIgnored(): bool
{
return $this->allErrorsIgnored;
}

}
2 changes: 1 addition & 1 deletion src/Testing/RuleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private function getAnalyser(): Analyser
->willReturn([]);
$ignoredRegexValidator = $this->createMock(IgnoredRegexValidator::class);
$ignoredRegexValidator->method('validate')
->willReturn(new IgnoredRegexValidatorResult([], false));
->willReturn(new IgnoredRegexValidatorResult([], false, false));
$this->analyser = new Analyser(
$fileAnalyser,
$registry,
Expand Down
27 changes: 26 additions & 1 deletion tests/PHPStan/Command/IgnoredRegexValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ public function dataValidate(): array
'#^Call to function method_exists\\(\\) with ReflectionProperty and \'(?:hasType|getType)\' will always evaluate to true\\.$#iu',
[],
false,
false,
],
[
'#^Call to function method_exists\\(\\) with ReflectionProperty and \'(?:hasType|getType)\' will always evaluate to true\\.$#',
[],
false,
false,
],
[
'#Call to function method_exists\\(\\) with ReflectionProperty and \'(?:hasType|getType)\' will always evaluate to true\\.#',
[],
false,
false,
],
[
'#Parameter \#2 $destination of method Nette\\\\Application\\\\UI\\\\Component::redirect\(\) expects string|null, array|string|int given#',
Expand All @@ -34,44 +37,51 @@ public function dataValidate(): array
'int' => 'int given',
],
true,
false,
],
[
'#Parameter \#2 $destination of method Nette\\\\Application\\\\UI\\\\Component::redirect\(\) expects string|null, array|Foo|Bar given#',
[
'null' => 'null, array',
],
true,
false,
],
[
'#Parameter \#2 $destination of method Nette\\\\Application\\\\UI\\\\Component::redirect\(\) expects string\|null, array\|string\|int given#',
[],
true,
false,
],
[
'#Invalid array key type array|string\.#',
[
'string' => 'string\\.',
],
false,
false,
],
[
'#Invalid array key type array\|string\.#',
[],
false,
false,
],
[
'#Array (array<string>) does not accept key resource|iterable\.#',
[
'iterable' => 'iterable\.',
],
false,
false,
],
[
'#Parameter \#1 $i of method Levels\\\\AcceptTypes\\\\Foo::doBarArray\(\) expects array<int>, array<float|int> given.#',
[
'int' => 'int> given.',
],
true,
false,
],
[
'#Parameter \#1 \$i of method Levels\\\\AcceptTypes\\\\Foo::doBarArray\(\) expects array<int>|callable, array<float|int> given.#',
Expand All @@ -80,11 +90,19 @@ public function dataValidate(): array
'int' => 'int> given.',
],
false,
false,
],
[
'#Unclosed parenthesis(\)#',
[],
false,
false,
],
[
'~Result of || is always true.~',
[],
false,
true,
],
];
}
Expand All @@ -94,8 +112,14 @@ public function dataValidate(): array
* @param string $regex
* @param string[] $expectedTypes
* @param bool $expectedHasAnchors
* @param bool $expectAllErrorsIgnored
*/
public function testValidate(string $regex, array $expectedTypes, bool $expectedHasAnchors): void
public function testValidate(
string $regex,
array $expectedTypes,
bool $expectedHasAnchors,
bool $expectAllErrorsIgnored
): void
{
$grammar = new \Hoa\File\Read('hoa://Library/Regex/Grammar.pp');
$parser = \Hoa\Compiler\Llk\Llk::load($grammar);
Expand All @@ -104,6 +128,7 @@ public function testValidate(string $regex, array $expectedTypes, bool $expected
$result = $validator->validate($regex);
$this->assertSame($expectedTypes, $result->getIgnoredTypes());
$this->assertSame($expectedHasAnchors, $result->hasAnchorsInTheMiddle());
$this->assertSame($expectAllErrorsIgnored, $result->areAllErrorsIgnored());
}

}

0 comments on commit 3b71993

Please sign in to comment.