-
Notifications
You must be signed in to change notification settings - Fork 484
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dynamic return type extension for understanding compact function
- Loading branch information
1 parent
e92a5ab
commit 2a75a25
Showing
5 changed files
with
119 additions
and
1 deletion.
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
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,85 @@ | ||
<?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\Constant\ConstantArrayType; | ||
use PHPStan\Type\Constant\ConstantArrayTypeBuilder; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
use PHPStan\Type\Type; | ||
|
||
class CompactFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
{ | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return $functionReflection->getName() === 'compact'; | ||
} | ||
|
||
public function getTypeFromFunctionCall( | ||
FunctionReflection $functionReflection, | ||
FuncCall $functionCall, | ||
Scope $scope | ||
): Type | ||
{ | ||
$defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); | ||
if (count($functionCall->args) === 0) { | ||
return $defaultReturnType; | ||
} | ||
|
||
if ($scope->canAnyVariableExist()) { | ||
return $defaultReturnType; | ||
} | ||
|
||
$array = ConstantArrayTypeBuilder::createEmpty(); | ||
foreach ($functionCall->args as $arg) { | ||
$type = $scope->getType($arg->value); | ||
$constantStrings = $this->findConstantStrings($type); | ||
if ($constantStrings === null) { | ||
return $defaultReturnType; | ||
} | ||
foreach ($constantStrings as $constantString) { | ||
$has = $scope->hasVariableType($constantString->getValue()); | ||
if ($has->no()) { | ||
continue; | ||
} | ||
|
||
$array->setOffsetValueType($constantString, $scope->getVariableType($constantString->getValue()), $has->maybe()); | ||
} | ||
} | ||
|
||
return $array->getArray(); | ||
} | ||
|
||
/** | ||
* @param Type $type | ||
* @return array<int, ConstantStringType>|null | ||
*/ | ||
private function findConstantStrings(Type $type): ?array | ||
{ | ||
if ($type instanceof ConstantStringType) { | ||
return [$type]; | ||
} | ||
|
||
if ($type instanceof ConstantArrayType) { | ||
$result = []; | ||
foreach ($type->getValueTypes() as $valueType) { | ||
$constantStrings = $this->findConstantStrings($valueType); | ||
if ($constantStrings === null) { | ||
return null; | ||
} | ||
|
||
$result = array_merge($result, $constantStrings); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace CompactExtension; | ||
|
||
use function PHPStan\Analyser\assertType; | ||
|
||
assertType('array<string, mixed>', compact(['foo' => 'bar'])); | ||
|
||
function (string $dolor): void { | ||
$foo = 'bar'; | ||
$bar = 'baz'; | ||
if (rand(0, 1)) { | ||
$lorem = 'ipsum'; | ||
} | ||
assertType('array(\'foo\' => \'bar\', \'bar\' => \'baz\')', compact('foo', ['bar'])); | ||
assertType('array(\'foo\' => \'bar\', \'bar\' => \'baz\', ?\'lorem\' => \'ipsum\')', compact([['foo']], 'bar', 'lorem')); | ||
|
||
assertType('array<string, mixed>', compact($dolor)); | ||
assertType('array<string, mixed>', compact([$dolor])); | ||
|
||
assertType('array()', compact([])); | ||
}; |