New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds type specifier for settype. #2920
Changes from 2 commits
446cab4
281edfa
7a52ea3
eb36a3a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?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; | ||
use function var_dump; | ||
|
||
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; | ||
} | ||
|
||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the constant string is empty, we should return empty |
||
if (count($constantStrings) < 1) { | ||
return new SpecifiedTypes(); | ||
} | ||
|
||
$types = []; | ||
|
||
foreach ($constantStrings as $constantString) { | ||
switch ($constantString->getValue()) { | ||
case 'bool': | ||
case 'boolean': | ||
var_dump('converting to bool'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Forgot var_dump calls there |
||
$types[] = $valueType->toBoolean(); | ||
break; | ||
case 'int': | ||
case 'integer': | ||
var_dump('converting to int'); | ||
$types[] = $valueType->toInteger(); | ||
break; | ||
case 'float': | ||
case 'double': | ||
var_dump('converting to float'); | ||
$types[] = $valueType->toFloat(); | ||
break; | ||
case 'string': | ||
var_dump('converting to string'); | ||
$types[] = $valueType->toString(); | ||
break; | ||
case 'array': | ||
var_dump('converting to array'); | ||
$types[] = $valueType->toArray(); | ||
break; | ||
case 'object': | ||
var_dump('converting to object'); | ||
$types[] = new ObjectType(stdClass::class); | ||
break; | ||
case 'null': | ||
var_dump('converting to null'); | ||
$types[] = new NullType(); | ||
break; | ||
default: | ||
var_dump('defaulting'); | ||
$types[] = new ErrorType(); | ||
} | ||
} | ||
|
||
return $this->typeSpecifier->create( | ||
$value, | ||
TypeCombinator::union(...$types), | ||
TypeSpecifierContext::createTruthy(), | ||
true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No overwriting - this needs to be |
||
$scope, | ||
); | ||
} | ||
|
||
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void | ||
{ | ||
$this->typeSpecifier = $typeSpecifier; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to check that
$context->null()
.