Skip to content

Commit f7e6e74

Browse files
committed
Support arrays with union value-types in implode()
1 parent fd7bad3 commit f7e6e74

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

Diff for: phpstan-baseline.neon

-5
Original file line numberDiff line numberDiff line change
@@ -1367,11 +1367,6 @@ parameters:
13671367
count: 1
13681368
path: src/Type/Php/FunctionExistsFunctionTypeSpecifyingExtension.php
13691369

1370-
-
1371-
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
1372-
count: 1
1373-
path: src/Type/Php/ImplodeFunctionReturnTypeExtension.php
1374-
13751370
-
13761371
message: """
13771372
#^Call to deprecated method getConstantScalars\\(\\) of class PHPStan\\\\Type\\\\TypeUtils\\:

Diff for: src/Type/Php/ImplodeFunctionReturnTypeExtension.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\Node\Expr\FuncCall;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\Internal\CombinationsHelper;
78
use PHPStan\Reflection\FunctionReflection;
89
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
910
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
@@ -12,7 +13,6 @@
1213
use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
1314
use PHPStan\Type\Constant\ConstantArrayType;
1415
use PHPStan\Type\Constant\ConstantStringType;
15-
use PHPStan\Type\ConstantScalarType;
1616
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1717
use PHPStan\Type\IntersectionType;
1818
use PHPStan\Type\StringType;
@@ -115,13 +115,17 @@ private function inferConstantType(ConstantArrayType $arrayType, ConstantStringT
115115

116116
$arrayValues = [];
117117
foreach ($valueTypes as $valueType) {
118-
if (!$valueType instanceof ConstantScalarType) {
118+
$constScalars = $valueType->getConstantScalarValues();
119+
if (count($constScalars) === 0) {
119120
return null;
120121
}
121-
$arrayValues[] = $valueType->getValue();
122+
$arrayValues[] = $constScalars;
122123
}
123124

124-
$strings[] = new ConstantStringType(implode($separatorType->getValue(), $arrayValues));
125+
$combinations = CombinationsHelper::combinations($arrayValues);
126+
foreach ($combinations as $combination) {
127+
$strings[] = new ConstantStringType(implode($separatorType->getValue(), $combination));
128+
}
125129
}
126130

127131
return TypeCombinator::union(...$strings);

Diff for: tests/PHPStan/Analyser/nsrt/implode.php

+25
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,29 @@ public function constants() {
2121
assertType("'x,345'", join(',', [self::X, '345']));
2222
assertType("'1,345'", join(',', [self::ONE, '345']));
2323
}
24+
25+
/** @param array{0: 1|2, 1: 'a'|'b'} $constArr */
26+
public function constArrays($constArr) {
27+
assertType("'1a'|'1b'|'2a'|'2b'", implode('', $constArr));
28+
}
29+
30+
/** @param array{0: 1|2|3, 1: 'a'|'b'|'c'} $constArr */
31+
public function constArrays2($constArr) {
32+
assertType("'1a'|'1b'|'1c'|'2a'|'2b'|'2c'|'3a'|'3b'|'3c'", implode('', $constArr));
33+
}
34+
35+
/** @param array{0: 1, 1: 'a'|'b', 2: 'x'|'y'} $constArr */
36+
public function constArrays3($constArr) {
37+
assertType("'1ax'|'1ay'|'1bx'|'1by'", implode('', $constArr));
38+
}
39+
40+
/** @param array{0: 1, 1: 'a'|'b', 2?: 'x'|'y'} $constArr */
41+
public function constArrays4($constArr) {
42+
assertType("'1a'|'1ax'|'1ay'|'1b'|'1bx'|'1by'", implode('', $constArr));
43+
}
44+
45+
/** @param array{10: 1|2|3, xy: 'a'|'b'|'c'} $constArr */
46+
public function constArrays5($constArr) {
47+
assertType("'1a'|'1b'|'1c'|'2a'|'2b'|'2c'|'3a'|'3b'|'3c'", implode('', $constArr));
48+
}
2449
}

0 commit comments

Comments
 (0)