1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Merge pull request #9018 from orklah/TClosure

create proper TClosure instead of TNamedObject with a Closure value
This commit is contained in:
orklah 2022-12-28 20:53:52 +01:00 committed by GitHub
commit 435acb823c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 1 deletions

View File

@ -1246,7 +1246,7 @@ class AssertionFinder
$instanceof_class = $codebase->classlikes->getUnAliasedName($instanceof_class);
}
return [new IsType(new TNamedObject($instanceof_class))];
return [new IsType(TNamedObject::createFromName($instanceof_class))];
}
if ($this_class_name

View File

@ -384,6 +384,9 @@ abstract class Atomic implements TypeNode
case 'non-empty-mixed':
return new TNonEmptyMixed();
case 'Closure':
return new TClosure('Closure');
}
if (strpos($value, '-') && strpos($value, 'OCI-') !== 0) {

View File

@ -255,4 +255,21 @@ class TNamedObject extends Atomic
{
return ['extra_types'];
}
/**
* @param array<string, TNamedObject|TTemplateParam|TIterable|TObjectWithProperties> $extra_types
*/
public static function createFromName(
string $value,
bool $is_static = false,
bool $definite_class = false,
array $extra_types = [],
bool $from_docblock = false
): TNamedObject {
if ($value === 'Closure') {
return new TClosure($value, null, null, null, [], $extra_types, $from_docblock);
}
return new TNamedObject($value, $is_static, $definite_class, $extra_types, $from_docblock);
}
}

View File

@ -914,6 +914,26 @@ class ClosureTest extends TestCase
/** @psalm-suppress UndefinedFunction */
unknown(...);',
],
'reconcileClosure' => [
'code' => '<?php
/**
* @param Closure|callable-string $callable
*/
function use_callable($callable) : void
{
}
/**
* @param Closure|string $var
*/
function test($var) : void
{
if (is_callable($var))
use_callable($var);
else
echo $var; // $var should be string, instead it\'s considered to be Closure|string.
}',
],
];
}