-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?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\TypeSpecifierContext; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Type\ErrorType; | ||
use PHPStan\Type\FunctionTypeSpecifyingExtension; | ||
use PHPStan\Type\NullType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\TypeCombinator; | ||
use stdClass; | ||
use function count; | ||
use function strtolower; | ||
|
||
class SetTypeFunctionTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension | ||
{ | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection, FuncCall $node, TypeSpecifierContext $context): bool | ||
{ | ||
return strtolower($functionReflection->getName()) === 'settype' | ||
&& count($node->getArgs()) > 1; | ||
} | ||
|
||
public function specifyTypes(FunctionReflection $functionReflection, FuncCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes | ||
{ | ||
$value = $node->getArgs()[0]->value; | ||
$valueType = $scope->getType($value); | ||
$castType = $scope->getType($node->getArgs()[1]->value); | ||
|
||
$constantStrings = $castType->getConstantStrings(); | ||
$types = []; | ||
|
||
foreach ($constantStrings as $constantString) { | ||
$types[] = match ($constantString->getValue()) { | ||
'bool', 'boolean' => $valueType->toBoolean(), | ||
'int', 'integer' => $valueType->toInteger(), | ||
'float', 'double' => $valueType->toFloat(), | ||
'string' => $valueType->toString(), | ||
'array' => $valueType->toArray(), | ||
'object' => new ObjectType(stdClass::class), | ||
'null' => new NullType(), | ||
default => new ErrorType() | ||
}; | ||
} | ||
|
||
return new SpecifiedTypes([[$value, TypeCombinator::union(...$types)]], [], true); | ||
Check failure on line 50 in src/Type/Php/SetTypeFunctionTypeSpecifyingExtension.php GitHub Actions / PHPStan with result cache (8.1)
Check failure on line 50 in src/Type/Php/SetTypeFunctionTypeSpecifyingExtension.php GitHub Actions / PHPStan with result cache (8.3)
Check failure on line 50 in src/Type/Php/SetTypeFunctionTypeSpecifyingExtension.php GitHub Actions / PHPStan with result cache (8.2)
Check failure on line 50 in src/Type/Php/SetTypeFunctionTypeSpecifyingExtension.php GitHub Actions / PHPStan (8.2, ubuntu-latest)
Check failure on line 50 in src/Type/Php/SetTypeFunctionTypeSpecifyingExtension.php GitHub Actions / PHPStan (8.0, ubuntu-latest)
Check failure on line 50 in src/Type/Php/SetTypeFunctionTypeSpecifyingExtension.php GitHub Actions / PHPStan (8.1, ubuntu-latest)
Check failure on line 50 in src/Type/Php/SetTypeFunctionTypeSpecifyingExtension.php GitHub Actions / PHPStan (8.3, ubuntu-latest)
Check failure on line 50 in src/Type/Php/SetTypeFunctionTypeSpecifyingExtension.php GitHub Actions / PHPStan (8.2, windows-latest)
Check failure on line 50 in src/Type/Php/SetTypeFunctionTypeSpecifyingExtension.php GitHub Actions / PHPStan (8.3, windows-latest)
Check failure on line 50 in src/Type/Php/SetTypeFunctionTypeSpecifyingExtension.php GitHub Actions / PHPStan (8.1, windows-latest)
Check failure on line 50 in src/Type/Php/SetTypeFunctionTypeSpecifyingExtension.php GitHub Actions / PHPStan (8.0, windows-latest)
|
||
} | ||
|
||
} |