diff --git a/src/Method.php b/src/Method.php index a46e3f2f3..1642fd553 100644 --- a/src/Method.php +++ b/src/Method.php @@ -17,8 +17,8 @@ use Barryvdh\Reflection\DocBlock\Tag; use Barryvdh\Reflection\DocBlock\Tag\ParamTag; use Barryvdh\Reflection\DocBlock\Tag\ReturnTag; -use Illuminate\Database\Eloquent\Builder; -use Illuminate\Support\Str; +use Illuminate\Database\Eloquent\Builder as EloquentBuilder; +use Illuminate\Database\Query\Builder as QueryBuilder; class Method { @@ -180,6 +180,33 @@ 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 + */ + public function getReturnTag($phpdoc = null) + { + if ($phpdoc === null) { + $phpdoc = $this->phpdoc; + } + + $returnTags = $phpdoc->getTagsByName('return'); + + if (count($returnTags) === 0) { + return null; + } + + return reset($returnTags); + } + /** * Get the parameters for this method including default values * @@ -255,15 +282,13 @@ protected function normalizeParams(DocBlock $phpdoc) protected function normalizeReturn(DocBlock $phpdoc) { //Get the return type and adjust them for better autocomplete - $returnTags = $phpdoc->getTagsByName('return'); + $tag = $this->getReturnTag($phpdoc); - if (count($returnTags) === 0) { + if ($tag === null) { $this->return = null; return; } - /** @var ReturnTag $tag */ - $tag = reset($returnTags); // Get the expanded type $returnValue = $tag->getType(); @@ -277,7 +302,7 @@ protected function normalizeReturn(DocBlock $phpdoc) $this->return = $returnValue; if ($tag->getType() === '$this') { - Str::contains($this->root, Builder::class) + in_array(ltrim($this->root, '\\'), [EloquentBuilder::class, QueryBuilder::class]) ? $tag->setType($this->root . '|static') : $tag->setType($this->root); } diff --git a/tests/MethodTest.php b/tests/MethodTest.php index 98010b0f3..5697e016e 100644 --- a/tests/MethodTest.php +++ b/tests/MethodTest.php @@ -5,7 +5,8 @@ namespace Barryvdh\LaravelIdeHelper\Tests; use Barryvdh\LaravelIdeHelper\Method; -use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Builder as EloquentBuilder; +use Illuminate\Database\Query\Builder as QueryBuilder; use PHPUnit\Framework\TestCase; class MethodTest extends TestCase @@ -54,16 +55,16 @@ public function testOutput() } /** - * Test the output of a class + * Test the output of Illuminate\Database\Eloquent\Builder */ public function testEloquentBuilderOutput() { - $reflectionClass = new \ReflectionClass(Builder::class); + $reflectionClass = new \ReflectionClass(EloquentBuilder::class); $reflectionMethod = $reflectionClass->getMethod('with'); $method = new Method($reflectionMethod, 'Builder', $reflectionClass); - $output = <<<'DOC' + $expectedDocComment = <<<'DOC' /** * Set the relationships that should be eager loaded. * @@ -73,14 +74,46 @@ public function testEloquentBuilderOutput() * @static */ DOC; - $this->assertSame($output, $method->getDocComment('')); + $this->assertSame($expectedDocComment, $method->getDocComment('')); $this->assertSame('with', $method->getName()); - $this->assertSame('\\' . Builder::class, $method->getDeclaringClass()); - $this->assertSame('$relations, $callback', $method->getParams(true)); + $this->assertSame('\\' . EloquentBuilder::class, $method->getDeclaringClass()); $this->assertSame(['$relations', '$callback'], $method->getParams(false)); - $this->assertSame('$relations, $callback = null', $method->getParamsWithDefault(true)); $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())); + } + + /** + * Test the output of Illuminate\Database\Query\Builder + */ + public function testQueryBuilderOutput() + { + $reflectionClass = new \ReflectionClass(QueryBuilder::class); + $reflectionMethod = $reflectionClass->getMethod('whereNull'); + + $method = new Method($reflectionMethod, 'Builder', $reflectionClass); + + $expectedDocComment = <<<'DOC' +/** + * Add a "where null" clause to the query. + * + * @param string|array|\Illuminate\Contracts\Database\Query\Expression $columns + * @param string $boolean + * @param bool $not + * @return \Illuminate\Database\Query\Builder|static + * @static + */ +DOC; + + $this->assertSame($expectedDocComment, $method->getDocComment('')); + $this->assertSame('whereNull', $method->getName()); + $this->assertSame('\\' . QueryBuilder::class, $method->getDeclaringClass()); + $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\Query\Builder|static', rtrim($method->getReturnTag()->getType())); } /**