From 5975d8d033922dd34f38e0762d0a302c6f10fc56 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Thu, 5 Mar 2020 12:14:38 +0100 Subject: [PATCH] Fix the handling of invalid phpdoc tags Such tags are represented by an InvalidTag instance rather than a Method tag, but the name might still be `method`. As we want to ignore invalid tags (that's the reason to catch exceptions), these tags are filtered out. --- fixtures/WithPhpdocClass.php | 17 ++++++++++ .../PhpDocumentor/ClassTagRetriever.php | 10 +++++- .../Doubler/ClassPatch/MagicCallPatchTest.php | 32 +++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 fixtures/WithPhpdocClass.php create mode 100644 tests/Doubler/ClassPatch/MagicCallPatchTest.php diff --git a/fixtures/WithPhpdocClass.php b/fixtures/WithPhpdocClass.php new file mode 100644 index 000000000..3f42c3c42 --- /dev/null +++ b/fixtures/WithPhpdocClass.php @@ -0,0 +1,17 @@ +contextFactory->createFromReflector($reflectionClass) ); - return $phpdoc->getTagsByName('method'); + $methods = array(); + + foreach ($phpdoc->getTagsByName('method') as $tag) { + if ($tag instanceof Method) { + $methods[] = $tag; + } + } + + return $methods; } catch (\InvalidArgumentException $e) { return array(); } diff --git a/tests/Doubler/ClassPatch/MagicCallPatchTest.php b/tests/Doubler/ClassPatch/MagicCallPatchTest.php new file mode 100644 index 000000000..989998adc --- /dev/null +++ b/tests/Doubler/ClassPatch/MagicCallPatchTest.php @@ -0,0 +1,32 @@ +reflect($class, array()); + + $patch = new MagicCallPatch(); + + $patch->apply($classNode); + + // Newer phpDocumentor versions allow reading valid method tags, even when some other tags are invalid + if (class_exists('phpDocumentor\Reflection\DocBlockFactory') && class_exists('phpDocumentor\Reflection\Types\ContextFactory')) { + $this->assertTrue($classNode->hasMethod('name')); + } + + $this->assertFalse($classNode->hasMethod('randomElement')); + } +}