Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions src/Type/Php/ImplodeFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ public function getTypeFromFunctionCall(
private function implode(Type $arrayType, Type $separatorType): Type
{
if (count($arrayType->getConstantArrays()) > 0 && count($separatorType->getConstantStrings()) > 0) {
$isNonEmpty = $arrayType->isIterableAtLeastOnce()->yes();
$result = [];
foreach ($separatorType->getConstantStrings() as $separator) {
foreach ($arrayType->getConstantArrays() as $constantArray) {
$constantType = $this->inferConstantType($constantArray, $separator);
$constantType = $this->inferConstantType($constantArray, $separator, $isNonEmpty);
if ($constantType !== null) {
$result[] = $constantType;
continue;
Expand Down Expand Up @@ -110,7 +111,7 @@ private function implode(Type $arrayType, Type $separatorType): Type
return new StringType();
}

private function inferConstantType(ConstantArrayType $arrayType, ConstantStringType $separatorType): ?Type
private function inferConstantType(ConstantArrayType $arrayType, ConstantStringType $separatorType, bool $isNonEmpty): ?Type
{
$sep = $separatorType->getValue();
$valueTypes = $arrayType->getValueTypes();
Expand Down Expand Up @@ -150,9 +151,16 @@ private function inferConstantType(ConstantArrayType $arrayType, ConstantStringT

$strings = [];
foreach ($partials as $partial) {
if ($partial === [] && $isNonEmpty) {
continue;
}
$strings[] = new ConstantStringType(implode($sep, $partial));
}

if ($strings === []) {
return null;
}

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

Expand Down
81 changes: 81 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14558.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types = 1);

namespace Bug14558;

use function PHPStan\Testing\assertType;

/** @return non-negative-int */
function num_types(string $g): int { return 0; }

/** @return non-empty-list<non-empty-string> */
function get_sort_keys(mixed ...$args): array { return ['a']; }

function cond(int $i): bool { return true; }


// Playground 1: with outer foreach loop
function test1(): void
{
$cols_cat = [ ];

foreach ([ 'PrV', 'PrA', 'Acc' ] as $g) {
$num_types = num_types($g);
for ($i = 1; $i <= $num_types; $i++) {

if (cond($i)) {

$k = 0;
$tmp_sort_keys = [ ];
foreach (get_sort_keys($g, $i) as $ce_tri) {
$k++;
$tmp_sort_alias = "Tri{$k}_Cat_{$g}{$i}";
$tmp_sort_keys[$tmp_sort_alias] = $tmp_sort_alias;
}
assertType('non-falsy-string', implode(',', $tmp_sort_keys));
$cols_cat[] = [
'g' => $g
, 't' => $i
, 's' => implode(',', $tmp_sort_keys)
];

}
}

}

assertType('list<array{g: \'Acc\'|\'PrA\'|\'PrV\', t: int<1, max>, s: non-falsy-string}>', $cols_cat);
}

// Playground 2: without outer foreach loop
function test2(): void
{
$cols_cat = [ ];

$g = 'PrV';
$num_types = num_types($g);
for ($i = 1; $i <= $num_types; $i++) {

if (cond($i)) {

$k = 0;
$tmp_sort_keys = [ ];
foreach (get_sort_keys($g, $i) as $ce_tri) {
$k++;
$tmp_sort_alias = "Tri{$k}_Cat_{$g}{$i}";
$tmp_sort_keys[$tmp_sort_alias] = $tmp_sort_alias;
}

assertType('non-falsy-string', implode(',', $tmp_sort_keys));
$cols_cat[] = [
'g' => $g
, 't' => $i
, 's' => implode(',', $tmp_sort_keys)
];

}
}

assertType('list<array{g: \'PrV\', t: int<1, max>, s: non-falsy-string}>', $cols_cat);
}
Loading