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 a679d68
Show file tree
Hide file tree
Showing 3 changed files with 53 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
27 changes: 27 additions & 0 deletions tests/Doubler/ClassPatch/MagicCallPatchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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);

$this->assertTrue($classNode->hasMethod('name'));
}
}

0 comments on commit a679d68

Please sign in to comment.