Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions resources/RegexGrammar.pp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
alternation()

alternation:
concatenation() ( ::alternation:: concatenation() #alternation )*
concatenation()? ( <alternation> concatenation()? #alternation )*

concatenation:
( internal_options() | assertion() | quantification() | condition() )
Expand All @@ -154,8 +154,8 @@
<index>
| ::assertion_reference_:: alternation() #assertioncondition
)
::_capturing:: concatenation()?
( ::alternation:: concatenation()? )?
::_capturing::
alternation()
::_capturing::

assertion:
Expand All @@ -165,7 +165,8 @@
| ::lookbehind_:: #lookbehind
| ::negative_lookbehind_:: #negativelookbehind
)
alternation() ::_capturing::
alternation()
::_capturing::

quantification:
( class() | simple() ) ( quantifier() #quantification )?
Expand Down Expand Up @@ -208,7 +209,8 @@
| ::atomic_group_:: #atomicgroup
| ::capturing_::
)
alternation() ::_capturing::
alternation()
::_capturing::

non_capturing_internal_options:
<non_capturing_internal_option>
Expand Down
20 changes: 8 additions & 12 deletions src/Command/IgnoredRegexValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\VerbosityLevel;
use function count;
use function str_contains;
use function str_starts_with;
use function strrpos;
use function substr;

Expand All @@ -34,19 +32,17 @@ public function validate(string $regex): IgnoredRegexValidatorResult
try {
/** @var TreeNode $ast */
$ast = $this->parser->parse($regex);
} catch (Exception $e) {
if (str_starts_with($e->getMessage(), 'Unexpected token "|" (alternation) at line 1')) {
return new IgnoredRegexValidatorResult([], false, true, '||', '\|\|');
}
if (
str_contains($regex, '()')
&& str_starts_with($e->getMessage(), 'Unexpected token ")" (_capturing) at line 1')
) {
return new IgnoredRegexValidatorResult([], false, true, '()', '\(\)');
}
} catch (Exception) {
return new IgnoredRegexValidatorResult([], false, false);
}

if (Strings::match($regex, '~(?<!\\\\)(?:\\\\\\\\)*\|\|~')) {
return new IgnoredRegexValidatorResult([], false, true, '||', '\|\|');
}
if (Strings::match($regex, '~(?<!\\\\)(?:\\\\\\\\)*\(\)~')) {
return new IgnoredRegexValidatorResult([], false, true, '()', '\(\)');
}

return new IgnoredRegexValidatorResult(
$this->getIgnoredTypes($ast),
$this->hasAnchorsInTheMiddle($ast),
Expand Down
49 changes: 49 additions & 0 deletions src/Type/Regex/RegexGroupParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function array_values;
use function count;
use function in_array;
use function is_int;
Expand Down Expand Up @@ -84,6 +85,9 @@ public function parseGroups(string $regex): ?array
return null;
}

$this->updateAlternationAstRemoveVerticalBarsAndAddEmptyToken($ast);
$this->updateCapturingAstAddEmptyToken($ast);

$captureOnlyNamed = false;
if ($this->phpVersion->supportsPregCaptureOnlyNamedGroups()) {
$captureOnlyNamed = str_contains($modifiers, 'n');
Expand All @@ -104,6 +108,51 @@ public function parseGroups(string $regex): ?array
return [$astWalkResult->getCapturingGroups(), $astWalkResult->getMarkVerbs()];
}

private function createEmptyTokenTreeNode(TreeNode $parentAst): TreeNode
{
return new TreeNode('token', ['token' => 'literal', 'value' => '', 'namespace' => 'default'], [], $parentAst);
}

private function updateAlternationAstRemoveVerticalBarsAndAddEmptyToken(TreeNode $ast): void
{
$children = $ast->getChildren();

foreach ($children as $i => $child) {
$this->updateAlternationAstRemoveVerticalBarsAndAddEmptyToken($child);

if ($ast->getId() !== '#alternation' || $child->getValueToken() !== 'alternation') {
continue;
}

unset($children[$i]);

if ($i !== 0
&& isset($children[$i + 1])
&& $children[$i + 1]->getValueToken() !== 'alternation') {
continue;
}

$children[$i] = $this->createEmptyTokenTreeNode($ast);
}

$ast->setChildren(array_values($children));
}

private function updateCapturingAstAddEmptyToken(TreeNode $ast): void
{
foreach ($ast->getChildren() as $child) {
$this->updateCapturingAstAddEmptyToken($child);
}

if ($ast->getId() !== '#capturing' || $ast->getChildren() !== []) {
return;
}

$emptyAlternationAst = new TreeNode('#alternation', null, [], $ast);
$emptyAlternationAst->setChildren([$this->createEmptyTokenTreeNode($emptyAlternationAst)]);
$ast->setChildren([$emptyAlternationAst]);
}

private function walkRegexAst(
TreeNode $ast,
?RegexAlternation $alternation,
Expand Down
42 changes: 42 additions & 0 deletions tests/PHPStan/Analyser/nsrt/preg_match_shapes.php
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,48 @@ function bugUnescapedDashAfterRange (string $string): void
}
}

function bugEmptySubexpression (string $string): void {
if (preg_match('//', $string, $matches)) {
assertType("array{string}", $matches); // could be array{''}
}

if (preg_match('/()/', $string, $matches)) {
assertType("array{string, ''}", $matches); // could be array{'', ''}
}

if (preg_match('/|/', $string, $matches)) {
assertType("array{string}", $matches); // could be array{''}
}

if (preg_match('~|(a)~', $string, $matches)) {
assertType("array{0: string, 1?: 'a'}", $matches);
}

if (preg_match('~(a)|~', $string, $matches)) {
assertType("array{0: string, 1?: 'a'}", $matches);
}

if (preg_match('~(a)||(b)~', $string, $matches)) {
assertType("array{0: string, 1?: 'a'}|array{string, '', 'b'}", $matches);
}

if (preg_match('~(|(a))~', $string, $matches)) {
assertType("array{0: string, 1: ''|'a', 2?: 'a'}", $matches);
}

if (preg_match('~((a)|)~', $string, $matches)) {
assertType("array{0: string, 1: ''|'a', 2?: 'a'}", $matches);
}

if (preg_match('~((a)||(b))~', $string, $matches)) {
assertType("array{0: string, 1: ''|'a'|'b', 2?: ''|'a', 3?: 'b'}", $matches);
}

if (preg_match('~((a)|()|(b))~', $string, $matches)) {
assertType("array{0: string, 1: ''|'a'|'b', 2?: ''|'a', 3?: '', 4?: 'b'}", $matches);
}
}

function bug11744(string $string): void
{
if (!preg_match('~^((/[a-z]+)?)~', $string, $matches)) {
Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Command/IgnoredRegexValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,48 @@ public function dataValidate(): array
false,
false,
],
[
'~(a\()~',
[],
false,
false,
],
[
'~b\\\()~',
[],
false,
true,
],
[
'~(c\\\\\()~',
[],
false,
false,
],
[
'~Result of || is always true.~',
[],
false,
true,
],
[
'~a\||~',
[],
false,
false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a blocker, I can't merge this. This is the main reason why IgnoredRegexValidator exists at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ondrejmirtes if I understand the test correctly it was rejecting ...||... regex but the regex is valid, | is an alternation regex token and thus || means alternation with empty string which is fixed by this PR, would you mind amending the test and pushing to by branch directly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IgnoredRegexValidator makes sure that if people have this in their configuration:

parameters:
	ignoreErrors:
		- '~Result of || is always true.~'

It makes PHPStan fail with the following error:

Invalid entry in ignoreErrors:
Ignored error ~Result of || is always true.~ has an unescaped '||' which leads to ignoring all errors. Use '\|\|' instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but it MUST NOT fail, as it is valid regex :)

So please tell me how to make everyone happy.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a valid regex for ignoreErrors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe in December, after I release 2.0. Don't expect it sooner.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bugfix into 1.12.x, might I understand why so late?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I have different priorities and this is not a severe bug affecting a lot of users.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ondrejmirtes is now a better time?

About the usecase, see https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/v3.65.0/src/DocBlock/TypeExpression.php#L62 for example.

(a|b|) is shorter syntax of ((?:a|b|)?), the first one is currently now parsed by PHPStan and this PR fixes it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ondrejmirtes, please review, the feedback was addressed.

],
[
'~b\\\||~',
[],
false,
true,
],
[
'~c\\\\\||~',
[],
false,
false,
],
[
'#Method PragmaRX\Notified\Data\Repositories\Notified::firstOrCreateByEvent() should return PragmaRX\Notified\Data\Models\Notified but returns Illuminate\Database\Eloquent\Model|null#',
[],
Expand Down
Loading