-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(type): support $preserve_keys argument for iterator_to_array()
- Loading branch information
1 parent
9beb27e
commit bb49b3b
Showing
4 changed files
with
73 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Type/Php/IteratorToArrayFunctionReturnTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\Type; | ||
use function strtolower; | ||
|
||
final class IteratorToArrayFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
{ | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return strtolower($functionReflection->getName()) === 'iterator_to_array'; | ||
} | ||
|
||
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type | ||
{ | ||
$arguments = $functionCall->getArgs(); | ||
|
||
if ($arguments === []) { | ||
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); | ||
} | ||
|
||
$traversableType = $scope->getType($arguments[0]->value); | ||
$arrayKeyType = $traversableType->getIterableKeyType(); | ||
|
||
if (isset($arguments[1])) { | ||
$preserveKeysType = $scope->getType($arguments[1]->value); | ||
|
||
if ($preserveKeysType instanceof ConstantBooleanType && !$preserveKeysType->getValue()) { | ||
$arrayKeyType = new IntegerType(); | ||
} | ||
} | ||
|
||
return new ArrayType( | ||
$arrayKeyType, | ||
$traversableType->getIterableValueType() | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters