From 378adeb1f563af656fafa1adcbd893bd45e6d469 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Wed, 22 Apr 2020 20:18:54 +0100 Subject: [PATCH] Add static return type to builder methods (#924) * Add static return type to builder methods * formatting * add eloquent builder test * change to use assertSame --- src/Method.php | 8 +++++-- tests/MethodTest.php | 52 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/Method.php b/src/Method.php index 7bc6b7e..234dd68 100644 --- a/src/Method.php +++ b/src/Method.php @@ -16,6 +16,8 @@ use Barryvdh\Reflection\DocBlock\Tag; use Barryvdh\Reflection\DocBlock\Tag\ReturnTag; use Barryvdh\Reflection\DocBlock\Tag\ParamTag; use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Support\Str; class Method { @@ -266,7 +268,9 @@ class Method $this->return = $returnValue; if ($tag->getType() === '$this') { - $tag->setType($this->root); + Str::contains($this->root, Builder::class) + ? $tag->setType($this->root . '|static') + : $tag->setType($this->root); } } else { $this->return = null; @@ -357,7 +361,7 @@ class Method if ($method) { $namespace = $method->getDeclaringClass()->getNamespaceName(); $phpdoc = new DocBlock($method, new Context($namespace)); - + if (strpos($phpdoc->getText(), '{@inheritdoc}') !== false) { //Not at the end yet, try another parent/interface.. return $this->getInheritDoc($method); diff --git a/tests/MethodTest.php b/tests/MethodTest.php index 883b887..989ba26 100644 --- a/tests/MethodTest.php +++ b/tests/MethodTest.php @@ -3,6 +3,7 @@ namespace Barryvdh\LaravelIdeHelper\Tests; use Barryvdh\LaravelIdeHelper\Method; +use Illuminate\Database\Eloquent\Builder; use PHPUnit\Framework\TestCase; class ExampleTest extends TestCase @@ -38,14 +39,41 @@ class ExampleTest extends TestCase * @param string $middle * @static */'; - $this->assertEquals($output, $method->getDocComment('')); - $this->assertEquals('setName', $method->getName()); - $this->assertEquals('\\'.ExampleClass::class, $method->getDeclaringClass()); - $this->assertEquals('$last, $first, ...$middle', $method->getParams(true)); - $this->assertEquals(['$last', '$first', '...$middle'], $method->getParams(false)); - $this->assertEquals('$last, $first = \'Barry\', ...$middle', $method->getParamsWithDefault(true)); - $this->assertEquals(['$last', '$first = \'Barry\'', '...$middle'], $method->getParamsWithDefault(false)); - $this->assertEquals(true, $method->shouldReturn()); + $this->assertSame($output, $method->getDocComment('')); + $this->assertSame('setName', $method->getName()); + $this->assertSame('\\'.ExampleClass::class, $method->getDeclaringClass()); + $this->assertSame('$last, $first, ...$middle', $method->getParams(true)); + $this->assertSame(['$last', '$first', '...$middle'], $method->getParams(false)); + $this->assertSame('$last, $first = \'Barry\', ...$middle', $method->getParamsWithDefault(true)); + $this->assertSame(['$last', '$first = \'Barry\'', '...$middle'], $method->getParamsWithDefault(false)); + $this->assertSame(true, $method->shouldReturn()); + } + + /** + * Test the output of a class + */ + public function testEloquentBuilderOutput() + { + $reflectionClass = new \ReflectionClass(Builder::class); + $reflectionMethod = $reflectionClass->getMethod('with'); + + $method = new Method($reflectionMethod, 'Builder', $reflectionClass); + + $output = '/** + * Set the relationships that should be eager loaded. + * + * @param mixed $relations + * @return \Illuminate\Database\Eloquent\Builder|static + * @static + */'; + $this->assertSame($output, $method->getDocComment('')); + $this->assertSame('with', $method->getName()); + $this->assertSame('\\'.Builder::class, $method->getDeclaringClass()); + $this->assertSame('$relations', $method->getParams(true)); + $this->assertSame(['$relations'], $method->getParams(false)); + $this->assertSame('$relations', $method->getParamsWithDefault(true)); + $this->assertSame(['$relations'], $method->getParamsWithDefault(false)); + $this->assertSame(true, $method->shouldReturn()); } /** @@ -57,10 +85,10 @@ class ExampleTest extends TestCase $reflectionMethod = $reflectionClass->getMethod('setSpecialChars'); $method = new Method($reflectionMethod, 'Example', $reflectionClass); - $this->assertEquals('$chars', $method->getParams(true)); - $this->assertEquals(['$chars'], $method->getParams(false)); - $this->assertEquals('$chars = \'$\\\'\\\\\'', $method->getParamsWithDefault(true)); - $this->assertEquals(['$chars = \'$\\\'\\\\\''], $method->getParamsWithDefault(false)); + $this->assertSame('$chars', $method->getParams(true)); + $this->assertSame(['$chars'], $method->getParams(false)); + $this->assertSame('$chars = \'$\\\'\\\\\'', $method->getParamsWithDefault(true)); + $this->assertSame(['$chars = \'$\\\'\\\\\''], $method->getParamsWithDefault(false)); } }