From be97950a072d4ab24c85e87ad6862a4a56fc5ba9 Mon Sep 17 00:00:00 2001 From: Carl-Philipp Jung Date: Sat, 27 Jul 2024 10:53:56 +0200 Subject: [PATCH 1/8] Add return type `|static` to Query\Builder methods (#1574) --- src/Method.php | 39 ++++++++++++++++++----- tests/MethodTest.php | 74 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 102 insertions(+), 11 deletions(-) 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 c740f03bf..a732f6c95 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,11 +55,11 @@ 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('upsert'); $method = new Method($reflectionMethod, 'Builder', $reflectionClass); @@ -76,12 +77,77 @@ public function testEloquentBuilderOutput() DOC; $this->assertSame($output, $method->getDocComment('')); $this->assertSame('upsert', $method->getName()); - $this->assertSame('\\' . Builder::class, $method->getDeclaringClass()); + $this->assertSame('\\' . EloquentBuilder::class, $method->getDeclaringClass()); $this->assertSame('$values, $uniqueBy, $update', $method->getParams(true)); $this->assertSame(['$values', '$uniqueBy', '$update'], $method->getParams(false)); $this->assertSame('$values, $uniqueBy, $update = null', $method->getParamsWithDefault(true)); $this->assertSame(['$values', '$uniqueBy', '$update = null'], $method->getParamsWithDefault(false)); $this->assertTrue($method->shouldReturn()); + $this->assertSame('int', rtrim($method->getReturnTag()->getType())); + } + + /** + * Test normalized return type of Illuminate\Database\Eloquent\Builder + */ + public function testEloquentBuilderNormalizedReturnType() + { + $reflectionClass = new \ReflectionClass(EloquentBuilder::class); + $reflectionMethod = $reflectionClass->getMethod('where'); + + $method = new Method($reflectionMethod, 'Builder', $reflectionClass, null, [], [], ['$this' => '\\' . EloquentBuilder::class . '|static']); + + $output = <<<'DOC' +/** + * Add a basic where clause to the query. + * + * @param (\Closure(static): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column + * @param mixed $operator + * @param mixed $value + * @param string $boolean + * @return \Illuminate\Database\Eloquent\Builder|static + * @static + */ +DOC; + $this->assertSame($output, $method->getDocComment('')); + $this->assertSame('where', $method->getName()); + $this->assertSame('\\' . EloquentBuilder::class, $method->getDeclaringClass()); + $this->assertSame(['$column', '$operator', '$value', '$boolean'], $method->getParams(false)); + $this->assertSame(['$column', "\$operator = null", "\$value = null", "\$boolean = 'and'"], $method->getParamsWithDefault(false)); + $this->assertTrue($method->shouldReturn()); + $this->assertSame('$this', $method->getReturn()); + $this->assertSame('\Illuminate\Database\Eloquent\Builder|static', rtrim($method->getReturnTag()->getType())); + } + + /** + * Test normalized return type of Illuminate\Database\Query\Builder + */ + public function testQueryBuilderNormalizedReturnType() + { + $reflectionClass = new \ReflectionClass(QueryBuilder::class); + $reflectionMethod = $reflectionClass->getMethod('whereNull'); + + $method = new Method($reflectionMethod, 'Builder', $reflectionClass, null, [], [], ['$this' => '\\' . EloquentBuilder::class . '|static']); + + $output = <<<'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($output, $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())); } /** From 9436d2c835d8585d68bd73bf36f56ad608c75f95 Mon Sep 17 00:00:00 2001 From: Carl-Philipp Jung Date: Sat, 27 Jul 2024 12:41:28 +0200 Subject: [PATCH 2/8] Replace return type Query\Builder with Eloquent\Builder (#1574) --- src/Method.php | 2 +- tests/MethodTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Method.php b/src/Method.php index 1642fd553..8de76daea 100644 --- a/src/Method.php +++ b/src/Method.php @@ -303,7 +303,7 @@ protected function normalizeReturn(DocBlock $phpdoc) if ($tag->getType() === '$this') { in_array(ltrim($this->root, '\\'), [EloquentBuilder::class, QueryBuilder::class]) - ? $tag->setType($this->root . '|static') + ? $tag->setType(EloquentBuilder::class . '|static') : $tag->setType($this->root); } } diff --git a/tests/MethodTest.php b/tests/MethodTest.php index a732f6c95..12175baa7 100644 --- a/tests/MethodTest.php +++ b/tests/MethodTest.php @@ -135,7 +135,7 @@ public function testQueryBuilderNormalizedReturnType() * @param string|array|\Illuminate\Contracts\Database\Query\Expression $columns * @param string $boolean * @param bool $not - * @return \Illuminate\Database\Query\Builder|static + * @return \Illuminate\Database\Eloquent\Builder|static * @static */ DOC; @@ -147,7 +147,7 @@ public function testQueryBuilderNormalizedReturnType() $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())); + $this->assertSame('\Illuminate\Database\Eloquent\Builder|static', rtrim($method->getReturnTag()->getType())); } /** From 210bec610334d95491cc974d2dfc44af0efbd78b Mon Sep 17 00:00:00 2001 From: Carl-Philipp Jung Date: Sat, 27 Jul 2024 15:00:20 +0200 Subject: [PATCH 3/8] Replace return type Query\Builder only for facade Eloquent (#1574) --- src/Alias.php | 18 ++++++++++++++++-- src/Method.php | 21 ++++++++++++++------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/Alias.php b/src/Alias.php index 046fffe41..5471f3e73 100644 --- a/src/Alias.php +++ b/src/Alias.php @@ -333,7 +333,15 @@ protected function addMagicMethods() if (!in_array($magic, $this->usedMethods)) { if ($class !== $this->root) { - $this->methods[] = new Method($method, $this->alias, $class, $magic, $this->interfaces, $this->classAliases); + $this->methods[] = new Method( + $method, + $this->alias, + $class, + $magic, + $this->interfaces, + $this->classAliases, + $this->getReplaceReturnTypes() + ); } $this->usedMethods[] = $magic; } @@ -363,7 +371,8 @@ protected function detectMethods() $reflection, $method->name, $this->interfaces, - $this->classAliases + $this->classAliases, + $this->getReplaceReturnTypes() ); } $this->usedMethods[] = $method->name; @@ -395,6 +404,11 @@ protected function detectMethods() } } + protected function getReplaceReturnTypes() + { + return $this->alias === 'Eloquent' ? ['$this' => EloquentBuilder::class . '|static'] : []; + } + /** * @param $macro_func * diff --git a/src/Method.php b/src/Method.php index 8de76daea..f3d016f0b 100644 --- a/src/Method.php +++ b/src/Method.php @@ -17,8 +17,6 @@ use Barryvdh\Reflection\DocBlock\Tag; use Barryvdh\Reflection\DocBlock\Tag\ParamTag; use Barryvdh\Reflection\DocBlock\Tag\ReturnTag; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Query\Builder as QueryBuilder; class Method { @@ -39,6 +37,7 @@ class Method protected $return = null; protected $root; protected $classAliases; + protected $replaceReturnTypes; /** * @param \ReflectionMethod|\ReflectionFunctionAbstract $method @@ -48,7 +47,7 @@ class Method * @param array $interfaces * @param array $classAliases */ - public function __construct($method, $alias, $class, $methodName = null, $interfaces = [], array $classAliases = []) + public function __construct($method, $alias, $class, $methodName = null, $interfaces = [], array $classAliases = [], $replaceReturnTypes = []) { $this->method = $method; $this->interfaces = $interfaces; @@ -65,6 +64,7 @@ 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); @@ -274,6 +274,15 @@ 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) * @@ -301,10 +310,8 @@ protected function normalizeReturn(DocBlock $phpdoc) $tag->setContent($returnValue . ' ' . $tag->getDescription()); $this->return = $returnValue; - if ($tag->getType() === '$this') { - in_array(ltrim($this->root, '\\'), [EloquentBuilder::class, QueryBuilder::class]) - ? $tag->setType(EloquentBuilder::class . '|static') - : $tag->setType($this->root); + if (array_key_exists($tag->getType(), $this->replaceReturnTypes)) { + $tag->setType($this->replaceReturnTypes[$tag->getType()]); } } From 9e1fb6cd75810b3826130b0fc65bfea6c88cb67a Mon Sep 17 00:00:00 2001 From: Carl-Philipp Jung Date: Sat, 27 Jul 2024 16:46:16 +0200 Subject: [PATCH 4/8] Add special return type replacement for Macros of \Eloquent --- src/Alias.php | 3 ++- src/Macro.php | 6 ++++-- src/Method.php | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Alias.php b/src/Alias.php index 5471f3e73..b76f505ee 100644 --- a/src/Alias.php +++ b/src/Alias.php @@ -395,7 +395,8 @@ protected function detectMethods() $reflection, $macro_name, $this->interfaces, - $this->classAliases + $this->classAliases, + $this->getReplaceReturnTypes() ); $this->usedMethods[] = $macro_name; } diff --git a/src/Macro.php b/src/Macro.php index 77cc5a6e2..53e70a428 100644 --- a/src/Macro.php +++ b/src/Macro.php @@ -18,6 +18,7 @@ class Macro extends Method * @param null $methodName * @param array $interfaces * @param array $classAliases + * @param array $replaceReturnTypes */ public function __construct( $method, @@ -25,9 +26,10 @@ public function __construct( $class, $methodName = null, $interfaces = [], - $classAliases = [] + $classAliases = [], + $replaceReturnTypes = [] ) { - parent::__construct($method, $alias, $class, $methodName, $interfaces, $classAliases); + parent::__construct($method, $alias, $class, $methodName, $interfaces, $classAliases, $replaceReturnTypes); } /** diff --git a/src/Method.php b/src/Method.php index f3d016f0b..094e7aaed 100644 --- a/src/Method.php +++ b/src/Method.php @@ -46,6 +46,7 @@ class Method * @param string|null $methodName * @param array $interfaces * @param array $classAliases + * @param array $replaceReturnTypes */ public function __construct($method, $alias, $class, $methodName = null, $interfaces = [], array $classAliases = [], $replaceReturnTypes = []) { From 3d6c545c2e876500463f15c3ab2a55e930bcc179 Mon Sep 17 00:00:00 2001 From: Carl-Philipp Jung Date: Sat, 27 Jul 2024 17:00:56 +0200 Subject: [PATCH 5/8] Restrict special return type to methods from Eloquent\Builder and Query\Builder --- src/Alias.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Alias.php b/src/Alias.php index b76f505ee..0859982ca 100644 --- a/src/Alias.php +++ b/src/Alias.php @@ -18,6 +18,7 @@ use Closure; use Illuminate\Config\Repository as ConfigRepository; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; +use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Support\Facades\Facade; use ReflectionClass; use Throwable; @@ -340,7 +341,7 @@ protected function addMagicMethods() $magic, $this->interfaces, $this->classAliases, - $this->getReplaceReturnTypes() + $this->getReplaceReturnTypes($class) ); } $this->usedMethods[] = $magic; @@ -372,7 +373,7 @@ protected function detectMethods() $method->name, $this->interfaces, $this->classAliases, - $this->getReplaceReturnTypes() + $this->getReplaceReturnTypes($reflection) ); } $this->usedMethods[] = $method->name; @@ -396,7 +397,7 @@ protected function detectMethods() $macro_name, $this->interfaces, $this->classAliases, - $this->getReplaceReturnTypes() + $this->getReplaceReturnTypes($reflection) ); $this->usedMethods[] = $macro_name; } @@ -405,9 +406,17 @@ protected function detectMethods() } } - protected function getReplaceReturnTypes() + /** + * @param ReflectionClass $class + * @return string[] + */ + protected function getReplaceReturnTypes($class) { - return $this->alias === 'Eloquent' ? ['$this' => EloquentBuilder::class . '|static'] : []; + if ($this->alias === 'Eloquent' && in_array($class->getName(), [EloquentBuilder::class, QueryBuilder::class])) { + return ['$this' => EloquentBuilder::class . '|static']; + } + + return []; } /** From fe0c24a6ba8bdbbf33412280c965c6c59725e96e Mon Sep 17 00:00:00 2001 From: Carl-Philipp Jung Date: Tue, 3 Sep 2024 15:39:17 +0200 Subject: [PATCH 6/8] Do not overwrite return type in normalizeReturn() with conditional call of setType() --- src/Alias.php | 12 ++++++------ src/Macro.php | 6 +++--- src/Method.php | 39 +++++++++++++-------------------------- tests/MethodTest.php | 2 -- 4 files changed, 22 insertions(+), 37 deletions(-) diff --git a/src/Alias.php b/src/Alias.php index 0859982ca..80d1c2b3d 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 12175baa7..5d60e47f5 100644 --- a/tests/MethodTest.php +++ b/tests/MethodTest.php @@ -114,7 +114,6 @@ public function testEloquentBuilderNormalizedReturnType() $this->assertSame(['$column', '$operator', '$value', '$boolean'], $method->getParams(false)); $this->assertSame(['$column', "\$operator = null", "\$value = null", "\$boolean = 'and'"], $method->getParamsWithDefault(false)); $this->assertTrue($method->shouldReturn()); - $this->assertSame('$this', $method->getReturn()); $this->assertSame('\Illuminate\Database\Eloquent\Builder|static', rtrim($method->getReturnTag()->getType())); } @@ -146,7 +145,6 @@ public function testQueryBuilderNormalizedReturnType() $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())); } From 09ea0bfbf0d86919afe13a6fc96415efbbf8a760 Mon Sep 17 00:00:00 2001 From: Carl-Philipp Jung Date: Tue, 29 Oct 2024 13:11:32 +0100 Subject: [PATCH 7/8] Use generic return type for builder methods in \Eloquent --- src/Alias.php | 4 +++- tests/MethodTest.php | 12 ++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Alias.php b/src/Alias.php index 80d1c2b3d..7f2eae20c 100644 --- a/src/Alias.php +++ b/src/Alias.php @@ -413,7 +413,9 @@ protected function detectMethods() 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 . ($this->config->get('ide-helper.use_generics_annotations') ? '' : '|static') + ]; } return []; diff --git a/tests/MethodTest.php b/tests/MethodTest.php index 5d60e47f5..f5beafbba 100644 --- a/tests/MethodTest.php +++ b/tests/MethodTest.php @@ -94,7 +94,7 @@ public function testEloquentBuilderNormalizedReturnType() $reflectionClass = new \ReflectionClass(EloquentBuilder::class); $reflectionMethod = $reflectionClass->getMethod('where'); - $method = new Method($reflectionMethod, 'Builder', $reflectionClass, null, [], [], ['$this' => '\\' . EloquentBuilder::class . '|static']); + $method = new Method($reflectionMethod, 'Builder', $reflectionClass, null, [], [], ['$this' => '\\' . EloquentBuilder::class . '']); $output = <<<'DOC' /** @@ -104,7 +104,7 @@ public function testEloquentBuilderNormalizedReturnType() * @param mixed $operator * @param mixed $value * @param string $boolean - * @return \Illuminate\Database\Eloquent\Builder|static + * @return \Illuminate\Database\Eloquent\Builder * @static */ DOC; @@ -114,7 +114,7 @@ public function testEloquentBuilderNormalizedReturnType() $this->assertSame(['$column', '$operator', '$value', '$boolean'], $method->getParams(false)); $this->assertSame(['$column', "\$operator = null", "\$value = null", "\$boolean = 'and'"], $method->getParamsWithDefault(false)); $this->assertTrue($method->shouldReturn()); - $this->assertSame('\Illuminate\Database\Eloquent\Builder|static', rtrim($method->getReturnTag()->getType())); + $this->assertSame('\Illuminate\Database\Eloquent\Builder', rtrim($method->getReturnTag()->getType())); } /** @@ -125,7 +125,7 @@ public function testQueryBuilderNormalizedReturnType() $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 . '']); $output = <<<'DOC' /** @@ -134,7 +134,7 @@ public function testQueryBuilderNormalizedReturnType() * @param string|array|\Illuminate\Contracts\Database\Query\Expression $columns * @param string $boolean * @param bool $not - * @return \Illuminate\Database\Eloquent\Builder|static + * @return \Illuminate\Database\Eloquent\Builder * @static */ DOC; @@ -145,7 +145,7 @@ public function testQueryBuilderNormalizedReturnType() $this->assertSame(['$columns', '$boolean', '$not'], $method->getParams(false)); $this->assertSame(['$columns', "\$boolean = 'and'", '$not = false'], $method->getParamsWithDefault(false)); $this->assertTrue($method->shouldReturn()); - $this->assertSame('\Illuminate\Database\Eloquent\Builder|static', rtrim($method->getReturnTag()->getType())); + $this->assertSame('\Illuminate\Database\Eloquent\Builder', rtrim($method->getReturnTag()->getType())); } /** From b844a572456407a939ef2bd1ad8d748c30c2b7c8 Mon Sep 17 00:00:00 2001 From: laravel-ide-helper Date: Tue, 29 Oct 2024 12:59:21 +0000 Subject: [PATCH 8/8] composer fix-style --- src/Alias.php | 2 +- tests/MethodTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Alias.php b/src/Alias.php index 7f2eae20c..74bd8cec5 100644 --- a/src/Alias.php +++ b/src/Alias.php @@ -414,7 +414,7 @@ protected function getReturnTypeNormalizers($class) { if ($this->alias === 'Eloquent' && in_array($class->getName(), [EloquentBuilder::class, QueryBuilder::class])) { return [ - '$this' => '\\' . EloquentBuilder::class . ($this->config->get('ide-helper.use_generics_annotations') ? '' : '|static') + '$this' => '\\' . EloquentBuilder::class . ($this->config->get('ide-helper.use_generics_annotations') ? '' : '|static'), ]; } diff --git a/tests/MethodTest.php b/tests/MethodTest.php index f5beafbba..c1681da62 100644 --- a/tests/MethodTest.php +++ b/tests/MethodTest.php @@ -112,7 +112,7 @@ public function testEloquentBuilderNormalizedReturnType() $this->assertSame('where', $method->getName()); $this->assertSame('\\' . EloquentBuilder::class, $method->getDeclaringClass()); $this->assertSame(['$column', '$operator', '$value', '$boolean'], $method->getParams(false)); - $this->assertSame(['$column', "\$operator = null", "\$value = null", "\$boolean = 'and'"], $method->getParamsWithDefault(false)); + $this->assertSame(['$column', '$operator = null', '$value = null', "\$boolean = 'and'"], $method->getParamsWithDefault(false)); $this->assertTrue($method->shouldReturn()); $this->assertSame('\Illuminate\Database\Eloquent\Builder', rtrim($method->getReturnTag()->getType())); }