Avoid code duplication in constructors

This commit is contained in:
Pavlo Zhukov
2018-10-01 00:20:54 +03:00
parent 5d8d93273f
commit d7359b86d4
2 changed files with 41 additions and 30 deletions
+16 -23
View File
@@ -17,37 +17,30 @@ class Macro extends Method
* @param array $interfaces * @param array $interfaces
*/ */
public function __construct( public function __construct(
\ReflectionFunctionAbstract $method, $method,
$alias, $alias,
$class, $class,
$methodName = null, $methodName = null,
$interfaces = array() $interfaces = array()
) { ) {
$this->method = $method; parent::__construct($method, $alias, $class, $methodName, $interfaces);
$this->interfaces = $interfaces; }
$this->name = $methodName ?: $method->name;
$this->real_name = $method->name;
$this->namespace = $class->getNamespaceName();
//Create a DocBlock and serializer instance /**
* @param \ReflectionFunctionAbstract $method
*/
protected function initPhpDoc($method)
{
$this->phpdoc = new DocBlock($method); $this->phpdoc = new DocBlock($method);
}
//Normalize the description and inherit the docs from parents/interfaces /**
try { * @param \ReflectionFunctionAbstract $method
$this->normalizeParams($this->phpdoc); * @param \ReflectionClass $class
$this->normalizeReturn($this->phpdoc); */
$this->normalizeDescription($this->phpdoc); protected function initClassDefinedProperties($method, \ReflectionClass $class)
} catch (\Exception $e) { {
} $this->namespace = $class->getNamespaceName();
//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
$this->declaringClassName = '\\' . ltrim($class->name, '\\'); $this->declaringClassName = '\\' . ltrim($class->name, '\\');
$this->root = '\\' . ltrim($class->getName(), '\\');
} }
} }
+25 -7
View File
@@ -26,6 +26,7 @@ class Method
protected $method; protected $method;
protected $output = ''; protected $output = '';
protected $declaringClassName;
protected $name; protected $name;
protected $namespace; protected $namespace;
protected $params = array(); protected $params = array();
@@ -35,22 +36,22 @@ class Method
protected $return = null; protected $return = null;
/** /**
* @param \ReflectionMethod $method * @param \ReflectionMethod|\ReflectionFunctionAbstract $method
* @param string $alias * @param string $alias
* @param \ReflectionClass $class * @param \ReflectionClass $class
* @param string|null $methodName * @param string|null $methodName
* @param array $interfaces * @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->method = $method;
$this->interfaces = $interfaces; $this->interfaces = $interfaces;
$this->name = $methodName ?: $method->name; $this->name = $methodName ?: $method->name;
$this->real_name = $method->name; $this->real_name = $method->name;
$this->namespace = $method->getDeclaringClass()->getNamespaceName(); $this->initClassDefinedProperties($method, $class);
//Create a DocBlock and serializer instance //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 //Normalize the description and inherit the docs from parents/interfaces
try { try {
@@ -66,12 +67,29 @@ class Method
//Make the method static //Make the method static
$this->phpdoc->appendTag(Tag::createInstance('@static', $this->phpdoc)); $this->phpdoc->appendTag(Tag::createInstance('@static', $this->phpdoc));
//Reference the 'real' function in the declaringclass //Reference the 'real' function in the declaring class
$declaringClass = $method->getDeclaringClass();
$this->declaringClassName = '\\' . ltrim($declaringClass->name, '\\');
$this->root = '\\' . ltrim($class->getName(), '\\'); $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 * Get the class wherein the function resides
* *