From d7359b86d4299b1b8070e563f5165746007a1628 Mon Sep 17 00:00:00 2001 From: Pavlo Zhukov Date: Mon, 1 Oct 2018 00:20:54 +0300 Subject: [PATCH] Avoid code duplication in constructors --- src/Macro.php | 39 ++++++++++++++++----------------------- src/Method.php | 32 +++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/Macro.php b/src/Macro.php index 96ed968..95f214e 100644 --- a/src/Macro.php +++ b/src/Macro.php @@ -17,37 +17,30 @@ class Macro extends Method * @param array $interfaces */ public function __construct( - \ReflectionFunctionAbstract $method, + $method, $alias, $class, $methodName = null, $interfaces = array() ) { - $this->method = $method; - $this->interfaces = $interfaces; - $this->name = $methodName ?: $method->name; - $this->real_name = $method->name; - $this->namespace = $class->getNamespaceName(); + parent::__construct($method, $alias, $class, $methodName, $interfaces); + } - //Create a DocBlock and serializer instance + /** + * @param \ReflectionFunctionAbstract $method + */ + protected function initPhpDoc($method) + { $this->phpdoc = new DocBlock($method); + } - //Normalize the description and inherit the docs from parents/interfaces - try { - $this->normalizeParams($this->phpdoc); - $this->normalizeReturn($this->phpdoc); - $this->normalizeDescription($this->phpdoc); - } catch (\Exception $e) { - } - - //Get the parameters, including formatted default values - $this->getParameters($method); - - //Make the method static - $this->phpdoc->appendTag(Tag::createInstance('@static', $this->phpdoc)); - - //Reference the 'real' function in the declaringclass + /** + * @param \ReflectionFunctionAbstract $method + * @param \ReflectionClass $class + */ + protected function initClassDefinedProperties($method, \ReflectionClass $class) + { + $this->namespace = $class->getNamespaceName(); $this->declaringClassName = '\\' . ltrim($class->name, '\\'); - $this->root = '\\' . ltrim($class->getName(), '\\'); } } diff --git a/src/Method.php b/src/Method.php index 8a5117d..d0876cf 100644 --- a/src/Method.php +++ b/src/Method.php @@ -26,6 +26,7 @@ class Method protected $method; protected $output = ''; + protected $declaringClassName; protected $name; protected $namespace; protected $params = array(); @@ -35,22 +36,22 @@ class Method protected $return = null; /** - * @param \ReflectionMethod $method + * @param \ReflectionMethod|\ReflectionFunctionAbstract $method * @param string $alias * @param \ReflectionClass $class * @param string|null $methodName * @param array $interfaces */ - public function __construct(\ReflectionMethod $method, $alias, $class, $methodName = null, $interfaces = array()) + public function __construct($method, $alias, $class, $methodName = null, $interfaces = array()) { $this->method = $method; $this->interfaces = $interfaces; $this->name = $methodName ?: $method->name; $this->real_name = $method->name; - $this->namespace = $method->getDeclaringClass()->getNamespaceName(); + $this->initClassDefinedProperties($method, $class); //Create a DocBlock and serializer instance - $this->phpdoc = new DocBlock($method, new Context($this->namespace)); + $this->initPhpDoc($method); //Normalize the description and inherit the docs from parents/interfaces try { @@ -66,12 +67,29 @@ class Method //Make the method static $this->phpdoc->appendTag(Tag::createInstance('@static', $this->phpdoc)); - //Reference the 'real' function in the declaringclass - $declaringClass = $method->getDeclaringClass(); - $this->declaringClassName = '\\' . ltrim($declaringClass->name, '\\'); + //Reference the 'real' function in the declaring class $this->root = '\\' . ltrim($class->getName(), '\\'); } + /** + * @param \ReflectionMethod $method + */ + protected function initPhpDoc($method) + { + $this->phpdoc = new DocBlock($method, new Context($this->namespace)); + } + + /** + * @param \ReflectionMethod $method + * @param \ReflectionClass $class + */ + protected function initClassDefinedProperties($method, \ReflectionClass $class) + { + $declaringClass = $method->getDeclaringClass(); + $this->namespace = $declaringClass->getNamespaceName(); + $this->declaringClassName = '\\' . ltrim($declaringClass->name, '\\'); + } + /** * Get the class wherein the function resides *