Skip to content
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

implement gettype() inference in TypeSpecifier #1611

Merged
merged 3 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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\ConstantArrayType;
Expand All @@ -52,6 +53,7 @@
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;
Expand Down Expand Up @@ -490,6 +492,62 @@ public function specifyTypesInCondition(
);
}

$getttypeType = static function (ConstantStringType $constantStringType) {
$type = null;
if ($constantStringType->getValue() === 'string') {
$type = new StringType();
}
if ($constantStringType->getValue() === 'array') {
$type = new ArrayType(new MixedType(), new MixedType());
}
if ($constantStringType->getValue() === 'boolean') {
$type = new BooleanType();
}
if ($constantStringType->getValue() === 'resource' || $constantStringType->getValue() === 'resource (closed)') {
$type = new ResourceType();
}
if ($constantStringType->getValue() === 'integer') {
$type = new IntegerType();
}
if ($constantStringType->getValue() === 'double') {
$type = new FloatType();
}
if ($constantStringType->getValue() === 'NULL') {
$type = new NullType();
}
if ($constantStringType->getValue() === 'object') {
$type = new ObjectWithoutClassType();
}
return $type;
};

if (
$expr->left instanceof FuncCall
&& $expr->left->name instanceof Name
&& strtolower($expr->left->name->toString()) === 'gettype'
&& isset($expr->left->getArgs()[0])
&& $rightType instanceof ConstantStringType
) {
$type = $getttypeType($rightType);

if ($type !== null) {
return $this->create($expr->left->getArgs()[0]->value, $type, $context, false, $scope, $rootExpr);
}
}
if (
$expr->right instanceof FuncCall
&& $expr->right->name instanceof Name
&& strtolower($expr->right->name->toString()) === 'gettype'
&& isset($expr->right->getArgs()[0])
&& $leftType instanceof ConstantStringType
) {
$type = $getttypeType($leftType);

if ($type !== null) {
return $this->create($expr->right->getArgs()[0]->value, $type, $context, false, $scope, $rootExpr);
}
}

$stringType = new StringType();
$integerType = new IntegerType();
$floatType = new FloatType();
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Variables/data/bug-7417.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Arrays/data/bug-7469.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Variables/data/bug-3391.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6901.php');
}

/**
Expand Down
57 changes: 57 additions & 0 deletions tests/PHPStan/Analyser/data/bug-6901.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Bug6901;

use function PHPStan\Testing\assertType;

/**
* @param integer|string|array<string>|bool $y
* @return integer
*/
function foo($y)
{
switch (gettype($y)) {
case "integer":
assertType('int', $y);
break;
case "string":
assertType('string', $y);
break;
case "boolean":
assertType('bool', $y);
break;
case "array":
assertType('array<string>', $y);
break;
default:
assertType('*NEVER*', $y);
}
assertType('array<string>|bool|int|string', $y);
return 0;
}

/**
* @param object|float|null|resource $y
* @return integer
*/
function bar($y)
{
switch (gettype($y)) {
case "object":
assertType('object', $y);
break;
case "double":
assertType('float', $y);
break;
case "NULL":
assertType('null', $y);
break;
case "resource":
assertType('resource', $y);
break;
default:
assertType('*NEVER*', $y);
}
assertType('float|object|resource|null', $y);
return 0;
}