Skip to content

Commit

Permalink
Add return type |static to Query\Builder methods (barryvdh#1574)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjio committed Jul 27, 2024
1 parent 591e7d6 commit 62afe8e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 15 deletions.
39 changes: 32 additions & 7 deletions src/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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();

Expand All @@ -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);
}
Expand Down
49 changes: 41 additions & 8 deletions tests/MethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand All @@ -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()));
}

/**
Expand Down

0 comments on commit 62afe8e

Please sign in to comment.