From a8ed8a7b5c1e544850911841edecf36596d62aa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Wed, 5 Apr 2023 14:51:50 +0200 Subject: [PATCH] revert: `class-string` should not allow `callable` only MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As per discussion with Ondřej, `class-string` should explicitly target objects. Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com> --- src/Psalm/Internal/Type/TypeParser.php | 4 --- tests/IntersectionTypeTest.php | 47 +++++++++++++------------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/src/Psalm/Internal/Type/TypeParser.php b/src/Psalm/Internal/Type/TypeParser.php index df330c89b..443b18505 100644 --- a/src/Psalm/Internal/Type/TypeParser.php +++ b/src/Psalm/Internal/Type/TypeParser.php @@ -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; diff --git a/tests/IntersectionTypeTest.php b/tests/IntersectionTypeTest.php index 655818067..acf90438c 100644 --- a/tests/IntersectionTypeTest.php +++ b/tests/IntersectionTypeTest.php @@ -92,29 +92,6 @@ final class IntersectionTypeTest extends TestCase 'assertions' => [], 'ignored_issues' => ['UnsafeInstantiation', 'MixedMethodCall'], ], - 'classStringOfCallableWillBeTreatedAsCallableObject' => [ - 'code' => ' $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' => ' 'MixedMethodCall', ], + 'classStringOfCallableIsNotAllowed' => [ + # Ref: https://github.com/phpstan/phpstan/issues/9148 + 'code' => ' $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'], + ], ]; } }