1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Add support for special typeof types (used in get_class checks)

This commit is contained in:
Matthew Brown 2016-08-09 18:10:46 -04:00
parent 02ce85e383
commit 24d67e9615
2 changed files with 36 additions and 11 deletions

View File

@ -2909,21 +2909,14 @@ class StatementsChecker
{
$type_candidate_var = null;
if ($stmt->cond instanceof PhpParser\Node\Expr\FuncCall &&
$stmt->cond->name instanceof PhpParser\Node\Name &&
$stmt->cond->name->parts === ['get_class']) {
$var = $stmt->cond->args[0]->value;
if ($var instanceof PhpParser\Node\Expr\Variable && is_string($var->name)) {
$type_candidate_var = $var->name;
}
}
if ($this->_checkExpression($stmt->cond, $context) === false) {
return false;
}
if (isset($stmt->cond->inferredType) && array_values($stmt->cond->inferredType->types)[0] instanceof Type\T) {
$type_candidate_var = array_values($stmt->cond->inferredType->types)[0]->typeof;
}
$original_context = clone $context;
$case_types = [];
@ -3239,6 +3232,14 @@ class StatementsChecker
}
}
}
if ($stmt->name instanceof PhpParser\Node\Name && $stmt->name->parts === ['get_class']) {
$var = $stmt->args[0]->value;
if ($var instanceof PhpParser\Node\Expr\Variable && is_string($var->name)) {
$stmt->inferredType = new Type\Union([new Type\T($var->name)]);
}
}
}
/**

24
src/Psalm/Type/T.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace Psalm\Type;
use Psalm\Type;
class T extends Atomic
{
/**
* Used to hold information as to what this refers to
* @var string
*/
public $typeof;
/**
* @param string $typeof the variable id
*/
public function __construct($typeof)
{
$this->value = 'string';
$this->typeof = $typeof;
}
}