Skip to content

Commit

Permalink
Fix the handling of invalid phpdoc tags
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stof committed Mar 5, 2020
1 parent b4400ef commit 5975d8d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
17 changes: 17 additions & 0 deletions fixtures/WithPhpdocClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php


namespace Fixtures\Prophecy;


/**
* @method string name(string $gender = null)
* @method mixed randomElement(array $array = array('a', 'b', 'c'))
*/
class WithPhpdocClass
{
public function __call($name, $arguments)
{
// TODO: Implement __call() method.
}
}
10 changes: 9 additions & 1 deletion src/Prophecy/PhpDocumentor/ClassTagRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ public function getTagList(\ReflectionClass $reflectionClass)
$this->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();
}
Expand Down
32 changes: 32 additions & 0 deletions tests/Doubler/ClassPatch/MagicCallPatchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Tests\Prophecy\Doubler\ClassPatch;

use PHPUnit\Framework\TestCase;
use Prophecy\Doubler\ClassPatch\MagicCallPatch;
use Prophecy\Doubler\Generator\ClassMirror;

class MagicCallPatchTest extends TestCase
{
/**
* @test
*/
public function it_supports_classes_with_invalid_tags()
{
$class = new \ReflectionClass('Fixtures\Prophecy\WithPhpdocClass');

$mirror = new ClassMirror();
$classNode = $mirror->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'));
}
}

0 comments on commit 5975d8d

Please sign in to comment.