Skip to content

Commit

Permalink
Use generic return type for builder methods in \Eloquent
Browse files Browse the repository at this point in the history
  • Loading branch information
pjio committed Oct 29, 2024
1 parent fe0c24a commit 09ea0bf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>' : '|static')
];
}

return [];
Expand Down
12 changes: 6 additions & 6 deletions tests/MethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 . '<static>']);

$output = <<<'DOC'
/**
Expand All @@ -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>
* @static
*/
DOC;
Expand All @@ -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<static>', rtrim($method->getReturnTag()->getType()));
}

/**
Expand All @@ -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 . '<static>']);

$output = <<<'DOC'
/**
Expand All @@ -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>
* @static
*/
DOC;
Expand All @@ -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<static>', rtrim($method->getReturnTag()->getType()));
}

/**
Expand Down

0 comments on commit 09ea0bf

Please sign in to comment.