Skip to content
Closed
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
59 changes: 59 additions & 0 deletions src/Type/Php/ArrayKeyFirstLastTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use function in_array;
use function strtolower;

#[AutowiredService]
final class ArrayKeyFirstLastTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension
{

private TypeSpecifier $typeSpecifier;

public function isFunctionSupported(
FunctionReflection $functionReflection,
FuncCall $node,

Check failure on line 26 in src/Type/Php/ArrayKeyFirstLastTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Call to function in_array() requires parameter #3 to be set.

Check failure on line 26 in src/Type/Php/ArrayKeyFirstLastTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Call to function in_array() requires parameter #3 to be set.
TypeSpecifierContext $context,
): bool
{
return in_array(strtolower($functionReflection->getName()), ['array_key_first', 'array_key_last'])

Check failure on line 30 in src/Type/Php/ArrayKeyFirstLastTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Call to function in_array() requires parameter #3 to be set.

Check failure on line 30 in src/Type/Php/ArrayKeyFirstLastTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.5)

Call to function in_array() requires parameter #3 to be set.

Check failure on line 30 in src/Type/Php/ArrayKeyFirstLastTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Call to function in_array() requires parameter #3 to be set.

Check failure on line 30 in src/Type/Php/ArrayKeyFirstLastTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, ubuntu-latest)

Call to function in_array() requires parameter #3 to be set.

Check failure on line 30 in src/Type/Php/ArrayKeyFirstLastTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Call to function in_array() requires parameter #3 to be set.

Check failure on line 30 in src/Type/Php/ArrayKeyFirstLastTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Call to function in_array() requires parameter #3 to be set.

Check failure on line 30 in src/Type/Php/ArrayKeyFirstLastTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Call to function in_array() requires parameter #3 to be set.

Check failure on line 30 in src/Type/Php/ArrayKeyFirstLastTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Call to function in_array() requires parameter #3 to be set.

Check failure on line 30 in src/Type/Php/ArrayKeyFirstLastTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Call to function in_array() requires parameter #3 to be set.

Check failure on line 30 in src/Type/Php/ArrayKeyFirstLastTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Call to function in_array() requires parameter #3 to be set.

Check failure on line 30 in src/Type/Php/ArrayKeyFirstLastTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Call to function in_array() requires parameter #3 to be set.

Check failure on line 30 in src/Type/Php/ArrayKeyFirstLastTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, windows-latest)

Call to function in_array() requires parameter #3 to be set.

Check failure on line 30 in src/Type/Php/ArrayKeyFirstLastTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Call to function in_array() requires parameter #3 to be set.

Check failure on line 30 in src/Type/Php/ArrayKeyFirstLastTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Call to function in_array() requires parameter #3 to be set.

Check failure on line 30 in src/Type/Php/ArrayKeyFirstLastTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Call to function in_array() requires parameter #3 to be set.

Check failure on line 30 in src/Type/Php/ArrayKeyFirstLastTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Call to function in_array() requires parameter #3 to be set.
&& $context->true();
}

public function specifyTypes(
FunctionReflection $functionReflection,
FuncCall $node,
Scope $scope,
TypeSpecifierContext $context,
): SpecifiedTypes
{
$arrayArg = $node->getArgs()[0]->value ?? null;
if ($arrayArg === null) {
return new SpecifiedTypes();
}

return $this->typeSpecifier->create(
$arrayArg,
new NonEmptyArrayType(),
$context,
$scope,
);
}

public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
{
$this->typeSpecifier = $typeSpecifier;
}

}
31 changes: 31 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14081.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types = 1);

namespace Bug14081;

use function PHPStan\Testing\assertType;

/** @param list<string> $array */
function first(array $array): mixed
{
if (($key = array_key_first($array))) {
assertType('int<1, max>', $key);
assertType('non-empty-list<string>', $array);
assertType('string', $array[$key]);
return $array[$key];
}
return null;
}

/** @param list<string> $array */
function last(array $array): mixed
{
if (($key = array_key_last($array))) {
assertType('int<1, max>', $key);
assertType('non-empty-list<string>', $array);
assertType('string', $array[$key]);
return $array[$key];
}
return null;
}
Loading