1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

revert: class-string should not allow callable only

As per discussion with Ondřej, `class-string` should explicitly target objects.

Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com>
This commit is contained in:
Maximilian Bösing 2023-04-05 14:51:50 +02:00
parent 0621ca6e70
commit a8ed8a7b5c
No known key found for this signature in database
GPG Key ID: 9A8988C93CEC81A3
2 changed files with 24 additions and 27 deletions

View File

@ -718,10 +718,6 @@ class TypeParser
continue;
}
if ($type instanceof TCallable) {
$type = new TCallableObject($from_docblock, $type);
}
if ($type instanceof TCallableObject) {
$types[] = new TUnknownClassString($type, false, $from_docblock);
continue;

View File

@ -92,29 +92,6 @@ final class IntersectionTypeTest extends TestCase
'assertions' => [],
'ignored_issues' => ['UnsafeInstantiation', 'MixedMethodCall'],
],
'classStringOfCallableWillBeTreatedAsCallableObject' => [
'code' => '<?php
/**
* @param class-string<callable():int> $className
*/
function takesCallableObject(string $className): int {
$object = new $className();
return $object();
}
class Foo
{
public function __invoke(): int
{
return 0;
}
}
takesCallableObject(Foo::class);
',
'assertions' => [],
'ignored_issues' => ['UnsafeInstantiation', 'MixedMethodCall'],
],
'classStringOfCallableObjectEqualsObjectWithCallableIntersection' => [
'code' => '<?php
/**
@ -211,6 +188,30 @@ final class IntersectionTypeTest extends TestCase
',
'error_message' => 'MixedMethodCall',
],
'classStringOfCallableIsNotAllowed' => [
# Ref: https://github.com/phpstan/phpstan/issues/9148
'code' => '<?php
/**
* @param class-string<callable():int> $className
*/
function takesCallableObject(string $className): int {
$object = new $className();
return $object();
}
class Foo
{
public function __invoke(): int
{
return 0;
}
}
takesCallableObject(Foo::class);
',
'error_message' => 'class-string param can only target',
'error_levels' => ['UnsafeInstantiation', 'MixedMethodCall'],
],
];
}
}