Skip to content

Commit 37fb38a

Browse files
staabmondrejmirtes
authored andcommitted
Implement BinaryConcatHandler
1 parent 79aedb0 commit 37fb38a

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser\Generator\ExprHandler;
4+
5+
use Generator;
6+
use PhpParser\Node\Expr;
7+
use PhpParser\Node\Stmt;
8+
use PHPStan\Analyser\ExpressionContext;
9+
use PHPStan\Analyser\Generator\ExprAnalysisRequest;
10+
use PHPStan\Analyser\Generator\ExprAnalysisResult;
11+
use PHPStan\Analyser\Generator\ExprHandler;
12+
use PHPStan\Analyser\Generator\GeneratorScope;
13+
use PHPStan\Analyser\SpecifiedTypes;
14+
use PHPStan\DependencyInjection\AutowiredService;
15+
use PHPStan\Reflection\InitializerExprTypeResolver;
16+
use function array_merge;
17+
18+
/**
19+
* @implements ExprHandler<Expr\BinaryOp\Concat>
20+
*/
21+
#[AutowiredService]
22+
final class BinaryConcatHandler implements ExprHandler
23+
{
24+
25+
public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver)
26+
{
27+
}
28+
29+
public function supports(Expr $expr): bool
30+
{
31+
return $expr instanceof Expr\BinaryOp\Concat;
32+
}
33+
34+
public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, ExpressionContext $context, ?callable $alternativeNodeCallback): Generator
35+
{
36+
$leftResult = yield new ExprAnalysisRequest($stmt, $expr->left, $scope, $context, $alternativeNodeCallback);
37+
$rightResult = yield new ExprAnalysisRequest($stmt, $expr->right, $leftResult->scope, $context, $alternativeNodeCallback);
38+
39+
return new ExprAnalysisResult(
40+
$this->initializerExprTypeResolver->resolveConcatType($leftResult->type, $rightResult->type),
41+
$this->initializerExprTypeResolver->resolveConcatType($leftResult->nativeType, $rightResult->nativeType),
42+
$rightResult->scope,
43+
hasYield: $leftResult->hasYield || $rightResult->hasYield,
44+
isAlwaysTerminating: $leftResult->isAlwaysTerminating || $rightResult->isAlwaysTerminating,
45+
throwPoints: array_merge($leftResult->throwPoints, $rightResult->throwPoints),
46+
impurePoints: array_merge($leftResult->impurePoints, $rightResult->impurePoints),
47+
specifiedTruthyTypes: new SpecifiedTypes(),
48+
specifiedFalseyTypes: new SpecifiedTypes(),
49+
specifiedNullTypes: new SpecifiedTypes(),
50+
);
51+
}
52+
53+
}

tests/PHPStan/Analyser/Generator/data/gnsr.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ public function doMul($a, $b, int $c, int $d): void
8282
assertNativeType('int', $c * $d);
8383
}
8484

85+
/**
86+
* @param string $a
87+
* @param string $b
88+
* @return void
89+
*/
90+
public function doConcat($a, $b, string $c, string $d): void
91+
{
92+
assertType('string', $a . $b);
93+
assertNativeType('string', $a . $b);
94+
assertType("'1a'", '1' . 'a');
95+
assertNativeType("'1a'", '1' . 'a');
96+
assertType('string', $c . $d);
97+
assertNativeType('string', $c . $d);
98+
}
99+
85100
}
86101

87102
function (): void {

0 commit comments

Comments
 (0)