From cf570b1be0d330b5c6e5f121720fc2f2b8759df6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr> Date: Wed, 9 Mar 2016 15:11:40 +0100 Subject: [PATCH] =?UTF-8?q?Passe=20des=20classe=20(\ReflectionClass)=20plu?= =?UTF-8?q?t=C3=B4t=20que=20des=20$className.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Model/ClassFilterInterface.php | 6 ++-- Model/ClassVisitor.php | 36 +++++++++++++----------- Model/Decorator/CompositeDecorator.php | 5 ++-- Model/Decorator/FilteringDecorator.php | 7 +++-- Model/Decorator/InheritanceDecorator.php | 4 +-- Model/DecoratorInterface.php | 4 ++- Model/Filter/AcceptAllFilter.php | 3 +- Model/Filter/Whitelist.php | 5 ++-- 8 files changed, 39 insertions(+), 31 deletions(-) diff --git a/Model/ClassFilterInterface.php b/Model/ClassFilterInterface.php index 6c8ac88..7eca714 100644 --- a/Model/ClassFilterInterface.php +++ b/Model/ClassFilterInterface.php @@ -8,6 +8,8 @@ namespace Irstea\PlantUmlBundle\Model; +use ReflectionClass; + /** * * @author Guillaume Perréal <guillaume.perreal@irstea.fr> @@ -15,8 +17,8 @@ namespace Irstea\PlantUmlBundle\Model; interface ClassFilterInterface { /** - * @param type $className + * @param ReflectionClass $class * @return bool */ - public function accept($className); + public function accept(ReflectionClass $class); } diff --git a/Model/ClassVisitor.php b/Model/ClassVisitor.php index d39a4f4..3a57ad5 100644 --- a/Model/ClassVisitor.php +++ b/Model/ClassVisitor.php @@ -73,37 +73,39 @@ class ClassVisitor implements ClassVisitorInterface, WritableInterface if (isset($this->nodes[$className])) { return $this->nodes[$className]; } - if (!$this->filter->accept($className)) { + + $class = new ReflectionClass($className); + + if (!$this->filter->accept($class)) { return $this->nodes[$className] = false; } - return $this->createNode($className); + $namespace = $this->rootNamespace->getNamespace($class->getNamespaceName()); + $node = $this->nodes[$className] = $this->createNode($namespace, $class); + $namespace->addNode($node); + + $this->decorator->decorate($class, $node, $this); + + return $node; } /** * - * @param string $className + * @param \Irstea\PlantUmlBundle\Model\NamespaceInterface $namespace + * @param ReflectionClass $class * @return NodeInterface */ - protected function createNode($className) + protected function createNode(NamespaceInterface $namespace, ReflectionClass $class) { - $class = new ReflectionClass($className); - $namespace = $this->rootNamespace->getNamespace($class->getNamespaceName()); - if ($class->isTrait()) { - $node = new Trait_($namespace, $className); - } elseif ($class->isInterface()) { - $node = new Interface_($namespace, $className); - } else { - $node = new Class_($namespace, $className, $class->isAbstract(), $class->isFinal()); + return new Trait_($namespace, $class->getName()); } - $this->nodes[$class->getName()] = $node; - $namespace->addNode($node); - - $this->decorator->decorate($className, $node, $this); + if ($class->isInterface()) { + return new Interface_($namespace, $class->getName()); + } - return $node; + return new Class_($namespace, $class->getName(), $class->isAbstract(), $class->isFinal()); } public function outputTo(WriterInterface $writer) diff --git a/Model/Decorator/CompositeDecorator.php b/Model/Decorator/CompositeDecorator.php index 788210a..8e6759d 100644 --- a/Model/Decorator/CompositeDecorator.php +++ b/Model/Decorator/CompositeDecorator.php @@ -11,6 +11,7 @@ namespace Irstea\PlantUmlBundle\Model\Decorator; use Irstea\PlantUmlBundle\Model\ClassVisitorInterface; use Irstea\PlantUmlBundle\Model\DecoratorInterface; use Irstea\PlantUmlBundle\Model\NodeInterface; +use ReflectionClass; /** * Description of CompositeDecorator @@ -35,10 +36,10 @@ class CompositeDecorator implements DecoratorInterface return $this; } - public function decorate($className, NodeInterface $node, ClassVisitorInterface $visitor) + public function decorate(ReflectionClass $class, NodeInterface $node, ClassVisitorInterface $visitor) { foreach($this->decorators as $decorator) { - $decorator->decorate($className, $node, $visitor); + $decorator->decorate($class, $node, $visitor); } return $this; } diff --git a/Model/Decorator/FilteringDecorator.php b/Model/Decorator/FilteringDecorator.php index 6025de1..7fb1ade 100644 --- a/Model/Decorator/FilteringDecorator.php +++ b/Model/Decorator/FilteringDecorator.php @@ -12,6 +12,7 @@ use Irstea\PlantUmlBundle\Model\ClassFilterInterface; use Irstea\PlantUmlBundle\Model\ClassVisitorInterface; use Irstea\PlantUmlBundle\Model\DecoratorInterface; use Irstea\PlantUmlBundle\Model\NodeInterface; +use ReflectionClass; /** * Description of FilteringDecorator @@ -40,10 +41,10 @@ class FilteringDecorator implements DecoratorInterface $this->filter = $filter; } - public function decorate($className, NodeInterface $node, ClassVisitorInterface $visitor) + public function decorate(ReflectionClass $class, NodeInterface $node, ClassVisitorInterface $visitor) { - if ($this->filter->accept($className)) { - $this->next->decorate($className, $node, $visitor); + if ($this->filter->accept($class)) { + $this->next->decorate($class, $node, $visitor); } return $this; } diff --git a/Model/Decorator/InheritanceDecorator.php b/Model/Decorator/InheritanceDecorator.php index d3b52c4..ff901f0 100644 --- a/Model/Decorator/InheritanceDecorator.php +++ b/Model/Decorator/InheritanceDecorator.php @@ -21,10 +21,8 @@ use ReflectionClass; */ class InheritanceDecorator implements DecoratorInterface { - public function decorate($className, NodeInterface $node, ClassVisitorInterface $visitor) + public function decorate(ReflectionClass $class, NodeInterface $node, ClassVisitorInterface $visitor) { - $class = new ReflectionClass($className); - $parent = $class->getParentClass(); $traits = $class->getTraitNames(); $interfaces = $class->getInterfaceNames(); diff --git a/Model/DecoratorInterface.php b/Model/DecoratorInterface.php index 72c530b..c391a90 100644 --- a/Model/DecoratorInterface.php +++ b/Model/DecoratorInterface.php @@ -8,11 +8,13 @@ namespace Irstea\PlantUmlBundle\Model; +use ReflectionClass; + /** * * @author Guillaume Perréal <guillaume.perreal@irstea.fr> */ interface DecoratorInterface { - public function decorate($className, NodeInterface $node, ClassVisitorInterface $visitor); + public function decorate(ReflectionClass $class, NodeInterface $node, ClassVisitorInterface $visitor); } diff --git a/Model/Filter/AcceptAllFilter.php b/Model/Filter/AcceptAllFilter.php index 7ec74ee..1fd59bf 100644 --- a/Model/Filter/AcceptAllFilter.php +++ b/Model/Filter/AcceptAllFilter.php @@ -9,6 +9,7 @@ namespace Irstea\PlantUmlBundle\Model\Filter; use Irstea\PlantUmlBundle\Model\ClassFilterInterface; +use ReflectionClass; /** * Description of AcceptAllFilter @@ -19,7 +20,7 @@ class AcceptAllFilter implements ClassFilterInterface { use \Irstea\PlantUmlBundle\Utils\Singleton; - public function accept($className) + public function accept(ReflectionClass $class) { return true; } diff --git a/Model/Filter/Whitelist.php b/Model/Filter/Whitelist.php index ecbd663..582960a 100644 --- a/Model/Filter/Whitelist.php +++ b/Model/Filter/Whitelist.php @@ -9,6 +9,7 @@ namespace Irstea\PlantUmlBundle\Model\Filter; use Irstea\PlantUmlBundle\Model\ClassFilterInterface; +use ReflectionClass; /** * Description of Whiltelist @@ -27,8 +28,8 @@ class Whitelist implements ClassFilterInterface $this->accepted = array_fill_keys($classNames, true); } - public function accept($className) + public function accept(ReflectionClass $class) { - return isset($this->accepted[$className]); + return isset($this->accepted[$class->getName()]); } } -- GitLab