-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #275 from dunglas/dockbloc-3
Compatibility with phpDocumentor Reflection Dockbloc 3
- Loading branch information
Showing
7 changed files
with
215 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,16 +16,17 @@ | |
"email": "[email protected]" | ||
} | ||
], | ||
|
||
"require": { | ||
"php": "^5.3|^7.0", | ||
"phpdocumentor/reflection-docblock": "~2.0", | ||
"sebastian/comparator": "~1.1", | ||
"phpdocumentor/reflection-docblock": "^2.0|^3.0.2", | ||
"sebastian/comparator": "^1.1", | ||
"doctrine/instantiator": "^1.0.2", | ||
"sebastian/recursion-context": "~1.0" | ||
"sebastian/recursion-context": "^1.0" | ||
}, | ||
|
||
"require-dev": { | ||
"phpspec/phpspec": "~2.0" | ||
"phpspec/phpspec": "^2.0" | ||
}, | ||
|
||
"autoload": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,17 +11,27 @@ | |
|
||
namespace Prophecy\Doubler\ClassPatch; | ||
|
||
use phpDocumentor\Reflection\DocBlock; | ||
use Prophecy\Doubler\Generator\Node\ClassNode; | ||
use Prophecy\Doubler\Generator\Node\MethodNode; | ||
use Prophecy\PhpDocumentor\ClassAndInterfaceTagRetriever; | ||
use Prophecy\PhpDocumentor\MethodTagRetrieverInterface; | ||
|
||
/** | ||
* Discover Magical API using "@method" PHPDoc format. | ||
* | ||
* @author Thomas Tourlourat <[email protected]> | ||
* @author Kévin Dunglas <[email protected]> | ||
* @author Théo FIDRY <[email protected]> | ||
*/ | ||
class MagicCallPatch implements ClassPatchInterface | ||
{ | ||
private $tagRetriever; | ||
|
||
public function __construct(MethodTagRetrieverInterface $tagRetriever = null) | ||
{ | ||
$this->tagRetriever = null === $tagRetriever ? new ClassAndInterfaceTagRetriever() : $tagRetriever; | ||
} | ||
|
||
/** | ||
* Support any class | ||
* | ||
|
@@ -44,15 +54,7 @@ public function apply(ClassNode $node) | |
$parentClass = $node->getParentClass(); | ||
$reflectionClass = new \ReflectionClass($parentClass); | ||
|
||
$phpdoc = new DocBlock($reflectionClass->getDocComment()); | ||
|
||
$tagList = $phpdoc->getTagsByName('method'); | ||
|
||
$interfaces = $reflectionClass->getInterfaces(); | ||
foreach($interfaces as $interface) { | ||
$phpdoc = new DocBlock($interface); | ||
$tagList = array_merge($tagList, $phpdoc->getTagsByName('method')); | ||
} | ||
$tagList = $this->tagRetriever->getTagList($reflectionClass); | ||
|
||
foreach($tagList as $tag) { | ||
$methodName = $tag->getMethodName(); | ||
|
@@ -62,7 +64,7 @@ public function apply(ClassNode $node) | |
} | ||
|
||
if (!$reflectionClass->hasMethod($methodName)) { | ||
$methodNode = new MethodNode($tag->getMethodName()); | ||
$methodNode = new MethodNode($methodName); | ||
$methodNode->setStatic($tag->isStatic()); | ||
|
||
$node->addMethod($methodNode); | ||
|
69 changes: 69 additions & 0 deletions
69
src/Prophecy/PhpDocumentor/ClassAndInterfaceTagRetriever.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Prophecy. | ||
* (c) Konstantin Kudryashov <[email protected]> | ||
* Marcello Duarte <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Prophecy\PhpDocumentor; | ||
|
||
use phpDocumentor\Reflection\DocBlock\Tag\MethodTag as LegacyMethodTag; | ||
use phpDocumentor\Reflection\DocBlock\Tags\Method; | ||
|
||
/** | ||
* @author Théo FIDRY <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class ClassAndInterfaceTagRetriever implements MethodTagRetrieverInterface | ||
{ | ||
private $classRetriever; | ||
|
||
public function __construct(MethodTagRetrieverInterface $classRetriever = null) | ||
{ | ||
if (null !== $classRetriever) { | ||
$this->classRetriever = $classRetriever; | ||
|
||
return; | ||
} | ||
|
||
$this->classRetriever = class_exists('phpDocumentor\Reflection\DocBlockFactory') && class_exists('phpDocumentor\Reflection\Types\ContextFactory') | ||
? new ClassTagRetriever() | ||
: new LegacyClassTagRetriever() | ||
; | ||
} | ||
|
||
/** | ||
* @param \ReflectionClass $reflectionClass | ||
* | ||
* @return LegacyMethodTag[]|Method[] | ||
*/ | ||
public function getTagList(\ReflectionClass $reflectionClass) | ||
{ | ||
return array_merge( | ||
$this->classRetriever->getTagList($reflectionClass), | ||
$this->getInterfacesTagList($reflectionClass) | ||
); | ||
} | ||
|
||
/** | ||
* @param \ReflectionClass $reflectionClass | ||
* | ||
* @return LegacyMethodTag[]|Method[] | ||
*/ | ||
private function getInterfacesTagList(\ReflectionClass $reflectionClass) | ||
{ | ||
$interfaces = $reflectionClass->getInterfaces(); | ||
$tagList = array(); | ||
|
||
foreach($interfaces as $interface) { | ||
$tagList = array_merge($tagList, $this->classRetriever->getTagList($interface)); | ||
} | ||
|
||
return $tagList; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Prophecy. | ||
* (c) Konstantin Kudryashov <[email protected]> | ||
* Marcello Duarte <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Prophecy\PhpDocumentor; | ||
|
||
use phpDocumentor\Reflection\DocBlock\Tags\Method; | ||
use phpDocumentor\Reflection\DocBlockFactory; | ||
use phpDocumentor\Reflection\Types\ContextFactory; | ||
|
||
/** | ||
* @author Théo FIDRY <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class ClassTagRetriever implements MethodTagRetrieverInterface | ||
{ | ||
private $docBlockFactory; | ||
private $contextFactory; | ||
|
||
public function __construct() | ||
{ | ||
$this->docBlockFactory = DocBlockFactory::createInstance(); | ||
$this->contextFactory = new ContextFactory(); | ||
} | ||
|
||
/** | ||
* @param \ReflectionClass $reflectionClass | ||
* | ||
* @return Method[] | ||
*/ | ||
public function getTagList(\ReflectionClass $reflectionClass) | ||
{ | ||
try { | ||
$phpdoc = $this->docBlockFactory->create( | ||
$reflectionClass, | ||
$this->contextFactory->createFromReflector($reflectionClass) | ||
); | ||
|
||
return $phpdoc->getTagsByName('method'); | ||
} catch (\InvalidArgumentException $e) { | ||
return array(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Prophecy. | ||
* (c) Konstantin Kudryashov <[email protected]> | ||
* Marcello Duarte <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Prophecy\PhpDocumentor; | ||
|
||
use phpDocumentor\Reflection\DocBlock; | ||
use phpDocumentor\Reflection\DocBlock\Tag\MethodTag as LegacyMethodTag; | ||
|
||
/** | ||
* @author Théo FIDRY <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class LegacyClassTagRetriever implements MethodTagRetrieverInterface | ||
{ | ||
/** | ||
* @param \ReflectionClass $reflectionClass | ||
* | ||
* @return LegacyMethodTag[] | ||
*/ | ||
public function getTagList(\ReflectionClass $reflectionClass) | ||
{ | ||
$phpdoc = new DocBlock($reflectionClass->getDocComment()); | ||
|
||
return $phpdoc->getTagsByName('method'); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Prophecy/PhpDocumentor/MethodTagRetrieverInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Prophecy. | ||
* (c) Konstantin Kudryashov <[email protected]> | ||
* Marcello Duarte <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Prophecy\PhpDocumentor; | ||
|
||
use phpDocumentor\Reflection\DocBlock\Tag\MethodTag as LegacyMethodTag; | ||
use phpDocumentor\Reflection\DocBlock\Tags\Method; | ||
|
||
/** | ||
* @author Théo FIDRY <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
interface MethodTagRetrieverInterface | ||
{ | ||
/** | ||
* @param \ReflectionClass $reflectionClass | ||
* | ||
* @return LegacyMethodTag[]|Method[] | ||
*/ | ||
public function getTagList(\ReflectionClass $reflectionClass); | ||
} |