1313use PHPStan \Php \PhpVersion ;
1414use PHPStan \ShouldNotHappenException ;
1515use PHPStan \TrinaryLogic ;
16- use PHPStan \Type \Accessory \AccessoryDecimalIntegerStringType ;
1716use PHPStan \Type \Accessory \AccessoryNonEmptyStringType ;
1817use PHPStan \Type \Accessory \AccessoryNonFalsyStringType ;
18+ use PHPStan \Type \Accessory \AccessoryNumericStringType ;
1919use PHPStan \Type \Constant \ConstantStringType ;
2020use PHPStan \Type \IntersectionType ;
2121use 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 }
0 commit comments