-
Notifications
You must be signed in to change notification settings - Fork 514
Support arrays with union value-types in implode() #3772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,15 +4,16 @@ | |
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Internal\CombinationsHelper; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Reflection\InitializerExprTypeResolver; | ||
use PHPStan\Type\Accessory\AccessoryLiteralStringType; | ||
use PHPStan\Type\Accessory\AccessoryLowercaseStringType; | ||
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; | ||
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType; | ||
use PHPStan\Type\Accessory\AccessoryUppercaseStringType; | ||
use PHPStan\Type\Constant\ConstantArrayType; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\ConstantScalarType; | ||
use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
use PHPStan\Type\IntersectionType; | ||
use PHPStan\Type\StringType; | ||
|
@@ -114,14 +115,28 @@ private function inferConstantType(ConstantArrayType $arrayType, ConstantStringT | |
$valueTypes = $array->getValueTypes(); | ||
|
||
$arrayValues = []; | ||
$combinationsCount = 1; | ||
foreach ($valueTypes as $valueType) { | ||
if (!$valueType instanceof ConstantScalarType) { | ||
$constScalars = $valueType->getConstantScalarValues(); | ||
if (count($constScalars) === 0) { | ||
return null; | ||
} | ||
$arrayValues[] = $valueType->getValue(); | ||
$arrayValues[] = $constScalars; | ||
$combinationsCount *= count($constScalars); | ||
} | ||
|
||
$strings[] = new ConstantStringType(implode($separatorType->getValue(), $arrayValues)); | ||
if ($combinationsCount > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) { | ||
return null; | ||
} | ||
|
||
$combinations = CombinationsHelper::combinations($arrayValues); | ||
foreach ($combinations as $combination) { | ||
$strings[] = new ConstantStringType(implode($separatorType->getValue(), $combination)); | ||
} | ||
} | ||
|
||
if (count($strings) > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally you'd count the combinations before generating them, it can explode as well. |
||
return null; | ||
} | ||
|
||
return TypeCombinator::union(...$strings); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Bug11854; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class HelloWorld | ||
{ | ||
public function sayHello(): void | ||
{ | ||
$arr = []; | ||
$arr[] = rand(0,1) ? 'A' : 'B'; | ||
$arr[] = rand(0,1) ? 'C' : ''; | ||
|
||
assertType("array{'A'|'B', ''|'C'}", $arr); | ||
assertType("'A '|'A C'|'B '|'B C'", implode(' ', $arr)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,34 @@ public function constants() { | |
assertType("'x,345'", join(',', [self::X, '345'])); | ||
assertType("'1,345'", join(',', [self::ONE, '345'])); | ||
} | ||
|
||
/** @param array{0: 1|2, 1: 'a'|'b'} $constArr */ | ||
public function constArrays($constArr) { | ||
assertType("'1a'|'1b'|'2a'|'2b'", implode('', $constArr)); | ||
} | ||
|
||
/** @param array{0: 1|2|3, 1: 'a'|'b'|'c'} $constArr */ | ||
public function constArrays2($constArr) { | ||
assertType("'1a'|'1b'|'1c'|'2a'|'2b'|'2c'|'3a'|'3b'|'3c'", implode('', $constArr)); | ||
} | ||
|
||
/** @param array{0: 1, 1: 'a'|'b', 2: 'x'|'y'} $constArr */ | ||
public function constArrays3($constArr) { | ||
assertType("'1ax'|'1ay'|'1bx'|'1by'", implode('', $constArr)); | ||
} | ||
|
||
/** @param array{0: 1, 1: 'a'|'b', 2?: 'x'|'y'} $constArr */ | ||
public function constArrays4($constArr) { | ||
assertType("'1a'|'1ax'|'1ay'|'1b'|'1bx'|'1by'", implode('', $constArr)); | ||
} | ||
|
||
/** @param array{10: 1|2|3, xy: 'a'|'b'|'c'} $constArr */ | ||
public function constArrays5($constArr) { | ||
assertType("'1a'|'1b'|'1c'|'2a'|'2b'|'2c'|'3a'|'3b'|'3c'", implode('', $constArr)); | ||
} | ||
|
||
/** @param array{0: 1, 1: 'a'|'b', 3?: 'c'|'d', 4?: 'e'|'f', 5?: 'g'|'h', 6?: 'x'|'y'} $constArr */ | ||
public function constArrays6($constArr) { | ||
assertType("string", implode('', $constArr)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this too generic type made me think about the general case and triggerd #3774 |
||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.