Skip to content

Commit f0e4cf5

Browse files
tanerkaymfn
andauthored
Generics annotations support (#1298)
* option to use generics syntax, default to yes in laravel 9 * rename `use_generics_syntax` to `use_generics_annotations` * update readme and changelog * remove laravel 9 default override * include key type in generics annotations * readme rewording * set `use_generics_annotations` to `true` by default * update tests to reflect `use_generics_annotations` being set to `true` by default * default `use_generics_annotations` setting to true when not set in config Co-authored-by: Markus Podar <markus@fischer.name> --------- Co-authored-by: Markus Podar <markus@fischer.name>
1 parent 5d9925b commit f0e4cf5

File tree

15 files changed

+148
-20
lines changed

15 files changed

+148
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file.
1414

1515
### Added
1616
- Add support for custom casts that implement `CastsInboundAttributes` [#1329 / sforward](https://github.com/barryvdh/laravel-ide-helper/pull/1329)
17+
- Add option `use_generics_annotations` for collection type hints [#1298 / tanerkay](https://github.com/barryvdh/laravel-ide-helper/pull/1298)
1718

1819
2022-03-06, 2.12.3
1920
------------------

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ You may use the [`::withCount`](https://laravel.com/docs/master/eloquent-relatio
227227

228228
By default, these attributes are generated in the phpdoc. You can turn them off by setting the config `write_model_relation_count_properties` to `false`.
229229

230+
#### Generics annotations
231+
232+
Laravel 9 introduced generics annotations in DocBlocks for collections. PhpStorm 2022.3 and above support the use of generics annotations within `@property` and `@property-read` declarations in DocBlocks, e.g. `Collection<User>` instead of `Collection|User[]`.
233+
234+
These can be disabled by setting the config `use_generics_annotations` to `false`.
235+
230236
#### Support `@comment` based on DocBlock
231237

232238
In order to better support IDEs, relations and getters/setters can also add a comment to a property like table columns. Therefore a custom docblock `@comment` is used:

config/ide-helper.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,17 @@
292292
*/
293293
'force_fqn' => false,
294294

295+
/*
296+
|--------------------------------------------------------------------------
297+
| Use generics syntax
298+
|--------------------------------------------------------------------------
299+
|
300+
| Use generics syntax within DocBlocks,
301+
| e.g. `Collection<User>` instead of `Collection|User[]`.
302+
|
303+
*/
304+
'use_generics_annotations' => true,
305+
295306
/*
296307
|--------------------------------------------------------------------------
297308
| Additional relation types

src/Console/ModelsCommand.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -713,9 +713,10 @@ public function getPropertiesFromMethods($model)
713713
$model,
714714
$collectionClass
715715
);
716+
$collectionTypeHint = $this->getCollectionTypeHint($collectionClassNameInModel, $relatedModel);
716717
$this->setProperty(
717718
$method,
718-
$collectionClassNameInModel . '|' . $relatedModel . '[]',
719+
$collectionTypeHint,
719720
true,
720721
null,
721722
$comment
@@ -1080,6 +1081,23 @@ protected function getCollectionClass($className)
10801081
return '\\' . get_class($model->newCollection());
10811082
}
10821083

1084+
/**
1085+
* Determine a model classes' collection type hint.
1086+
*
1087+
* @param string $collectionClassNameInModel
1088+
* @param string $relatedModel
1089+
* @return string
1090+
*/
1091+
protected function getCollectionTypeHint(string $collectionClassNameInModel, string $relatedModel): string
1092+
{
1093+
$useGenericsSyntax = $this->laravel['config']->get('ide-helper.use_generics_annotations', true);
1094+
if ($useGenericsSyntax) {
1095+
return $collectionClassNameInModel . '<int, ' . $relatedModel . '>';
1096+
} else {
1097+
return $collectionClassNameInModel . '|' . $relatedModel . '[]';
1098+
}
1099+
}
1100+
10831101
/**
10841102
* Returns the available relation types
10851103
*/
@@ -1277,8 +1295,9 @@ protected function getCollectionMethods($model)
12771295
if ($collectionClass !== '\\' . \Illuminate\Database\Eloquent\Collection::class) {
12781296
$collectionClassInModel = $this->getClassNameInDestinationFile($model, $collectionClass);
12791297

1280-
$this->setMethod('get', $collectionClassInModel . '|static[]', ['$columns = [\'*\']']);
1281-
$this->setMethod('all', $collectionClassInModel . '|static[]', ['$columns = [\'*\']']);
1298+
$collectionTypeHint = $this->getCollectionTypeHint($collectionClassInModel, 'static');
1299+
$this->setMethod('get', $collectionTypeHint, ['$columns = [\'*\']']);
1300+
$this->setMethod('all', $collectionTypeHint, ['$columns = [\'*\']']);
12821301
}
12831302
}
12841303

tests/Console/ModelsCommand/Comment/__snapshots__/Test__test__1.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* This is second line, success too.
2222
* @property-read string $many_format_comment There is format comment, success.
2323
* @property-read string $not_comment
24-
* @property-read \Illuminate\Database\Eloquent\Collection|Simple[] $relationHasMany HasMany relations.
24+
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationHasMany HasMany relations.
2525
* @property-read int|null $relation_has_many_count
2626
* @property-read Simple|null $relationHasOne Others relations.
2727
* @property-read Model|\Eloquent $relationMorphTo MorphTo relations.

tests/Console/ModelsCommand/CustomCollection/__snapshots__/Test__test__1.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\CustomCollection\Models\Simple
1313
*
1414
* @property integer $id
15-
* @property-read SimpleCollection|Simple[] $relationHasMany
15+
* @property-read SimpleCollection<int, Simple> $relationHasMany
1616
* @property-read int|null $relation_has_many_count
17-
* @method static SimpleCollection|static[] all($columns = ['*'])
18-
* @method static SimpleCollection|static[] get($columns = ['*'])
17+
* @method static SimpleCollection<int, static> all($columns = ['*'])
18+
* @method static SimpleCollection<int, static> get($columns = ['*'])
1919
* @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery()
2020
* @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery()
2121
* @method static \Illuminate\Database\Eloquent\Builder|Simple query()

tests/Console/ModelsCommand/DynamicRelations/__snapshots__/Test__test__1.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\DynamicRelations\Models\Dynamic
1414
*
15-
* @property-read \Illuminate\Database\Eloquent\Collection|Dynamic[] $regularHasMany
15+
* @property-read \Illuminate\Database\Eloquent\Collection<int, Dynamic> $regularHasMany
1616
* @property-read int|null $regular_has_many_count
1717
* @method static \Illuminate\Database\Eloquent\Builder|Dynamic newModelQuery()
1818
* @method static \Illuminate\Database\Eloquent\Builder|Dynamic newQuery()

tests/Console/ModelsCommand/GeneratePhpdocWithForcedFqn/__snapshots__/Test__test__1.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
* @property string $macaddress_not_nullable
8585
* @property \Illuminate\Support\Carbon|null $created_at
8686
* @property \Illuminate\Support\Carbon|null $updated_at
87-
* @property-read \Illuminate\Database\Eloquent\Collection|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post[] $posts
87+
* @property-read \Illuminate\Database\Eloquent\Collection<int, \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post> $posts
8888
* @property-read int|null $posts_count
8989
* @method static \Illuminate\Database\Eloquent\Builder|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post newModelQuery()
9090
* @method static \Illuminate\Database\Eloquent\Builder|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post newQuery()

tests/Console/ModelsCommand/GeneratePhpdocWithFqn/__snapshots__/Test__test__1.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
* @property string $macaddress_not_nullable
9191
* @property Carbon|null $created_at
9292
* @property Carbon|null $updated_at
93-
* @property-read Collection|Post[] $posts
93+
* @property-read Collection<int, Post> $posts
9494
* @property-read int|null $posts_count
9595
* @method static EloquentBuilder|Post newModelQuery()
9696
* @method static EloquentBuilder|Post newQuery()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenericsSyntaxDisabled\Models;
6+
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9+
use Illuminate\Database\Eloquent\Relations\HasMany;
10+
11+
class Simple extends Model
12+
{
13+
// Regular relations
14+
public function regularHasMany(): HasMany
15+
{
16+
return $this->hasMany(Simple::class);
17+
}
18+
19+
public function regularBelongsToMany(): BelongsToMany
20+
{
21+
return $this->belongsToMany(Simple::class);
22+
}
23+
}

0 commit comments

Comments
 (0)