Skip to content

Commit

Permalink
ImplodeFunctionReturnTypeExtension - fix handling optional keys in Co…
Browse files Browse the repository at this point in the history
…nstantArrayType
  • Loading branch information
ondrejmirtes committed Mar 1, 2022
1 parent 05a28d7 commit 7f643c5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/Type/Php/ImplodeFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\Type\IntersectionType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;
use function implode;
use function in_array;
Expand Down Expand Up @@ -83,17 +84,22 @@ private function implode(Type $arrayType, Type $separatorType): Type

private function inferConstantType(ConstantArrayType $arrayType, ConstantStringType $separatorType): ?Type
{
$valueTypes = $arrayType->getValueTypes();

$arrayValues = [];
foreach ($valueTypes as $valueType) {
if (!$valueType instanceof ConstantScalarType) {
return null;
$strings = [];
foreach ($arrayType->getAllArrays() as $array) {
$valueTypes = $array->getValueTypes();

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

$strings[] = new ConstantStringType(implode($separatorType->getValue(), $arrayValues));
}

return new ConstantStringType(implode($separatorType->getValue(), $arrayValues));
return TypeCombinator::union(...$strings);
}

}
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@ public function dataFileAsserts(): iterable
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6699.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6715.php');
}

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

namespace Bug6715;

use function PHPStan\Testing\assertType;

class Test
{
/** @var bool */
public $bool;

function test(): void
{
assertType('array{test: true}', array_filter([
'test' => true,
]));

assertType('array{}', array_filter([
'test' => false,
]));

assertType('array{test?: 1}', array_filter([
'test' => rand(0, 1),
]));

assertType('array{test?: true}', array_filter([
'test' => $this->bool,
]));
}

function test2(): void
{
assertType('\'1\'', implode(', ', array_filter([
'test' => true,
])));

assertType('\'\'', implode(', ', array_filter([
'test' => false,
])));

assertType('\'\'|\'1\'', implode(', ', array_filter([
'test' => rand(0, 1),
])));

assertType('\'\'|\'1\'', implode(', ', array_filter([
'test' => $this->bool,
])));
}
}

0 comments on commit 7f643c5

Please sign in to comment.