From 5f59f07163f737bcd0b193f29f3d78ff3288342c Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Fri, 24 Feb 2023 02:19:21 -0400 Subject: [PATCH] Allow `array` as magic method name Fixes vimeo/psalm#9321 --- .../PhpVisitor/Reflector/ClassLikeDocblockParser.php | 7 ++++++- src/Psalm/Internal/Type/ParseTreeCreator.php | 3 +-- tests/MagicMethodAnnotationTest.php | 8 ++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeDocblockParser.php b/src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeDocblockParser.php index 59805870186..38f524b668d 100644 --- a/src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeDocblockParser.php +++ b/src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeDocblockParser.php @@ -356,7 +356,12 @@ public static function parse( $method_tree = $parse_tree_creator->create(); } catch (TypeParseTreeException $e) { - throw new DocblockParseException($method_entry . ' is not a valid method'); + throw new DocblockParseException( + $method_entry . ' is not a valid method: ' + . $e->getMessage(), + 0, + $e, + ); } if (!$method_tree instanceof MethodWithReturnTypeTree diff --git a/src/Psalm/Internal/Type/ParseTreeCreator.php b/src/Psalm/Internal/Type/ParseTreeCreator.php index 9449448a8ee..34502e118cb 100644 --- a/src/Psalm/Internal/Type/ParseTreeCreator.php +++ b/src/Psalm/Internal/Type/ParseTreeCreator.php @@ -780,8 +780,7 @@ private function handleValue(array $type_token): void $type_token[0], $new_parent, ); - } elseif ($type_token[0] !== 'array' - && $type_token[0][0] !== '\\' + } elseif ($type_token[0][0] !== '\\' && $this->current_leaf instanceof Root ) { $new_leaf = new MethodTree( diff --git a/tests/MagicMethodAnnotationTest.php b/tests/MagicMethodAnnotationTest.php index 9351ea511f5..197b9fc62b1 100644 --- a/tests/MagicMethodAnnotationTest.php +++ b/tests/MagicMethodAnnotationTest.php @@ -939,6 +939,14 @@ class D extends C {} '$e' => 'B', ], ], + 'arrayAsMethodName' => [ + 'code' => <<<'PHP' +