diff --git a/src/Analyser/Generator/ExprHandler/BinaryConcatHandler.php b/src/Analyser/Generator/ExprHandler/BinaryConcatHandler.php new file mode 100644 index 0000000000..381365b5e8 --- /dev/null +++ b/src/Analyser/Generator/ExprHandler/BinaryConcatHandler.php @@ -0,0 +1,53 @@ + + */ +#[AutowiredService] +final class BinaryConcatHandler implements ExprHandler +{ + + public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver) + { + } + + public function supports(Expr $expr): bool + { + return $expr instanceof Expr\BinaryOp\Concat; + } + + public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, ExpressionContext $context, ?callable $alternativeNodeCallback): Generator + { + $leftResult = yield new ExprAnalysisRequest($stmt, $expr->left, $scope, $context, $alternativeNodeCallback); + $rightResult = yield new ExprAnalysisRequest($stmt, $expr->right, $leftResult->scope, $context, $alternativeNodeCallback); + + return new ExprAnalysisResult( + $this->initializerExprTypeResolver->resolveConcatType($leftResult->type, $rightResult->type), + $this->initializerExprTypeResolver->resolveConcatType($leftResult->nativeType, $rightResult->nativeType), + $rightResult->scope, + hasYield: $leftResult->hasYield || $rightResult->hasYield, + isAlwaysTerminating: $leftResult->isAlwaysTerminating || $rightResult->isAlwaysTerminating, + throwPoints: array_merge($leftResult->throwPoints, $rightResult->throwPoints), + impurePoints: array_merge($leftResult->impurePoints, $rightResult->impurePoints), + specifiedTruthyTypes: new SpecifiedTypes(), + specifiedFalseyTypes: new SpecifiedTypes(), + specifiedNullTypes: new SpecifiedTypes(), + ); + } + +} diff --git a/tests/PHPStan/Analyser/Generator/data/gnsr.php b/tests/PHPStan/Analyser/Generator/data/gnsr.php index ba7b679723..151ff2e406 100644 --- a/tests/PHPStan/Analyser/Generator/data/gnsr.php +++ b/tests/PHPStan/Analyser/Generator/data/gnsr.php @@ -82,6 +82,21 @@ public function doMul($a, $b, int $c, int $d): void assertNativeType('int', $c * $d); } + /** + * @param string $a + * @param string $b + * @return void + */ + public function doConcat($a, $b, string $c, string $d): void + { + assertType('string', $a . $b); + assertNativeType('string', $a . $b); + assertType("'1a'", '1' . 'a'); + assertNativeType("'1a'", '1' . 'a'); + assertType('string', $c . $d); + assertNativeType('string', $c . $d); + } + } function (): void {