|
13 | 13 | use PhpParser\Node\Name;
|
14 | 14 | use PHPStan\Analyser\Scope;
|
15 | 15 | use PHPStan\Php\PhpVersion;
|
| 16 | +use PHPStan\Reflection\InitializerExprTypeResolver; |
16 | 17 | use PHPStan\ShouldNotHappenException;
|
17 | 18 | use PHPStan\TrinaryLogic;
|
18 | 19 | use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
|
@@ -55,6 +56,7 @@ final class RegexArrayShapeMatcher
|
55 | 56 |
|
56 | 57 | public function __construct(
|
57 | 58 | private PhpVersion $phpVersion,
|
| 59 | + private InitializerExprTypeResolver $initializerExprTypeResolver, |
58 | 60 | )
|
59 | 61 | {
|
60 | 62 | }
|
@@ -724,38 +726,33 @@ private function getPatternType(Expr $patternExpr, Scope $scope): Type
|
724 | 726 | */
|
725 | 727 | private function resolvePatternConcat(Expr\BinaryOp\Concat $concat, Scope $scope): Type
|
726 | 728 | {
|
727 |
| - if ( |
728 |
| - $concat->left instanceof Expr\FuncCall |
729 |
| - && $concat->left->name instanceof Name |
730 |
| - && $concat->left->name->toLowerString() === 'preg_quote' |
731 |
| - ) { |
732 |
| - $left = new ConstantStringType(''); |
733 |
| - } elseif ($concat->left instanceof Expr\BinaryOp\Concat) { |
734 |
| - $left = $this->resolvePatternConcat($concat->left, $scope); |
735 |
| - } else { |
736 |
| - $left = $scope->getType($concat->left); |
737 |
| - } |
738 |
| - |
739 |
| - if ( |
740 |
| - $concat->right instanceof Expr\FuncCall |
741 |
| - && $concat->right->name instanceof Name |
742 |
| - && $concat->right->name->toLowerString() === 'preg_quote' |
743 |
| - ) { |
744 |
| - $right = new ConstantStringType(''); |
745 |
| - } elseif ($concat->right instanceof Expr\BinaryOp\Concat) { |
746 |
| - $right = $this->resolvePatternConcat($concat->right, $scope); |
747 |
| - } else { |
748 |
| - $right = $scope->getType($concat->right); |
749 |
| - } |
| 729 | + $resolveConcat = static function (Expr $expr) use (&$resolveConcat, $scope) { |
| 730 | + if ( |
| 731 | + $expr instanceof Expr\FuncCall |
| 732 | + && $expr->name instanceof Name |
| 733 | + && $expr->name->toLowerString() === 'preg_quote' |
| 734 | + ) { |
| 735 | + return new ConstantStringType(''); |
| 736 | + } |
| 737 | + |
| 738 | + if ($expr instanceof Expr\BinaryOp\Concat) { |
| 739 | + $left = $resolveConcat($expr->left); |
| 740 | + $right = $resolveConcat($expr->right); |
| 741 | + |
| 742 | + $strings = []; |
| 743 | + foreach ($left->toString()->getConstantStrings() as $leftString) { |
| 744 | + foreach ($right->toString()->getConstantStrings() as $rightString) { |
| 745 | + $strings[] = new ConstantStringType($leftString->getValue() . $rightString->getValue()); |
| 746 | + } |
| 747 | + } |
750 | 748 |
|
751 |
| - $strings = []; |
752 |
| - foreach ($left->getConstantStrings() as $leftString) { |
753 |
| - foreach ($right->getConstantStrings() as $rightString) { |
754 |
| - $strings[] = new ConstantStringType($leftString->getValue() . $rightString->getValue()); |
| 749 | + return TypeCombinator::union(...$strings); |
755 | 750 | }
|
756 |
| - } |
757 | 751 |
|
758 |
| - return TypeCombinator::union(...$strings); |
| 752 | + return $scope->getType($expr); |
| 753 | + }; |
| 754 | + |
| 755 | + return $this->initializerExprTypeResolver->getConcatType($concat->left, $concat->right, $resolveConcat); |
759 | 756 | }
|
760 | 757 |
|
761 | 758 | }
|
0 commit comments