-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds type-specifying extension for settype()
- Loading branch information
1 parent
b026126
commit d2c30d1
Showing
4 changed files
with
770 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?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\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, TypeSpecifierAwareExtension | ||
{ | ||
|
||
private TypeSpecifier $typeSpecifier; | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection, FuncCall $node, TypeSpecifierContext $context): bool | ||
{ | ||
return strtolower($functionReflection->getName()) === 'settype' | ||
&& count($node->getArgs()) > 1 | ||
&& $context->null(); | ||
} | ||
|
||
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(); | ||
if (count($constantStrings) < 1) { | ||
return new SpecifiedTypes(); | ||
} | ||
|
||
$types = []; | ||
|
||
foreach ($constantStrings as $constantString) { | ||
switch ($constantString->getValue()) { | ||
case 'bool': | ||
case 'boolean': | ||
$types[] = $valueType->toBoolean(); | ||
break; | ||
case 'int': | ||
case 'integer': | ||
$types[] = $valueType->toInteger(); | ||
break; | ||
case 'float': | ||
case 'double': | ||
$types[] = $valueType->toFloat(); | ||
break; | ||
case 'string': | ||
$types[] = $valueType->toString(); | ||
break; | ||
case 'array': | ||
$types[] = $valueType->toArray(); | ||
break; | ||
case 'object': | ||
$types[] = new ObjectType(stdClass::class); | ||
break; | ||
case 'null': | ||
$types[] = new NullType(); | ||
break; | ||
default: | ||
$types[] = new ErrorType(); | ||
} | ||
} | ||
|
||
return $this->typeSpecifier->create( | ||
$value, | ||
TypeCombinator::union(...$types), | ||
TypeSpecifierContext::createTruthy(), | ||
true, | ||
$scope, | ||
); | ||
} | ||
|
||
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void | ||
{ | ||
$this->typeSpecifier = $typeSpecifier; | ||
} | ||
|
||
} |
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
Oops, something went wrong.