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
30 changes: 28 additions & 2 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,32 @@ public function getKeyTypes(): array
return $this->keyTypes;
}

public function getFirstKeyType(): Type
{
$keyTypes = [];
foreach ($this->keyTypes as $i => $keyType) {
$keyTypes[] = $keyType;
if (!$this->isOptionalKey($i)) {
break;
}
}

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

public function getLastKeyType(): Type
{
$keyTypes = [];
for ($i = count($this->keyTypes) - 1; $i >= 0; $i--) {
$keyTypes[] = $this->keyTypes[$i];
if (!$this->isOptionalKey($i)) {
break;
}
}

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

/**
* @return array<int, Type>
*/
Expand All @@ -223,8 +249,8 @@ public function getValueTypes(): array
public function getFirstValueType(): Type
{
$valueTypes = [];
for ($i = 0, $keyTypesCount = count($this->keyTypes); $i < $keyTypesCount; $i++) {
$valueTypes[] = $this->valueTypes[$i];
foreach ($this->valueTypes as $i => $valueType) {
$valueTypes[] = $valueType;
if (!$this->isOptionalKey($i)) {
break;
}
Expand Down
7 changes: 3 additions & 4 deletions src/Type/Php/ArrayKeyFirstDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,16 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
return new NullType();
}

$constantArrays = TypeUtils::getConstantArrays($argType);
$constantArrays = TypeUtils::getOldConstantArrays($argType);
if (count($constantArrays) > 0) {
$keyTypes = [];
foreach ($constantArrays as $constantArray) {
$arrayKeyTypes = $constantArray->getKeyTypes();
if (count($arrayKeyTypes) === 0) {
if ($constantArray->isEmpty()) {
$keyTypes[] = new NullType();
continue;
}

$keyTypes[] = $arrayKeyTypes[0];
$keyTypes[] = $constantArray->getFirstKeyType();
}

return TypeCombinator::union(...$keyTypes);
Expand Down
7 changes: 3 additions & 4 deletions src/Type/Php/ArrayKeyLastDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,16 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
return new NullType();
}

$constantArrays = TypeUtils::getConstantArrays($argType);
$constantArrays = TypeUtils::getOldConstantArrays($argType);
if (count($constantArrays) > 0) {
$keyTypes = [];
foreach ($constantArrays as $constantArray) {
$arrayKeyTypes = $constantArray->getKeyTypes();
if (count($arrayKeyTypes) === 0) {
if ($constantArray->isEmpty()) {
$keyTypes[] = new NullType();
continue;
}

$keyTypes[] = $arrayKeyTypes[count($arrayKeyTypes) - 1];
$keyTypes[] = $constantArray->getLastKeyType();
}

return TypeCombinator::union(...$keyTypes);
Expand Down
24 changes: 24 additions & 0 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8772,6 +8772,30 @@ public function dataPhp73Functions(): array
'2|3',
'array_key_last($anotherLiteralArray)',
],
[
"'a'|'b'",
'array_key_first($constantArrayOptionalKeys1)',
],
[
"'c'",
'array_key_last($constantArrayOptionalKeys1)',
],
[
"'a'",
'array_key_first($constantArrayOptionalKeys2)',
],
[
"'c'",
'array_key_last($constantArrayOptionalKeys2)',
],
[
"'a'",
'array_key_first($constantArrayOptionalKeys3)',
],
[
"'b'|'c'",
'array_key_last($constantArrayOptionalKeys3)',
],
[
'array{int, int}',
'$hrtime1',
Expand Down
8 changes: 7 additions & 1 deletion tests/PHPStan/Analyser/data/php73_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ class Foo
* @param array $mixedArray
* @param array $nonEmptyArray
* @param array<string, mixed> $arrayWithStringKeys
* @param array{a?: 0, b: 1, c: 2} $constantArrayOptionalKeys1
* @param array{a: 0, b?: 1, c: 2} $constantArrayOptionalKeys2
* @param array{a: 0, b: 1, c?: 2} $constantArrayOptionalKeys3
*/
public function doFoo(
$mixed,
int $integer,
array $mixedArray,
array $nonEmptyArray,
array $arrayWithStringKeys
array $arrayWithStringKeys,
array $constantArrayOptionalKeys1,
array $constantArrayOptionalKeys2,
array $constantArrayOptionalKeys3
)
{
if (count($nonEmptyArray) === 0) {
Expand Down