diff --git a/src/Alias.php b/src/Alias.php index 0859982ca..1ca161822 100644 --- a/src/Alias.php +++ b/src/Alias.php @@ -341,7 +341,7 @@ protected function addMagicMethods() $magic, $this->interfaces, $this->classAliases, - $this->getReplaceReturnTypes($class) + $this->getReturnTypeNormalizers($class) ); } $this->usedMethods[] = $magic; @@ -373,7 +373,7 @@ protected function detectMethods() $method->name, $this->interfaces, $this->classAliases, - $this->getReplaceReturnTypes($reflection) + $this->getReturnTypeNormalizers($reflection) ); } $this->usedMethods[] = $method->name; @@ -397,7 +397,7 @@ protected function detectMethods() $macro_name, $this->interfaces, $this->classAliases, - $this->getReplaceReturnTypes($reflection) + $this->getReturnTypeNormalizers($reflection) ); $this->usedMethods[] = $macro_name; } @@ -408,12 +408,12 @@ protected function detectMethods() /** * @param ReflectionClass $class - * @return string[] + * @return Array */ - protected function getReplaceReturnTypes($class) + protected function getReturnTypeNormalizers($class) { if ($this->alias === 'Eloquent' && in_array($class->getName(), [EloquentBuilder::class, QueryBuilder::class])) { - return ['$this' => EloquentBuilder::class . '|static']; + return ['$this' => '\\' . EloquentBuilder::class . '|static']; } return []; diff --git a/src/Macro.php b/src/Macro.php index 53e70a428..3679d79fb 100644 --- a/src/Macro.php +++ b/src/Macro.php @@ -18,7 +18,7 @@ class Macro extends Method * @param null $methodName * @param array $interfaces * @param array $classAliases - * @param array $replaceReturnTypes + * @param array $returnTypeNormalizers */ public function __construct( $method, @@ -27,9 +27,9 @@ public function __construct( $methodName = null, $interfaces = [], $classAliases = [], - $replaceReturnTypes = [] + $returnTypeNormalizers = [] ) { - parent::__construct($method, $alias, $class, $methodName, $interfaces, $classAliases, $replaceReturnTypes); + parent::__construct($method, $alias, $class, $methodName, $interfaces, $classAliases, $returnTypeNormalizers); } /** diff --git a/src/Method.php b/src/Method.php index 094e7aaed..b4e0f7003 100644 --- a/src/Method.php +++ b/src/Method.php @@ -37,7 +37,7 @@ class Method protected $return = null; protected $root; protected $classAliases; - protected $replaceReturnTypes; + protected $returnTypeNormalizers; /** * @param \ReflectionMethod|\ReflectionFunctionAbstract $method @@ -46,13 +46,14 @@ class Method * @param string|null $methodName * @param array $interfaces * @param array $classAliases - * @param array $replaceReturnTypes + * @param array $returnTypeNormalizers */ - public function __construct($method, $alias, $class, $methodName = null, $interfaces = [], array $classAliases = [], $replaceReturnTypes = []) + public function __construct($method, $alias, $class, $methodName = null, $interfaces = [], array $classAliases = [], array $returnTypeNormalizers = []) { $this->method = $method; $this->interfaces = $interfaces; $this->classAliases = $classAliases; + $this->returnTypeNormalizers = $returnTypeNormalizers; $this->name = $methodName ?: $method->name; $this->real_name = $method->isClosure() ? $this->name : $method->name; $this->initClassDefinedProperties($method, $class); @@ -65,7 +66,6 @@ public function __construct($method, $alias, $class, $methodName = null, $interf //Normalize the description and inherit the docs from parents/interfaces try { - $this->setReplaceReturnTypes($replaceReturnTypes); $this->normalizeParams($this->phpdoc); $this->normalizeReturn($this->phpdoc); $this->normalizeDescription($this->phpdoc); @@ -181,14 +181,6 @@ public function getParams($implode = true) return $implode ? implode(', ', $this->params) : $this->params; } - /** - * @return string|null - */ - public function getReturn() - { - return $this->return; - } - /** * @param DocBlock|null $phpdoc * @return ReturnTag|null @@ -275,17 +267,8 @@ protected function normalizeParams(DocBlock $phpdoc) } } - protected function setReplaceReturnTypes($replaceReturnTypes) - { - if (!array_key_exists('$this', $replaceReturnTypes)) { - $replaceReturnTypes['$this'] = $this->root; - } - - $this->replaceReturnTypes = $replaceReturnTypes; - } - /** - * Normalize the return tag (make full namespace, replace interfaces) + * Normalize the return tag (make full namespace, replace interfaces, resolve $this) * * @param DocBlock $phpdoc */ @@ -302,6 +285,14 @@ protected function normalizeReturn(DocBlock $phpdoc) // Get the expanded type $returnValue = $tag->getType(); + if (array_key_exists($returnValue, $this->returnTypeNormalizers)) { + $returnValue = $this->returnTypeNormalizers[$returnValue]; + } + + if ($returnValue === '$this') { + $returnValue = $this->root; + } + // Replace the interfaces foreach ($this->interfaces as $interface => $real) { $returnValue = str_replace($interface, $real, $returnValue); @@ -310,10 +301,6 @@ protected function normalizeReturn(DocBlock $phpdoc) // Set the changed content $tag->setContent($returnValue . ' ' . $tag->getDescription()); $this->return = $returnValue; - - if (array_key_exists($tag->getType(), $this->replaceReturnTypes)) { - $tag->setType($this->replaceReturnTypes[$tag->getType()]); - } } /** diff --git a/tests/MethodTest.php b/tests/MethodTest.php index 125dac112..f2cb56ab3 100644 --- a/tests/MethodTest.php +++ b/tests/MethodTest.php @@ -62,7 +62,7 @@ public function testEloquentBuilderOutput() $reflectionClass = new \ReflectionClass(EloquentBuilder::class); $reflectionMethod = $reflectionClass->getMethod('with'); - $method = new Method($reflectionMethod, 'Builder', $reflectionClass, null, [], [], ['$this' => EloquentBuilder::class . '|static']); + $method = new Method($reflectionMethod, 'Builder', $reflectionClass, null, [], [], ['$this' => '\\' . EloquentBuilder::class . '|static']); $expectedDocComment = <<<'DOC' /** @@ -80,7 +80,6 @@ public function testEloquentBuilderOutput() $this->assertSame(['$relations', '$callback'], $method->getParams(false)); $this->assertSame(['$relations', '$callback = null'], $method->getParamsWithDefault(false)); $this->assertTrue($method->shouldReturn()); - $this->assertSame('$this', $method->getReturn()); $this->assertSame('\Illuminate\Database\Eloquent\Builder|static', rtrim($method->getReturnTag()->getType())); } @@ -92,7 +91,7 @@ public function testQueryBuilderOutput() $reflectionClass = new \ReflectionClass(QueryBuilder::class); $reflectionMethod = $reflectionClass->getMethod('whereNull'); - $method = new Method($reflectionMethod, 'Builder', $reflectionClass, null, [], [], ['$this' => EloquentBuilder::class . '|static']); + $method = new Method($reflectionMethod, 'Builder', $reflectionClass, null, [], [], ['$this' => '\\' . EloquentBuilder::class . '|static']); $expectedDocComment = <<<'DOC' /** @@ -112,7 +111,6 @@ public function testQueryBuilderOutput() $this->assertSame(['$columns', '$boolean', '$not'], $method->getParams(false)); $this->assertSame(['$columns', "\$boolean = 'and'", '$not = false'], $method->getParamsWithDefault(false)); $this->assertTrue($method->shouldReturn()); - $this->assertSame('$this', $method->getReturn()); $this->assertSame('\Illuminate\Database\Eloquent\Builder|static', rtrim($method->getReturnTag()->getType())); }