Skip to content

Commit

Permalink
create proper TClosure instead of TNamedObject with a Closure value
Browse files Browse the repository at this point in the history
  • Loading branch information
orklah committed Dec 28, 2022
1 parent 7b8b44c commit fa4891c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
use Psalm\Type\Atomic\TClassConstant;
use Psalm\Type\Atomic\TClassString;
use Psalm\Type\Atomic\TClosedResource;
use Psalm\Type\Atomic\TClosure;
use Psalm\Type\Atomic\TEnumCase;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TKeyedArray;
Expand Down Expand Up @@ -1246,7 +1247,7 @@ protected static function getInstanceOfAssertions(
$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
Expand Down
3 changes: 3 additions & 0 deletions src/Psalm/Type/Atomic.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ private static function createInner(

case 'non-empty-mixed':
return new TNonEmptyMixed();

case 'Closure':
return new TClosure('Closure');
}

if (strpos($value, '-') && strpos($value, 'OCI-') !== 0) {
Expand Down
17 changes: 17 additions & 0 deletions src/Psalm/Type/Atomic/TNamedObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,21 @@ protected function getChildNodeKeys(): array
{
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);
}
}
20 changes: 20 additions & 0 deletions tests/ClosureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,26 @@ private function handler(): void {
/** @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.
}',
],
];
}

Expand Down

0 comments on commit fa4891c

Please sign in to comment.