Support arrays with union value-types in implode()#3772
Merged
ondrejmirtes merged 5 commits intophpstan:1.12.xfrom Jan 7, 2025
Merged
Support arrays with union value-types in implode()#3772ondrejmirtes merged 5 commits intophpstan:1.12.xfrom
ondrejmirtes merged 5 commits intophpstan:1.12.xfrom
Conversation
Collaborator
|
This pull request has been marked as ready for review. |
ondrejmirtes
requested changes
Jan 5, 2025
ondrejmirtes
requested changes
Jan 5, 2025
| } | ||
| } | ||
|
|
||
| if (count($strings) > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) { |
Member
There was a problem hiding this comment.
Ideally you'd count the combinations before generating them, it can explode as well.
ondrejmirtes
reviewed
Jan 5, 2025
| } | ||
|
|
||
| $strings[] = new ConstantStringType(implode($separatorType->getValue(), $arrayValues)); | ||
| if (count($strings) + count($arrayValues, COUNT_RECURSIVE) > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) { |
Member
There was a problem hiding this comment.
You're not counting the combinations properly. This is how I verified it:
diff --git a/src/Type/Php/ImplodeFunctionReturnTypeExtension.php b/src/Type/Php/ImplodeFunctionReturnTypeExtension.php
index 76afc963c..e45e0146a 100644
--- a/src/Type/Php/ImplodeFunctionReturnTypeExtension.php
+++ b/src/Type/Php/ImplodeFunctionReturnTypeExtension.php
@@ -22,6 +22,7 @@ use PHPStan\Type\TypeCombinator;
use function count;
use function implode;
use function in_array;
+use function iterator_count;
use const COUNT_RECURSIVE;
final class ImplodeFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
@@ -124,10 +125,16 @@ final class ImplodeFunctionReturnTypeExtension implements DynamicFunctionReturnT
$arrayValues[] = $constScalars;
}
+ var_dump(count($strings) + count($arrayValues, COUNT_RECURSIVE));
+
if (count($strings) + count($arrayValues, COUNT_RECURSIVE) > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) {
return null;
}
+ var_dump(iterator_count(CombinationsHelper::combinations($arrayValues)));
+
+ var_dump('---');
+
$combinations = CombinationsHelper::combinations($arrayValues);
foreach ($combinations as $combination) {
$strings[] = new ConstantStringType(implode($separatorType->getValue(), $combination));
The output:
int(33)
int(8)
string(3) "---"
int(41)
int(8)
string(3) "---"
Expected output:
int(8)
int(8)
string(3) "---"
int(8)
int(8)
string(3) "---"
29caa26 to
9a991c8
Compare
Member
|
Thank you! |
staabm
commented
Jan 7, 2025
|
|
||
| /** @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)); |
Contributor
Author
There was a problem hiding this comment.
this too generic type made me think about the general case and triggerd #3774
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Primary motivation: get rid of
instanceof ConstantScalarTypecloses phpstan/phpstan#11854