From 3518f87e840fcdcbd2d958eb9c0f402f003bd7cd Mon Sep 17 00:00:00 2001 From: Casper Bloemendaal Date: Sat, 25 Jan 2025 14:09:10 +0100 Subject: [PATCH] Adhere to the IDE-helper config (#278) * Adhere to the IDE-helper config * fix: existing PHPStan issues * fix: ignore errors to keep matching original ide-helper code * Remove comments * Refactoring * Refactoring --------- Co-authored-by: Jonas Staudenmeir --- .../Relations/BelongsToManyOfDescendants.php | 2 +- src/Eloquent/Relations/Descendants.php | 2 +- src/Eloquent/Relations/Graph/Ancestors.php | 4 ++-- src/Eloquent/Relations/Graph/Descendants.php | 4 ++-- .../Graph/Traits/IsRecursiveRelation.php | 4 ++-- .../Traits/IsOfDescendantsRelation.php | 2 +- .../Relations/Traits/IsRecursiveRelation.php | 4 ++-- .../Traits/BuildsAdjacencyListQueries.php | 2 +- src/IdeHelper/RecursiveRelationsHook.php | 20 ++++++++++++++++--- .../IdeHelper/RecursiveRelationsHookTest.php | 20 +++++++++++++++++-- 10 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/Eloquent/Relations/BelongsToManyOfDescendants.php b/src/Eloquent/Relations/BelongsToManyOfDescendants.php index 3d9fd58..c48ec26 100644 --- a/src/Eloquent/Relations/BelongsToManyOfDescendants.php +++ b/src/Eloquent/Relations/BelongsToManyOfDescendants.php @@ -109,7 +109,7 @@ public function getEagerLoadingAccessor() * * @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder $query * @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder $parentQuery - * @param list|string|\Illuminate\Database\Query\Expression $columns + * @param list>|string|\Illuminate\Database\Query\Expression<*> $columns * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder */ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) diff --git a/src/Eloquent/Relations/Descendants.php b/src/Eloquent/Relations/Descendants.php index a4f2bc0..85e1736 100644 --- a/src/Eloquent/Relations/Descendants.php +++ b/src/Eloquent/Relations/Descendants.php @@ -104,7 +104,7 @@ protected function buildDictionary(Collection $results) * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parentQuery - * @param list|string|\Illuminate\Database\Query\Expression $columns + * @param list>|string|\Illuminate\Database\Query\Expression<*> $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) diff --git a/src/Eloquent/Relations/Graph/Ancestors.php b/src/Eloquent/Relations/Graph/Ancestors.php index b800f00..bf424a5 100644 --- a/src/Eloquent/Relations/Graph/Ancestors.php +++ b/src/Eloquent/Relations/Graph/Ancestors.php @@ -93,7 +93,7 @@ protected function buildDictionary(Collection $results) * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parentQuery - * @param list|string|\Illuminate\Database\Query\Expression $columns + * @param list>|string|\Illuminate\Database\Query\Expression<*> $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) @@ -126,7 +126,7 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, * Add the constraints for a relationship query on the same table. * * @param \Illuminate\Database\Eloquent\Builder $query - * @param list|string|\Illuminate\Database\Query\Expression $columns + * @param list>|string|\Illuminate\Database\Query\Expression<*> $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceQueryForSelfRelation( diff --git a/src/Eloquent/Relations/Graph/Descendants.php b/src/Eloquent/Relations/Graph/Descendants.php index a450a75..b1d8221 100644 --- a/src/Eloquent/Relations/Graph/Descendants.php +++ b/src/Eloquent/Relations/Graph/Descendants.php @@ -93,7 +93,7 @@ protected function buildDictionary(Collection $results) * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parentQuery - * @param list|string|\Illuminate\Database\Query\Expression $columns + * @param list>|string|\Illuminate\Database\Query\Expression<*> $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) @@ -126,7 +126,7 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, * Add the constraints for a relationship query on the same table. * * @param \Illuminate\Database\Eloquent\Builder $query - * @param list|string|\Illuminate\Database\Query\Expression $columns + * @param list>|string|\Illuminate\Database\Query\Expression<*> $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceQueryForSelfRelation( diff --git a/src/Eloquent/Relations/Graph/Traits/IsRecursiveRelation.php b/src/Eloquent/Relations/Graph/Traits/IsRecursiveRelation.php index 1115c39..8310719 100644 --- a/src/Eloquent/Relations/Graph/Traits/IsRecursiveRelation.php +++ b/src/Eloquent/Relations/Graph/Traits/IsRecursiveRelation.php @@ -121,8 +121,8 @@ public function __call($method, $parameters) * Replace table hash with expression name in self-relation aggregate queries. * * @param \Illuminate\Database\Eloquent\Builder<*> $query - * @param \Illuminate\Database\Query\Expression $expression - * @return \Illuminate\Database\Query\Expression + * @param \Illuminate\Database\Query\Expression<*> $expression + * @return \Illuminate\Database\Query\Expression<*> */ protected function replaceTableHash(Builder $query, Expression $expression): Expression { diff --git a/src/Eloquent/Relations/Traits/IsOfDescendantsRelation.php b/src/Eloquent/Relations/Traits/IsOfDescendantsRelation.php index e7e0e4f..6dff7a8 100644 --- a/src/Eloquent/Relations/Traits/IsOfDescendantsRelation.php +++ b/src/Eloquent/Relations/Traits/IsOfDescendantsRelation.php @@ -380,7 +380,7 @@ abstract public function getExpressionForeignKeyName(); * * @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder $query * @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder $parentQuery - * @param list|string|\Illuminate\Database\Query\Expression $columns + * @param list>|string|\Illuminate\Database\Query\Expression<*> $columns * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder */ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) diff --git a/src/Eloquent/Relations/Traits/IsRecursiveRelation.php b/src/Eloquent/Relations/Traits/IsRecursiveRelation.php index 80356c1..54f2398 100644 --- a/src/Eloquent/Relations/Traits/IsRecursiveRelation.php +++ b/src/Eloquent/Relations/Traits/IsRecursiveRelation.php @@ -92,8 +92,8 @@ public function __call($method, $parameters) * Replace table hash with expression name in self-relation aggregate queries. * * @param \Illuminate\Database\Eloquent\Builder<*> $query - * @param \Illuminate\Database\Query\Expression $expression - * @return \Illuminate\Database\Query\Expression + * @param \Illuminate\Database\Query\Expression<*> $expression + * @return \Illuminate\Database\Query\Expression<*> */ protected function replaceTableHash(Builder $query, Expression $expression) { diff --git a/src/Eloquent/Traits/BuildsAdjacencyListQueries.php b/src/Eloquent/Traits/BuildsAdjacencyListQueries.php index b210fc1..6469d1a 100644 --- a/src/Eloquent/Traits/BuildsAdjacencyListQueries.php +++ b/src/Eloquent/Traits/BuildsAdjacencyListQueries.php @@ -19,7 +19,7 @@ trait BuildsAdjacencyListQueries /** * Get the hydrated models without eager loading. * - * @param list|string $columns + * @param list>|string $columns * @return list<\Illuminate\Database\Eloquent\Model> */ public function getModels($columns = ['*']) diff --git a/src/IdeHelper/RecursiveRelationsHook.php b/src/IdeHelper/RecursiveRelationsHook.php index 98d111a..2ba75a7 100644 --- a/src/IdeHelper/RecursiveRelationsHook.php +++ b/src/IdeHelper/RecursiveRelationsHook.php @@ -127,12 +127,19 @@ class RecursiveRelationsHook implements ModelHookInterface public function run(ModelsCommand $command, Model $model): void { + /** @var \Illuminate\Contracts\Config\Repository $config */ + $config = $command->getLaravel()->make('config'); + $traits = class_uses_recursive($model); + $useGenericsSyntax = $config->get('ide-helper.use_generics_annotations', true); + if (in_array(HasRecursiveRelationships::class, $traits)) { foreach (static::$treeRelationships as $relationship) { $type = $relationship['manyRelation'] - ? '\\' . TreeCollection::class . '|\\' . $model::class . '[]' + ? ($useGenericsSyntax + ? '\\' . TreeCollection::class . '' + : '\\' . TreeCollection::class . '|\\' . $model::class . '[]') : '\\' . $model::class; $this->addRelationship($command, $relationship, $type); @@ -141,7 +148,9 @@ public function run(ModelsCommand $command, Model $model): void if (in_array(HasGraphRelationships::class, $traits)) { foreach (static::$graphRelationships as $relationship) { - $type = '\\' . GraphCollection::class . '|\\' . $model::class . '[]'; + $type = $useGenericsSyntax + ? '\\' . GraphCollection::class . '' + : '\\' . GraphCollection::class . '|\\' . $model::class . '[]'; $this->addRelationship($command, $relationship, $type); } @@ -153,6 +162,9 @@ public function run(ModelsCommand $command, Model $model): void */ protected function addRelationship(ModelsCommand $command, array $relationship, string $type): void { + /** @var \Illuminate\Contracts\Config\Repository $config */ + $config = $command->getLaravel()->make('config'); + $command->setProperty( $relationship['name'], $type, @@ -162,7 +174,9 @@ protected function addRelationship(ModelsCommand $command, array $relationship, !$relationship['manyRelation'] ); - if ($relationship['manyRelation']) { + $addCountProperties = $config->get('ide-helper.write_model_relation_count_properties', true); + + if ($relationship['manyRelation'] && $addCountProperties) { $command->setProperty( Str::snake($relationship['name']) . '_count', 'int', diff --git a/tests/IdeHelper/RecursiveRelationsHookTest.php b/tests/IdeHelper/RecursiveRelationsHookTest.php index ad569e9..75d0b58 100644 --- a/tests/IdeHelper/RecursiveRelationsHookTest.php +++ b/tests/IdeHelper/RecursiveRelationsHookTest.php @@ -3,6 +3,8 @@ namespace Staudenmeir\LaravelAdjacencyList\Tests\IdeHelper; use Barryvdh\LaravelIdeHelper\Console\ModelsCommand; +use Illuminate\Contracts\Config\Repository; +use Illuminate\Contracts\Foundation\Application; use Mockery; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use PHPUnit\Framework\TestCase; @@ -16,11 +18,18 @@ class RecursiveRelationsHookTest extends TestCase public function testTreeRelations(): void { + $config = Mockery::mock(Repository::class); + $config->shouldReceive('get')->andReturn(true); + + $app = Mockery::mock(Application::class); + $app->shouldReceive('make')->andReturn($config); + $command = Mockery::mock(ModelsCommand::class); + $command->shouldReceive('getLaravel')->andReturn($app); $command->shouldReceive('setProperty')->times(2); $command->shouldReceive('setProperty')->once()->with( 'ancestorsAndSelf', - '\Staudenmeir\LaravelAdjacencyList\Eloquent\Collection|\Staudenmeir\LaravelAdjacencyList\Tests\IdeHelper\Models\User[]', + '\Staudenmeir\LaravelAdjacencyList\Eloquent\Collection', true, false, "The model's recursive parents and itself.", @@ -51,11 +60,18 @@ public function testTreeRelations(): void public function testGraphRelations(): void { + $config = Mockery::mock(Repository::class); + $config->shouldReceive('get')->andReturn(true); + + $app = Mockery::mock(Application::class); + $app->shouldReceive('make')->andReturn($config); + $command = Mockery::mock(ModelsCommand::class); + $command->shouldReceive('getLaravel')->andReturn($app); $command->shouldReceive('setProperty')->times(2); $command->shouldReceive('setProperty')->once()->with( 'ancestorsAndSelf', - '\Staudenmeir\LaravelAdjacencyList\Eloquent\Graph\Collection|\Staudenmeir\LaravelAdjacencyList\Tests\IdeHelper\Models\Node[]', + '\Staudenmeir\LaravelAdjacencyList\Eloquent\Graph\Collection', true, false, "The node's recursive parents and itself.",