1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Add better support for get_debug_type

This commit is contained in:
Matt Brown 2020-10-14 17:30:08 -04:00 committed by Daniil Gentili
parent 0f8b017384
commit c00bc4ee51
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
3 changed files with 34 additions and 2 deletions

View File

@ -2725,7 +2725,8 @@ class AssertionFinder
&& $conditional->right->name instanceof PhpParser\Node\Name
&& strtolower($conditional->right->name->parts[0]) === 'get_debug_type'
&& $conditional->right->args
&& $conditional->left instanceof PhpParser\Node\Scalar\String_
&& ($conditional->left instanceof PhpParser\Node\Scalar\String_
|| $conditional->left instanceof PhpParser\Node\Expr\ClassConstFetch)
) {
return self::ASSIGNMENT_TO_RIGHT;
}
@ -2734,7 +2735,8 @@ class AssertionFinder
&& $conditional->left->name instanceof PhpParser\Node\Name
&& strtolower($conditional->left->name->parts[0]) === 'get_debug_type'
&& $conditional->left->args
&& $conditional->right instanceof PhpParser\Node\Scalar\String_
&& ($conditional->right instanceof PhpParser\Node\Scalar\String_
|| $conditional->right instanceof PhpParser\Node\Expr\ClassConstFetch)
) {
return self::ASSIGNMENT_TO_LEFT;
}

View File

@ -16,6 +16,7 @@ use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TTemplateParam;
use Psalm\Type\Atomic\TTemplateParamClass;
use Psalm\Type\Atomic\TDependentGetClass;
use Psalm\Type\Atomic\TDependentGetDebugType;
use Psalm\Type\Atomic\TDependentGetType;
use Psalm\Type\Atomic\THtmlEscapedString;
use Psalm\Type\Atomic\TInt;
@ -151,6 +152,18 @@ class ScalarTypeComparator
}
}
if ($container_type_part instanceof TDependentGetDebugType) {
if ($container_type_part instanceof TClassString || $container_type_part instanceof TLiteralClassString) {
return true;
}
return $input_type_part instanceof TString;
}
if ($input_type_part instanceof TDependentGetDebugType) {
$input_type_part = new TString();
}
if ($container_type_part instanceof TDependentGetType) {
$container_type_part = new TString();

View File

@ -1379,6 +1379,23 @@ class FunctionCallTest extends TestCase
return mb_convert_encoding($str, "UTF-8", "UTF-8");
}'
],
'getDebugType' => [
'<?php
function foo(mixed $var) : void {
switch (get_debug_type($var)) {
case "string":
echo "a string";
break;
case Exception::class;
echo "an Exception with message " . $var->getMessage();
break;
}
}',
[],
[],
'8.0'
],
];
}