Skip to content

Commit

Permalink
Restore src/Analyser/TypeSpecifier.php
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Dec 18, 2022
1 parent 653b3bf commit 690a128
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
30 changes: 29 additions & 1 deletion src/Analyser/TypeSpecifier.php
Expand Up @@ -31,6 +31,8 @@
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\HasPropertyType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\ConditionalTypeForParameter;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantBooleanType;
Expand All @@ -54,9 +56,11 @@
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\ResourceType;
use PHPStan\Type\StaticMethodTypeSpecifyingExtension;
use PHPStan\Type\StaticType;
use PHPStan\Type\StaticTypeFactory;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeTraverser;
Expand Down Expand Up @@ -1076,7 +1080,31 @@ private function specifyTypesForConstantStringBinaryExpression(
&& strtolower($exprNode->name->toString()) === 'gettype'
&& isset($exprNode->getArgs()[0])
) {
$type = GetTypeHelper::typeFromString($constantType->getValue());
$type = null;
if ($constantType->getValue() === 'string') {
$type = new StringType();
}
if ($constantType->getValue() === 'array') {
$type = new ArrayType(new MixedType(), new MixedType());
}
if ($constantType->getValue() === 'boolean') {
$type = new BooleanType();
}
if ($constantType->getValue() === 'resource' || $constantType->getValue() === 'resource (closed)') {
$type = new ResourceType();
}
if ($constantType->getValue() === 'integer') {
$type = new IntegerType();
}
if ($constantType->getValue() === 'double') {
$type = new FloatType();
}
if ($constantType->getValue() === 'NULL') {
$type = new NullType();
}
if ($constantType->getValue() === 'object') {
$type = new ObjectWithoutClassType();
}

if ($type !== null) {
return $this->create($exprNode->getArgs()[0]->value, $type, $context, false, $scope, $rootExpr);
Expand Down
10 changes: 6 additions & 4 deletions src/Reflection/InitializerExprTypeResolver.php
Expand Up @@ -6,6 +6,8 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\DNumber;
Expand Down Expand Up @@ -105,7 +107,7 @@ public function getType(Expr $expr, InitializerExprContext $context): Type
if ($expr instanceof String_) {
return new ConstantStringType($expr->value);
}
if ($expr instanceof Expr\ConstFetch) {
if ($expr instanceof ConstFetch) {
$constName = (string) $expr->name;
$loweredConstName = strtolower($constName);
if ($loweredConstName === 'true') {
Expand Down Expand Up @@ -157,7 +159,7 @@ public function getType(Expr $expr, InitializerExprContext $context): Type
$dim = $this->getType($expr->dim, $context);
return $var->getOffsetValueType($dim);
}
if ($expr instanceof Expr\ClassConstFetch && $expr->name instanceof Identifier) {
if ($expr instanceof ClassConstFetch && $expr->name instanceof Identifier) {
return $this->getClassConstFetchType($expr->class, $expr->name->toString(), $context->getClassName(), fn (Expr $expr): Type => $this->getType($expr, $context));
}
if ($expr instanceof Expr\UnaryPlus) {
Expand Down Expand Up @@ -538,7 +540,7 @@ private function builtDegradedConstantArray(Array_ $node, callable $getTypeCallb
$isList = false;

$itemKeyType = $getTypeCallback($key);
if (!$itemKeyType instanceof ConstantScalarType) {
if (!$itemKeyType instanceof ConstantScalarType || $key instanceof ClassConstFetch) {
throw new OversizedConstantArrayTypeException();
}
if (!$itemKeyType->isNonEmptyString()->yes()) {
Expand All @@ -557,7 +559,7 @@ private function builtDegradedConstantArray(Array_ $node, callable $getTypeCallb
}

$itemValueType = $getTypeCallback($value);
if (!$itemValueType instanceof ConstantScalarType) {
if (!$itemValueType instanceof ConstantScalarType || $value instanceof ClassConstFetch) {
throw new OversizedConstantArrayTypeException();
}
if (!$itemValueType->isNonEmptyString()->yes()) {
Expand Down

0 comments on commit 690a128

Please sign in to comment.