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
39 changes: 26 additions & 13 deletions src/Type/Regex/RegexGroupParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public function parseGroups(string $regex): ?RegexAstWalkResult
$subjectAsGroupResult = $this->walkGroupAst(
$ast,
false,
false,
$modifiers,
RegexGroupWalkResult::createEmpty(),
);
Expand Down Expand Up @@ -408,7 +407,6 @@ private function createGroupType(TreeNode $group, bool $maybeConstant, string $p
$walkResult = $this->walkGroupAst(
$group,
false,
false,
$patternModifiers,
RegexGroupWalkResult::createEmpty(),
);
Expand Down Expand Up @@ -469,7 +467,6 @@ private function getRootAlternation(TreeNode $group): ?TreeNode

private function walkGroupAst(
TreeNode $ast,
bool $inAlternation,
bool $inClass,
string $patternModifiers,
RegexGroupWalkResult $walkResult,
Expand All @@ -491,7 +488,7 @@ private function walkGroupAst(

$meaningfulTokens++;

if (!$nonFalsy || $inAlternation) {
if (!$nonFalsy) {
continue;
}

Expand All @@ -504,7 +501,7 @@ private function walkGroupAst(
$walkResult = $walkResult->nonEmpty(TrinaryLogic::createYes());

// two non-empty tokens concatenated results in a non-falsy string
if ($meaningfulTokens > 1 && !$inAlternation) {
if ($meaningfulTokens > 1) {
$walkResult = $walkResult->nonFalsy(TrinaryLogic::createYes());
}
}
Expand All @@ -519,7 +516,7 @@ private function walkGroupAst(
if ($min >= 1) {
$walkResult = $walkResult->nonEmpty(TrinaryLogic::createYes());
}
if ($min >= 2 && !$inAlternation) {
if ($min >= 2) {
$walkResult = $walkResult->nonFalsy(TrinaryLogic::createYes());
}
}
Expand Down Expand Up @@ -559,30 +556,47 @@ private function walkGroupAst(
}

if ($ast->getId() === '#alternation') {
if (count($children) === 0) {
return $walkResult;
}

$newLiterals = [];
$nonEmpty = TrinaryLogic::createYes();
$nonFalsy = TrinaryLogic::createYes();
$numeric = TrinaryLogic::createYes();
foreach ($children as $child) {
Comment thread
staabm marked this conversation as resolved.
$walkResult = $this->walkGroupAst(
$childResult = $this->walkGroupAst(
$child,
true,
$inClass,
$patternModifiers,
$walkResult->onlyLiterals([]),
$walkResult->onlyLiterals([])
->nonEmpty(TrinaryLogic::createMaybe())
->nonFalsy(TrinaryLogic::createMaybe())
->numeric(TrinaryLogic::createMaybe()),
);

$nonEmpty = $nonEmpty->and($childResult->isNonEmpty());
$nonFalsy = $nonFalsy->and($childResult->isNonFalsy());
$numeric = $numeric->and($childResult->isNumeric());

if ($newLiterals === null) {
continue;
}

if (count($walkResult->getOnlyLiterals() ?? []) > 0) {
foreach ($walkResult->getOnlyLiterals() as $alternationLiterals) {
if (count($childResult->getOnlyLiterals() ?? []) > 0) {
foreach ($childResult->getOnlyLiterals() as $alternationLiterals) {
$newLiterals[] = $alternationLiterals;
}
} else {
$newLiterals = null;
}
}

return $walkResult->onlyLiterals($newLiterals);
return $walkResult
->onlyLiterals($newLiterals)
->nonEmpty($walkResult->isNonEmpty()->or($nonEmpty))
->nonFalsy($walkResult->isNonFalsy()->or($nonFalsy))
->numeric($walkResult->isNumeric()->and($numeric));
}

// [^0-9] should not parse as numeric-string, and [^list-everything-but-numbers] is technically
Expand All @@ -595,7 +609,6 @@ private function walkGroupAst(
foreach ($children as $child) {
$walkResult = $this->walkGroupAst(
$child,
$inAlternation,
$inClass,
$patternModifiers,
$walkResult,
Expand Down
8 changes: 4 additions & 4 deletions tests/PHPStan/Analyser/nsrt/bug-12210.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@

function bug12210a(string $text): void {
assert(preg_match('(((sum|min|max)))', $text, $match) === 1);
assertType("array{non-empty-string, 'max'|'min'|'sum', 'max'|'min'|'sum'}", $match);
assertType("array{non-falsy-string, 'max'|'min'|'sum', 'max'|'min'|'sum'}", $match);
}

function bug12210b(string $text): void {
assert(preg_match('(((sum|min|ma.)))', $text, $match) === 1);
assertType("array{non-empty-string, non-empty-string, non-falsy-string}", $match);
assertType("array{non-falsy-string, non-falsy-string, non-falsy-string}", $match);
}

function bug12210c(string $text): void {
assert(preg_match('(((su.|min|max)))', $text, $match) === 1);
assertType("array{non-empty-string, non-empty-string, non-falsy-string}", $match);
assertType("array{non-falsy-string, non-falsy-string, non-falsy-string}", $match);
}

function bug12210d(string $text): void {
assert(preg_match('(((sum|mi.|max)))', $text, $match) === 1);
assertType("array{non-empty-string, non-empty-string, non-falsy-string}", $match);
assertType("array{non-falsy-string, non-falsy-string, non-falsy-string}", $match);
}
87 changes: 87 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14575.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php declare(strict_types = 1);

namespace Bug14575;

use function PHPStan\Testing\assertType;

function doFoo(string $string): void {
// Anchors in alternation should not make the match non-empty
if (preg_match('(foo|$)', $string, $match)) {
assertType('array{string}', $match);
}

if (preg_match('(^|foo)', $string, $match)) {
assertType('array{string}', $match);
}

if (preg_match('(\b|foo)', $string, $match)) {
assertType('array{string}', $match);
}

if (preg_match('/foo|$/', $string, $match)) {
assertType('array{string}', $match);
}

if (preg_match('/^|bar/', $string, $match)) {
assertType('array{string}', $match);
}

// Anchor in alternation within capturing group
if (preg_match('/(foo|$)/', $string, $match)) {
assertType('array{string, string}', $match);
}

if (preg_match('/(^|bar)/', $string, $match)) {
assertType('array{string, string}', $match);
}

// Anchor in alternation does not affect parent concatenation
if (preg_match('/^abc(def|$)/', $string, $match)) {
assertType("array{non-falsy-string, string}", $match);
}

// All non-empty alternatives should still produce non-empty/non-falsy
if (preg_match('/foo|bar/', $string, $match)) {
assertType('array{non-falsy-string}', $match);
}

if (preg_match('/foo/', $string, $match)) {
assertType('array{non-falsy-string}', $match);
}

// Anchor alone
if (preg_match('/^$/', $string, $match)) {
assertType('array{string}', $match);
}

// Three-way alternation with anchor
if (preg_match('/foo|bar|$/', $string, $match)) {
assertType('array{string}', $match);
}

// All alternatives are single chars (non-falsy not determinable from single tokens)
if (preg_match('/a|b/', $string, $match)) {
assertType('array{non-empty-string}', $match);
}

// Empty alternation branches
if (preg_match('/(|)/', $string, $match)) {
assertType("array{string, ''}", $match);
}

if (preg_match('/(foo|)/', $string, $match)) {
assertType("array{string, ''|'foo'}", $match);
}

if (preg_match('/(|bar)/', $string, $match)) {
assertType("array{string, ''|'bar'}", $match);
}

if (preg_match('/|/', $string, $match)) {
assertType('array{string}', $match);
}

if (preg_match('/foo|/', $string, $match)) {
assertType('array{string}', $match);
}
}
10 changes: 5 additions & 5 deletions tests/PHPStan/Analyser/nsrt/preg_match_shapes.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ function (string $size): void {
if (preg_match('~\{(?:(include)\\s+(?:[$]?\\w+(?<!file))\\s)|(?:(include\\s+file)\\s+(?:[$]?\\w+)\\s)|(?:(include(?:Template|(?:\\s+file)))\\s+(?:\'?.*?\.latte\'?)\\s)~', $size, $matches) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType("array{non-empty-string, '', '', non-falsy-string}|array{non-empty-string, '', non-falsy-string}|array{non-empty-string, 'include'}", $matches);
assertType("array{non-falsy-string, '', '', non-falsy-string}|array{non-falsy-string, '', non-falsy-string}|array{non-falsy-string, 'include'}", $matches);
};


Expand Down Expand Up @@ -519,7 +519,7 @@ function bug11323(string $s): void {
assertType('array{non-falsy-string, non-empty-string, "\n", "\n"}', $matches);
}
if (preg_match('/foo(*:first)|bar(*:second)([x])/', $s, $matches)) {
assertType("array{0: non-empty-string, 1?: 'x', MARK?: 'first'|'second'}", $matches);
assertType("array{0: non-falsy-string, 1?: 'x', MARK?: 'first'|'second'}", $matches);
}
}

Expand Down Expand Up @@ -762,13 +762,13 @@ function bug11604 (string $string): void {
return;
}

assertType("list{0: non-empty-string, 1?: ''|'XX', 2?: 'YY'}", $matches);
assertType("list{0: non-falsy-string, 1?: ''|'XX', 2?: 'YY'}", $matches);
// could be array{string, '', 'YY'}|array{string, 'XX'}|array{string}
}

function bug11604b (string $string): void {
if (preg_match('/(XX)|(YY)?(ZZ)/', $string, $matches)) {
assertType("list{0: non-empty-string, 1?: ''|'XX', 2?: ''|'YY', 3?: 'ZZ'}", $matches);
assertType("list{0: non-falsy-string, 1?: ''|'XX', 2?: ''|'YY', 3?: 'ZZ'}", $matches);
}
}

Expand Down Expand Up @@ -890,7 +890,7 @@ function testEscapedDelimiter (string $string): void {
if (preg_match(<<<'EOD'
{(x\\\{)|(y\\\\\})}
EOD, $string, $matches)) {
assertType("array{non-empty-string, '', 'y\\\\\\\}'}|array{non-empty-string, 'x\\\{'}", $matches);
assertType("array{non-falsy-string, '', 'y\\\\\\\}'}|array{non-falsy-string, 'x\\\{'}", $matches);
}
}

Expand Down
Loading