Skip to content

Commit e036b15

Browse files
committed
Revert 7fe8b00
1 parent e17e049 commit e036b15

13 files changed

Lines changed: 161 additions & 322 deletions

‎src/Type/Regex/RegexExpressionHelper.php‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
use PHPStan\Type\TypeCombinator;
1414
use function array_key_exists;
1515
use function ltrim;
16-
use function str_ends_with;
17-
use function str_starts_with;
1816
use function strrpos;
1917
use function substr;
2018

@@ -91,13 +89,6 @@ public function getPatternModifiers(string $pattern): ?string
9189
return substr($pattern, $endDelimiterPos + 1);
9290
}
9391

94-
public function isAnchoredPattern(string $pattern): bool
95-
{
96-
$cleanedPattern = $this->removeDelimitersAndModifiers($pattern);
97-
98-
return str_starts_with($cleanedPattern, '^') && str_ends_with($cleanedPattern, '$');
99-
}
100-
10192
public function removeDelimitersAndModifiers(string $pattern): string
10293
{
10394
$pattern = ltrim($pattern);

‎src/Type/Regex/RegexGroupParser.php‎

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
use PHPStan\Php\PhpVersion;
1414
use PHPStan\ShouldNotHappenException;
1515
use PHPStan\TrinaryLogic;
16-
use PHPStan\Type\Accessory\AccessoryDecimalIntegerStringType;
1716
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
1817
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
18+
use PHPStan\Type\Accessory\AccessoryNumericStringType;
1919
use PHPStan\Type\Constant\ConstantStringType;
2020
use PHPStan\Type\IntersectionType;
2121
use PHPStan\Type\StringType;
@@ -125,14 +125,8 @@ public function parseGroups(string $regex): ?RegexAstWalkResult
125125
);
126126

127127
if (!$subjectAsGroupResult->mightContainEmptyStringLiteral() && !$this->containsEscapeK($ast)) {
128-
if (
129-
$subjectAsGroupResult->isDecimalInteger()->yes()
130-
&& $this->regexExpressionHelper->isAnchoredPattern($regex)
131-
) {
132-
$astWalkResult = $astWalkResult->withSubjectBaseType(
133-
new IntersectionType([new StringType(), new AccessoryDecimalIntegerStringType()]),
134-
);
135-
} elseif ($subjectAsGroupResult->isNonFalsy()->yes()) {
128+
// we could handle numeric-string, in case we know the regex is delimited by ^ and $
129+
if ($subjectAsGroupResult->isNonFalsy()->yes()) {
136130
$astWalkResult = $astWalkResult->withSubjectBaseType(
137131
new IntersectionType([new StringType(), new AccessoryNonFalsyStringType()]),
138132
);
@@ -426,16 +420,16 @@ private function createGroupType(TreeNode $group, bool $maybeConstant, string $p
426420
return TypeCombinator::union(...$result);
427421
}
428422

429-
if ($walkResult->isDecimalInteger()->yes()) {
423+
if ($walkResult->isNumeric()->yes()) {
430424
if ($walkResult->isNonFalsy()->yes()) {
431425
return new IntersectionType([
432426
new StringType(),
433-
new AccessoryDecimalIntegerStringType(),
427+
new AccessoryNumericStringType(),
434428
new AccessoryNonFalsyStringType(),
435429
]);
436430
}
437431

438-
$result = new IntersectionType([new StringType(), new AccessoryDecimalIntegerStringType()]);
432+
$result = new IntersectionType([new StringType(), new AccessoryNumericStringType()]);
439433
if (!$walkResult->isNonEmpty()->yes()) {
440434
return new UnionType([new ConstantStringType(''), $result]);
441435
}
@@ -488,17 +482,12 @@ private function walkGroupAst(
488482
$meaningfulTokens = 0;
489483
foreach ($children as $child) {
490484
$nonFalsy = false;
491-
$isNonDecimal = false;
492-
if ($this->isMaybeEmptyNode($child, $patternModifiers, $nonFalsy, $isNonDecimal)) {
485+
if ($this->isMaybeEmptyNode($child, $patternModifiers, $nonFalsy)) {
493486
continue;
494487
}
495488

496489
$meaningfulTokens++;
497490

498-
if ($isNonDecimal) {
499-
$walkResult = $walkResult->decimalInteger(TrinaryLogic::createNo());
500-
}
501-
502491
if (!$nonFalsy) {
503492
continue;
504493
}
@@ -552,19 +541,10 @@ private function walkGroupAst(
552541
$walkResult = $walkResult->onlyLiterals($onlyLiterals);
553542

554543
if ($literalValue !== null) {
555-
if (Strings::match($literalValue, '/^\d+$/') !== null) {
556-
if ($walkResult->isDecimalInteger()->maybe()) {
557-
$walkResult = $walkResult->decimalInteger(TrinaryLogic::createYes());
558-
}
559-
} elseif (
560-
$literalValue === '-'
561-
&& $walkResult->isDecimalInteger()->maybe()
562-
&& !$walkResult->hasSeenDecimalIntegerSign()
563-
) {
564-
// a single leading minus sign keeps the string a decimal integer (e.g. "-1")
565-
$walkResult = $walkResult->seenDecimalIntegerSign(true);
566-
} elseif ($literalValue !== '') {
567-
$walkResult = $walkResult->decimalInteger(TrinaryLogic::createNo());
544+
if (Strings::match($literalValue, '/^\d+$/') === null) {
545+
$walkResult = $walkResult->numeric(TrinaryLogic::createNo());
546+
} elseif ($walkResult->isNumeric()->maybe()) {
547+
$walkResult = $walkResult->numeric(TrinaryLogic::createYes());
568548
}
569549

570550
if (!$walkResult->isInOptionalQuantification() && $literalValue !== '') {
@@ -583,7 +563,7 @@ private function walkGroupAst(
583563
$newLiterals = [];
584564
$nonEmpty = TrinaryLogic::createYes();
585565
$nonFalsy = TrinaryLogic::createYes();
586-
$decimalInteger = TrinaryLogic::createYes();
566+
$numeric = TrinaryLogic::createYes();
587567
foreach ($children as $child) {
588568
$childResult = $this->walkGroupAst(
589569
$child,
@@ -592,13 +572,12 @@ private function walkGroupAst(
592572
$walkResult->onlyLiterals([])
593573
->nonEmpty(TrinaryLogic::createMaybe())
594574
->nonFalsy(TrinaryLogic::createMaybe())
595-
->decimalInteger(TrinaryLogic::createMaybe())
596-
->seenDecimalIntegerSign(false),
575+
->numeric(TrinaryLogic::createMaybe()),
597576
);
598577

599578
$nonEmpty = $nonEmpty->and($childResult->isNonEmpty());
600579
$nonFalsy = $nonFalsy->and($childResult->isNonFalsy());
601-
$decimalInteger = $decimalInteger->and($childResult->isDecimalInteger());
580+
$numeric = $numeric->and($childResult->isNumeric());
602581

603582
if ($newLiterals === null) {
604583
continue;
@@ -617,14 +596,14 @@ private function walkGroupAst(
617596
->onlyLiterals($newLiterals)
618597
->nonEmpty($walkResult->isNonEmpty()->or($nonEmpty))
619598
->nonFalsy($walkResult->isNonFalsy()->or($nonFalsy))
620-
->decimalInteger(TrinaryLogic::maxMin($walkResult->isDecimalInteger(), $decimalInteger));
599+
->numeric($walkResult->isNumeric()->and($numeric));
621600
}
622601

623-
// [^0-9] should not parse as decimal-int-string, and [^list-everything-but-numbers] is technically
624-
// doable but really silly compared to just \d so we can safely assume the string is not a decimal
625-
// integer for negative classes
602+
// [^0-9] should not parse as numeric-string, and [^list-everything-but-numbers] is technically
603+
// doable but really silly compared to just \d so we can safely assume the string is not numeric
604+
// for negative classes
626605
if ($ast->getId() === '#negativeclass') {
627-
$walkResult = $walkResult->decimalInteger(TrinaryLogic::createNo());
606+
$walkResult = $walkResult->numeric(TrinaryLogic::createNo());
628607
}
629608

630609
foreach ($children as $child) {
@@ -639,7 +618,7 @@ private function walkGroupAst(
639618
return $walkResult;
640619
}
641620

642-
private function isMaybeEmptyNode(TreeNode $node, string $patternModifiers, bool &$isNonFalsy, bool &$isNonDecimal): bool
621+
private function isMaybeEmptyNode(TreeNode $node, string $patternModifiers, bool &$isNonFalsy): bool
643622
{
644623
if ($node->getId() === '#quantification') {
645624
[$min] = $this->getQuantificationRange($node);
@@ -658,14 +637,11 @@ private function isMaybeEmptyNode(TreeNode $node, string $patternModifiers, bool
658637
if ($literal !== '' && $literal !== '0') {
659638
$isNonFalsy = true;
660639
}
661-
if (Strings::match($literal, '/^\d+$/') === null) {
662-
$isNonDecimal = true;
663-
}
664640
return $literal === '';
665641
}
666642

667643
foreach ($node->getChildren() as $child) {
668-
if (!$this->isMaybeEmptyNode($child, $patternModifiers, $isNonFalsy, $isNonDecimal)) {
644+
if (!$this->isMaybeEmptyNode($child, $patternModifiers, $isNonFalsy)) {
669645
return false;
670646
}
671647
}

‎src/Type/Regex/RegexGroupWalkResult.php‎

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ public function __construct(
1616
private ?array $onlyLiterals,
1717
private TrinaryLogic $isNonEmpty,
1818
private TrinaryLogic $isNonFalsy,
19-
private TrinaryLogic $isDecimalInteger,
20-
private bool $seenDecimalIntegerSign,
19+
private TrinaryLogic $isNumeric,
2120
)
2221
{
2322
}
@@ -30,7 +29,6 @@ public static function createEmpty(): self
3029
TrinaryLogic::createMaybe(),
3130
TrinaryLogic::createMaybe(),
3231
TrinaryLogic::createMaybe(),
33-
false,
3432
);
3533
}
3634

@@ -41,8 +39,7 @@ public function inOptionalQuantification(bool $inOptionalQuantification): self
4139
$this->onlyLiterals,
4240
$this->isNonEmpty,
4341
$this->isNonFalsy,
44-
$this->isDecimalInteger,
45-
$this->seenDecimalIntegerSign,
42+
$this->isNumeric,
4643
);
4744
}
4845

@@ -56,8 +53,7 @@ public function onlyLiterals(?array $onlyLiterals): self
5653
$onlyLiterals,
5754
$this->isNonEmpty,
5855
$this->isNonFalsy,
59-
$this->isDecimalInteger,
60-
$this->seenDecimalIntegerSign,
56+
$this->isNumeric,
6157
);
6258
}
6359

@@ -68,8 +64,7 @@ public function nonEmpty(TrinaryLogic $nonEmpty): self
6864
$this->onlyLiterals,
6965
$nonEmpty,
7066
$this->isNonFalsy,
71-
$this->isDecimalInteger,
72-
$this->seenDecimalIntegerSign,
67+
$this->isNumeric,
7368
);
7469
}
7570

@@ -80,33 +75,18 @@ public function nonFalsy(TrinaryLogic $nonFalsy): self
8075
$this->onlyLiterals,
8176
$this->isNonEmpty,
8277
$nonFalsy,
83-
$this->isDecimalInteger,
84-
$this->seenDecimalIntegerSign,
78+
$this->isNumeric,
8579
);
8680
}
8781

88-
/** A decimal integer string is composed only of digits, optionally preceded by a single leading minus sign. */
89-
public function decimalInteger(TrinaryLogic $decimalInteger): self
82+
public function numeric(TrinaryLogic $numeric): self
9083
{
9184
return new self(
9285
$this->inOptionalQuantification,
9386
$this->onlyLiterals,
9487
$this->isNonEmpty,
9588
$this->isNonFalsy,
96-
$decimalInteger,
97-
$this->seenDecimalIntegerSign,
98-
);
99-
}
100-
101-
public function seenDecimalIntegerSign(bool $seenDecimalIntegerSign): self
102-
{
103-
return new self(
104-
$this->inOptionalQuantification,
105-
$this->onlyLiterals,
106-
$this->isNonEmpty,
107-
$this->isNonFalsy,
108-
$this->isDecimalInteger,
109-
$seenDecimalIntegerSign,
89+
$numeric,
11090
);
11191
}
11292

@@ -147,14 +127,9 @@ public function isNonFalsy(): TrinaryLogic
147127
return $this->isNonFalsy;
148128
}
149129

150-
public function isDecimalInteger(): TrinaryLogic
151-
{
152-
return $this->isDecimalInteger;
153-
}
154-
155-
public function hasSeenDecimalIntegerSign(): bool
130+
public function isNumeric(): TrinaryLogic
156131
{
157-
return $this->seenDecimalIntegerSign;
132+
return $this->isNumeric;
158133
}
159134

160135
}

‎tests/PHPStan/Analyser/nsrt/bug-11293.php‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,54 @@ class HelloWorld
99
public function sayHello(string $s): void
1010
{
1111
if (preg_match('/data-(\d{6})\.json$/', $s, $matches) > 0) {
12-
assertType('array{non-falsy-string, decimal-int-string&non-falsy-string}', $matches);
12+
assertType('array{non-falsy-string, non-falsy-string&numeric-string}', $matches);
1313
}
1414
}
1515

1616
public function sayHello2(string $s): void
1717
{
1818
if (preg_match('/data-(\d{6})\.json$/', $s, $matches) === 1) {
19-
assertType('array{non-falsy-string, decimal-int-string&non-falsy-string}', $matches);
19+
assertType('array{non-falsy-string, non-falsy-string&numeric-string}', $matches);
2020
}
2121
}
2222

2323
public function sayHello3(string $s): void
2424
{
2525
if (preg_match('/data-(\d{6})\.json$/', $s, $matches) >= 1) {
26-
assertType('array{non-falsy-string, decimal-int-string&non-falsy-string}', $matches);
26+
assertType('array{non-falsy-string, non-falsy-string&numeric-string}', $matches);
2727
}
2828
}
2929

3030
public function sayHello4(string $s): void
3131
{
3232
if (preg_match('/data-(\d{6})\.json$/', $s, $matches) <= 0) {
33-
assertType('array{}|array{non-falsy-string, decimal-int-string&non-falsy-string}', $matches);
33+
assertType('array{}|array{non-falsy-string, non-falsy-string&numeric-string}', $matches);
3434

3535
return;
3636
}
3737

38-
assertType('array{non-falsy-string, decimal-int-string&non-falsy-string}', $matches);
38+
assertType('array{non-falsy-string, non-falsy-string&numeric-string}', $matches);
3939
}
4040

4141
public function sayHello5(string $s): void
4242
{
4343
if (preg_match('/data-(\d{6})\.json$/', $s, $matches) < 1) {
44-
assertType('array{}|array{non-falsy-string, decimal-int-string&non-falsy-string}', $matches);
44+
assertType('array{}|array{non-falsy-string, non-falsy-string&numeric-string}', $matches);
4545

4646
return;
4747
}
4848

49-
assertType('array{non-falsy-string, decimal-int-string&non-falsy-string}', $matches);
49+
assertType('array{non-falsy-string, non-falsy-string&numeric-string}', $matches);
5050
}
5151

5252
public function sayHello6(string $s): void
5353
{
5454
if (1 > preg_match('/data-(\d{6})\.json$/', $s, $matches)) {
55-
assertType('array{}|array{non-falsy-string, decimal-int-string&non-falsy-string}', $matches);
55+
assertType('array{}|array{non-falsy-string, non-falsy-string&numeric-string}', $matches);
5656

5757
return;
5858
}
5959

60-
assertType('array{non-falsy-string, decimal-int-string&non-falsy-string}', $matches);
60+
assertType('array{non-falsy-string, non-falsy-string&numeric-string}', $matches);
6161
}
6262
}

0 commit comments

Comments
 (0)