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
28 changes: 28 additions & 0 deletions src/Type/Php/RegexGroupParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\IntersectionType;
Expand Down Expand Up @@ -295,13 +296,16 @@ private function getQuantificationRange(TreeNode $node): array
private function createGroupType(TreeNode $group, bool $maybeConstant): Type
{
$isNonEmpty = TrinaryLogic::createMaybe();
$isNonFalsy = TrinaryLogic::createMaybe();
$isNumeric = TrinaryLogic::createMaybe();
$inOptionalQuantification = false;
$onlyLiterals = [];

$this->walkGroupAst(
$group,
false,
$isNonEmpty,
$isNonFalsy,
$isNumeric,
$inOptionalQuantification,
$onlyLiterals,
Expand All @@ -318,11 +322,21 @@ private function createGroupType(TreeNode $group, bool $maybeConstant): Type
}

if ($isNumeric->yes()) {
if ($isNonFalsy->yes()) {
return new IntersectionType([
new StringType(),
new AccessoryNumericStringType(),
new AccessoryNonFalsyStringType(),
]);
}

$result = new IntersectionType([new StringType(), new AccessoryNumericStringType()]);
if (!$isNonEmpty->yes()) {
return TypeCombinator::union(new ConstantStringType(''), $result);
}
return $result;
} elseif ($isNonFalsy->yes()) {
return new IntersectionType([new StringType(), new AccessoryNonFalsyStringType()]);
} elseif ($isNonEmpty->yes()) {
return new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()]);
}
Expand All @@ -335,7 +349,9 @@ private function createGroupType(TreeNode $group, bool $maybeConstant): Type
*/
private function walkGroupAst(
TreeNode $ast,
bool $inAlternation,
TrinaryLogic &$isNonEmpty,
TrinaryLogic &$isNonFalsy,
TrinaryLogic &$isNumeric,
bool &$inOptionalQuantification,
?array &$onlyLiterals,
Expand All @@ -349,6 +365,9 @@ private function walkGroupAst(
&& count($children) > 0
) {
$isNonEmpty = TrinaryLogic::createYes();
if (!$inAlternation) {
$isNonFalsy = TrinaryLogic::createYes();
}
} elseif ($ast->getId() === '#quantification') {
[$min] = $this->getQuantificationRange($ast);

Expand All @@ -359,6 +378,9 @@ private function walkGroupAst(
$isNonEmpty = TrinaryLogic::createYes();
$inOptionalQuantification = false;
}
if ($min >= 2 && !$inAlternation) {
$isNonFalsy = TrinaryLogic::createYes();
}

$onlyLiterals = null;
} elseif ($ast->getId() === '#class' && $onlyLiterals !== null) {
Expand Down Expand Up @@ -396,6 +418,10 @@ private function walkGroupAst(
$onlyLiterals = null;
}

if ($ast->getId() === '#alternation') {
$inAlternation = true;
}

// [^0-9] should not parse as numeric-string, and [^list-everything-but-numbers] is technically
// doable but really silly compared to just \d so we can safely assume the string is not numeric
// for negative classes
Expand All @@ -406,7 +432,9 @@ private function walkGroupAst(
foreach ($children as $child) {
$this->walkGroupAst(
$child,
$inAlternation,
$isNonEmpty,
$isNonFalsy,
$isNumeric,
$inOptionalQuantification,
$onlyLiterals,
Expand Down
12 changes: 6 additions & 6 deletions tests/PHPStan/Analyser/nsrt/bug-11311.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ function (string $size): void {
if (preg_match('/a(\dAB){2}b(\d){2,4}([1-5])([1-5a-z])e?/', $size, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{string, non-empty-string, numeric-string, numeric-string, non-empty-string}', $matches);
assertType('array{string, non-falsy-string, numeric-string, numeric-string, non-empty-string}', $matches);
};

function (string $size): void {
if (preg_match('/ab(ab(\d)){2,4}xx([0-9][a-c])?e?/', $size, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{string, non-empty-string, numeric-string, non-empty-string|null}', $matches);
assertType('array{string, non-falsy-string, numeric-string, non-falsy-string|null}', $matches);
};

function (string $size): void {
Expand All @@ -120,14 +120,14 @@ function (string $size): void {
if (preg_match('/ab(\d\d)/', $size, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{string, numeric-string}', $matches);
assertType('array{string, non-falsy-string&numeric-string}', $matches);
};

function (string $size): void {
if (preg_match('/ab(\d+\s)e?/', $size, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{string, non-empty-string}', $matches);
assertType('array{string, non-falsy-string}', $matches);
};

function (string $size): void {
Expand Down Expand Up @@ -162,7 +162,7 @@ function (string $size): void {
if (preg_match('/ab(\d+\d?)e?/', $size, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{string, numeric-string}', $matches);
assertType('array{string, non-falsy-string&numeric-string}', $matches);
};

function (string $s): void {
Expand All @@ -179,7 +179,7 @@ function (string $s): void {

function (string $s): void {
if (preg_match('/^%([0-9]*\$)?[0-9]*\.?[0-9]*([sbdeEfFgGhHouxX])$/', $s, $matches, PREG_UNMATCHED_AS_NULL) === 1) {
assertType("array{string, non-empty-string|null, 'b'|'d'|'E'|'e'|'F'|'f'|'G'|'g'|'H'|'h'|'o'|'s'|'u'|'X'|'x'}", $matches);
assertType("array{string, non-falsy-string|null, 'b'|'d'|'E'|'e'|'F'|'f'|'G'|'g'|'H'|'h'|'o'|'s'|'u'|'X'|'x'}", $matches);
}
};

Expand Down
95 changes: 57 additions & 38 deletions tests/PHPStan/Analyser/nsrt/preg_match_shapes.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,34 @@ function doMatch(string $s): void {
assertType('array{}|array{0: string, 1?: non-empty-string}', $matches);

if (preg_match('/(foo)(bar)(baz)+/', $s, $matches)) {
assertType("array{string, 'foo', 'bar', non-empty-string}", $matches);
assertType("array{string, 'foo', 'bar', non-falsy-string}", $matches);
}
assertType("array{}|array{string, 'foo', 'bar', non-empty-string}", $matches);
assertType("array{}|array{string, 'foo', 'bar', non-falsy-string}", $matches);

if (preg_match('/(foo)(bar)(baz)*/', $s, $matches)) {
assertType("array{0: string, 1: 'foo', 2: 'bar', 3?: non-empty-string}", $matches);
assertType("array{0: string, 1: 'foo', 2: 'bar', 3?: non-falsy-string}", $matches);
}
assertType("array{}|array{0: string, 1: 'foo', 2: 'bar', 3?: non-empty-string}", $matches);
assertType("array{}|array{0: string, 1: 'foo', 2: 'bar', 3?: non-falsy-string}", $matches);

if (preg_match('/(foo)(bar)(baz)?/', $s, $matches)) {
assertType("array{0: string, 1: 'foo', 2: 'bar', 3?: 'baz'}", $matches);
}
assertType("array{}|array{0: string, 1: 'foo', 2: 'bar', 3?: 'baz'}", $matches);

if (preg_match('/(foo)(bar)(baz){0,3}/', $s, $matches)) {
assertType("array{0: string, 1: 'foo', 2: 'bar', 3?: non-empty-string}", $matches);
assertType("array{0: string, 1: 'foo', 2: 'bar', 3?: non-falsy-string}", $matches);
}
assertType("array{}|array{0: string, 1: 'foo', 2: 'bar', 3?: non-empty-string}", $matches);
assertType("array{}|array{0: string, 1: 'foo', 2: 'bar', 3?: non-falsy-string}", $matches);

if (preg_match('/(foo)(bar)(baz){2,3}/', $s, $matches)) {
assertType("array{string, 'foo', 'bar', non-empty-string}", $matches);
assertType("array{string, 'foo', 'bar', non-falsy-string}", $matches);
}
assertType("array{}|array{string, 'foo', 'bar', non-empty-string}", $matches);
assertType("array{}|array{string, 'foo', 'bar', non-falsy-string}", $matches);

if (preg_match('/(foo)(bar)(baz){2}/', $s, $matches)) {
assertType("array{string, 'foo', 'bar', non-empty-string}", $matches);
assertType("array{string, 'foo', 'bar', non-falsy-string}", $matches);
}
assertType("array{}|array{string, 'foo', 'bar', non-empty-string}", $matches);
assertType("array{}|array{string, 'foo', 'bar', non-falsy-string}", $matches);
}

function doNonCapturingGroup(string $s): void {
Expand All @@ -103,14 +103,14 @@ function doNamedSubpattern(string $s): void {
assertType('array{}|array{0: string, num: numeric-string, 1: numeric-string, 2: non-empty-string}', $matches);

if (preg_match('/^(?<name>\S+::\S+)/', $s, $matches)) {
assertType('array{0: string, name: non-empty-string, 1: non-empty-string}', $matches);
assertType('array{0: string, name: non-falsy-string, 1: non-falsy-string}', $matches);
}
assertType('array{}|array{0: string, name: non-empty-string, 1: non-empty-string}', $matches);
assertType('array{}|array{0: string, name: non-falsy-string, 1: non-falsy-string}', $matches);

if (preg_match('/^(?<name>\S+::\S+)(?:(?<dataname> with data set (?:#\d+|"[^"]+"))\s\()?/', $s, $matches)) {
assertType('array{0: string, name: non-empty-string, 1: non-empty-string, dataname?: non-empty-string, 2?: non-empty-string}', $matches);
assertType('array{0: string, name: non-falsy-string, 1: non-falsy-string, dataname?: non-falsy-string, 2?: non-falsy-string}', $matches);
}
assertType('array{}|array{0: string, name: non-empty-string, 1: non-empty-string, dataname?: non-empty-string, 2?: non-empty-string}', $matches);
assertType('array{}|array{0: string, name: non-falsy-string, 1: non-falsy-string, dataname?: non-falsy-string, 2?: non-falsy-string}', $matches);
}

function doOffsetCapture(string $s): void {
Expand Down Expand Up @@ -236,10 +236,10 @@ function doFoo(string $row): void
assertType("array{string, 'ab', 'b'}", $matches);
}
if (preg_match('~^(a(b)?)$~', $row, $matches) === 1) {
assertType("array{0: string, 1: non-empty-string, 2?: 'b'}", $matches);
assertType("array{0: string, 1: non-falsy-string, 2?: 'b'}", $matches);
}
if (preg_match('~^(a(b)?)?$~', $row, $matches) === 1) {
assertType("array{0: string, 1?: non-empty-string, 2?: 'b'}", $matches);
assertType("array{0: string, 1?: non-falsy-string, 2?: 'b'}", $matches);
}
}

Expand All @@ -249,7 +249,7 @@ function doFoo2(string $row): void
return;
}

assertType("array{0: string, 1: string, branchCode: ''|numeric-string, 2: ''|numeric-string, accountNumber: numeric-string, 3: numeric-string, bankCode: numeric-string, 4: numeric-string}", $matches);
assertType("array{0: string, 1: string, branchCode: ''|numeric-string, 2: ''|numeric-string, accountNumber: numeric-string, 3: numeric-string, bankCode: non-falsy-string&numeric-string, 4: non-falsy-string&numeric-string}", $matches);
}

function doFoo3(string $row): void
Expand All @@ -258,42 +258,42 @@ function doFoo3(string $row): void
return;
}

assertType('array{string, non-empty-string, non-empty-string, numeric-string, numeric-string, numeric-string, numeric-string}', $matches);
assertType('array{string, non-falsy-string, non-falsy-string, numeric-string, numeric-string, numeric-string, numeric-string}', $matches);
}

function (string $size): void {
if (preg_match('~^a\.b(c(\d+)(\d+)(\s+))?d~', $size, $matches) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{string, non-empty-string, numeric-string, numeric-string, non-empty-string}|array{string}', $matches);
assertType('array{string, non-falsy-string, numeric-string, numeric-string, non-empty-string}|array{string}', $matches);
};

function (string $size): void {
if (preg_match('~^a\.b(c(\d+))?d~', $size, $matches) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{string, non-empty-string, numeric-string}|array{string}', $matches);
assertType('array{string, non-falsy-string, numeric-string}|array{string}', $matches);
};

function (string $size): void {
if (preg_match('~^a\.b(c(\d+)?)d~', $size, $matches) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{0: string, 1: non-empty-string, 2?: numeric-string}', $matches);
assertType('array{0: string, 1: non-falsy-string, 2?: numeric-string}', $matches);
};

function (string $size): void {
if (preg_match('~^a\.b(c(\d+)?)?d~', $size, $matches) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{0: string, 1?: non-empty-string, 2?: numeric-string}', $matches);
assertType('array{0: string, 1?: non-falsy-string, 2?: numeric-string}', $matches);
};

function (string $size): void {
if (preg_match('~^a\.b(c(\d+))d~', $size, $matches) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{string, non-empty-string, numeric-string}', $matches);
assertType('array{string, non-falsy-string, numeric-string}', $matches);
};

function (string $size): void {
Expand Down Expand Up @@ -321,14 +321,14 @@ 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{0: string, 1: 'include', 2?: non-empty-string, 3?: non-empty-string}", $matches);
assertType("array{0: string, 1: 'include', 2?: non-falsy-string, 3?: non-falsy-string}", $matches);
};


function bug11277a(string $value): void
{
if (preg_match('/^\[(.+,?)*\]$/', $value, $matches)) {
assertType('array{0: string, 1?: non-empty-string}', $matches);
assertType('array{0: string, 1?: non-falsy-string}', $matches);
if (count($matches) === 2) {
assertType('array{string, string}', $matches); // could be array{string, non-empty-string}
}
Expand All @@ -338,7 +338,7 @@ function bug11277a(string $value): void
function bug11277b(string $value): void
{
if (preg_match('/^(?:(.+,?)|(x))*$/', $value, $matches)) {
assertType('array{0: string, 1?: non-empty-string, 2?: non-empty-string}', $matches);
assertType('array{0: string, 1?: non-falsy-string, 2?: non-empty-string}', $matches);
if (count($matches) === 2) {
assertType('array{string, string}', $matches); // could be array{string, non-empty-string}
}
Expand Down Expand Up @@ -441,13 +441,13 @@ function (string $s): void {

function (string $s): void {
if (preg_match('~^((\\d{1,6})-)$~', $s, $matches) === 1) {
assertType("array{string, non-empty-string, numeric-string}", $matches);
assertType("array{string, non-falsy-string, numeric-string}", $matches);
}
};

function (string $s): void {
if (preg_match('~^((\\d{1,6}).)$~', $s, $matches) === 1) {
assertType("array{string, non-empty-string, numeric-string}", $matches);
assertType("array{string, non-falsy-string, numeric-string}", $matches);
}
};

Expand All @@ -471,13 +471,13 @@ function bug11323(string $s): void {
assertType('array{string, non-empty-string, non-empty-string}', $matches);
}
if (preg_match('{([-\p{L}[\]*|\x03\a\b+?{}(?:)-]+[^[:digit:]?{}a-z0-9#-k]+)(a-z)}', $s, $matches)) {
assertType("array{string, non-empty-string, 'a-z'}", $matches);
assertType("array{string, non-falsy-string, 'a-z'}", $matches);
}
if (preg_match('{(\d+)(?i)insensitive((?x-i)case SENSITIVE here(?i:insensitive non-capturing group))}', $s, $matches)) {
assertType('array{string, numeric-string, non-empty-string}', $matches);
assertType('array{string, numeric-string, non-falsy-string}', $matches);
}
if (preg_match('{([]] [^]])}', $s, $matches)) {
assertType('array{string, non-empty-string}', $matches);
assertType('array{string, non-falsy-string}', $matches);
}
if (preg_match('{([[:digit:]])}', $s, $matches)) {
assertType('array{string, numeric-string}', $matches);
Expand All @@ -495,25 +495,25 @@ function bug11323(string $s): void {
assertType("array{string, ''|'a', string, non-empty-string, non-empty-string}", $matches);
}
if (preg_match('{(.\d)}', $s, $matches)) {
assertType('array{string, non-empty-string}', $matches);
assertType('array{string, non-falsy-string}', $matches);
}
if (preg_match('{(\d.)}', $s, $matches)) {
assertType('array{string, non-empty-string}', $matches);
assertType('array{string, non-falsy-string}', $matches);
}
if (preg_match('{(\d\d)}', $s, $matches)) {
assertType('array{string, numeric-string}', $matches);
assertType('array{string, non-falsy-string&numeric-string}', $matches);
}
if (preg_match('{(.(\d))}', $s, $matches)) {
assertType('array{string, non-empty-string, numeric-string}', $matches);
assertType('array{string, non-falsy-string, numeric-string}', $matches);
}
if (preg_match('{((\d).)}', $s, $matches)) {
assertType('array{string, non-empty-string, numeric-string}', $matches);
assertType('array{string, non-falsy-string, numeric-string}', $matches);
}
if (preg_match('{(\d([1-4])\d)}', $s, $matches)) {
assertType('array{string, numeric-string, numeric-string}', $matches);
assertType('array{string, non-falsy-string&numeric-string, numeric-string}', $matches);
}
if (preg_match('{(x?([1-4])\d)}', $s, $matches)) {
assertType('array{string, non-empty-string, numeric-string}', $matches);
assertType('array{string, non-falsy-string, numeric-string}', $matches);
}
if (preg_match('{([^1-4])}', $s, $matches)) {
assertType('array{string, non-empty-string}', $matches);
Expand Down Expand Up @@ -606,3 +606,22 @@ function (string $s): void {
assertType("array{string, 'bam'|'ban'|'bom'|'bon'}", $matches);
}
};

function (string $s): void {
if (preg_match('/Price: (\s{3}|0)/', $s, $matches)) {
assertType("array{string, non-empty-string}", $matches);
}
};

function (string $s): void {
if (preg_match('/Price: (a|0)/', $s, $matches)) {
assertType("array{string, non-empty-string}", $matches);
}
};

function (string $s): void {
if (preg_match('/Price: (aa|0)/', $s, $matches)) {
assertType("array{string, non-empty-string}", $matches);
}
};

Loading