Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1362,11 +1362,6 @@ parameters:
count: 1
path: src/Type/Php/FunctionExistsFunctionTypeSpecifyingExtension.php

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
count: 1
path: src/Type/Php/ImplodeFunctionReturnTypeExtension.php

-
message: """
#^Call to deprecated method getConstantScalars\\(\\) of class PHPStan\\\\Type\\\\TypeUtils\\:
Expand Down
18 changes: 14 additions & 4 deletions src/Type/Php/ImplodeFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +22,7 @@
use function count;
use function implode;
use function in_array;
use const COUNT_RECURSIVE;

final class ImplodeFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
Expand Down Expand Up @@ -115,13 +117,21 @@ private function inferConstantType(ConstantArrayType $arrayType, ConstantStringT

$arrayValues = [];
foreach ($valueTypes as $valueType) {
if (!$valueType instanceof ConstantScalarType) {
$constScalars = $valueType->getConstantScalarValues();
if (count($constScalars) === 0) {
return null;
}
$arrayValues[] = $valueType->getValue();
$arrayValues[] = $constScalars;
}

$strings[] = new ConstantStringType(implode($separatorType->getValue(), $arrayValues));
if (count($strings) + count($arrayValues, COUNT_RECURSIVE) > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) "---"

return null;
}

$combinations = CombinationsHelper::combinations($arrayValues);
foreach ($combinations as $combination) {
$strings[] = new ConstantStringType(implode($separatorType->getValue(), $combination));
}
}

return TypeCombinator::union(...$strings);
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-11854.php
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));
}
}
30 changes: 30 additions & 0 deletions tests/PHPStan/Analyser/nsrt/implode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

}
}
Loading