Skip to content

Commit ee9188a

Browse files
Jefemymfn
andauthored
Implement new config to specify return type of custom relations (#1300)
* Implement new config to specify return type of custom relations * Apply suggestions from code review Co-authored-by: Markus Podar <markus@fischer.name> * add test for morphed * fix test output * add info about how to add custom relationships * fix wording Co-authored-by: Markus Podar <markus@fischer.name>
1 parent a567f73 commit ee9188a

File tree

10 files changed

+167
-2
lines changed

10 files changed

+167
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ All notable changes to this project will be documented in this file.
3030
### Added
3131
- Add support for custom casts that using `Castable` [#1287 / binotaliu](https://github.com/barryvdh/laravel-ide-helper/pull/1287)
3232
- Added Laravel 9 support [#1297 / rcerljenko](https://github.com/barryvdh/laravel-ide-helper/pull/1297)
33+
- Added option `additional_relation_return_types` for custom relations that don't fit the typical naming scheme
3334

3435
2022-01-03, 2.11.0
3536
------------------

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,26 @@ For those special cases, you can map them via the config `custom_db_types`. Exam
281281
],
282282
```
283283

284+
#### Custom Relationship Types
285+
286+
If you are using relationships not built into Laravel you will need to specify the name and returning class in the config to get proper generation.
287+
288+
```php
289+
'additional_relation_types' => [
290+
'externalHasMany' => \My\Package\externalHasMany::class
291+
],
292+
```
293+
294+
Found relationships will typically generate a return value based on the name of the relationship.
295+
296+
If your custom relationships don't follow this traditional naming scheme you can define its return type manually. The available options are `many` and `morphTo`.
297+
298+
```php
299+
'additional_relation_return_types' => [
300+
'externalHasMultiple' => 'many'
301+
],
302+
```
303+
284304
#### Model Hooks
285305

286306
If you need additional information on your model from sources that are not handled by default, you can hook in to the

config/ide-helper.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,19 @@
304304
*/
305305
'additional_relation_types' => [],
306306

307+
/*
308+
|--------------------------------------------------------------------------
309+
| Additional relation return types
310+
|--------------------------------------------------------------------------
311+
|
312+
| When using custom relation types its possible for the class name to not contain
313+
| the proper return type of the relation. The key of the array is the relationship
314+
| method name. The value of the array is the return type of the relation.
315+
| e.g. `'relationName' => 'many'`.
316+
|
317+
*/
318+
'additional_relation_return_types' => [],
319+
307320
/*
308321
|--------------------------------------------------------------------------
309322
| Run artisan commands after migrations to generate model helpers

src/Console/ModelsCommand.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,10 @@ public function getPropertiesFromMethods($model)
702702
get_class($relationObj->getRelated())
703703
);
704704

705-
if (strpos(get_class($relationObj), 'Many') !== false) {
705+
if (
706+
strpos(get_class($relationObj), 'Many') !== false ||
707+
($this->getRelationReturnTypes()[$relation] ?? '') === 'many'
708+
) {
706709
//Collection or array of models (because Collection is Arrayable)
707710
$relatedClass = '\\' . get_class($relationObj->getRelated());
708711
$collectionClass = $this->getCollectionClass($relatedClass);
@@ -726,7 +729,10 @@ public function getPropertiesFromMethods($model)
726729
// What kind of comments should be added to the relation count here?
727730
);
728731
}
729-
} elseif ($relation === 'morphTo') {
732+
} elseif (
733+
$relation === 'morphTo' ||
734+
($this->getRelationReturnTypes()[$relation] ?? '') === 'morphTo'
735+
) {
730736
// Model isn't specified because relation is polymorphic
731737
$this->setProperty(
732738
$method,
@@ -1074,6 +1080,14 @@ protected function getRelationTypes(): array
10741080
return array_merge(self::RELATION_TYPES, $configuredRelations);
10751081
}
10761082

1083+
/**
1084+
* Returns the return types of relations
1085+
*/
1086+
protected function getRelationReturnTypes(): array
1087+
{
1088+
return $this->laravel['config']->get('ide-helper.additional_relation_return_types', []);
1089+
}
1090+
10771091
/**
10781092
* @return bool
10791093
*/

tests/Console/ModelsCommand/Relations/Models/Simple.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,14 @@ public function relationSampleRelationType()
9797
{
9898
return $this->testToManyRelation(Simple::class);
9999
}
100+
101+
public function relationSampleToAnyRelationType()
102+
{
103+
return $this->testToAnyRelation(Simple::class);
104+
}
105+
106+
public function relationSampleToAnyMorphedRelationType()
107+
{
108+
return $this->testToAnyMorphedRelation(Simple::class);
109+
}
100110
}

tests/Console/ModelsCommand/Relations/Test.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
88
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
9+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToAnyRelationType;
910
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToManyRelationType;
1011
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToOneRelationType;
1112
use Illuminate\Support\Facades\Config;
@@ -19,6 +20,13 @@ protected function setUp(): void
1920
Config::set('ide-helper.additional_relation_types', [
2021
'testToOneRelation' => SampleToOneRelationType::class,
2122
'testToManyRelation' => SampleToManyRelationType::class,
23+
'testToAnyRelation' => SampleToAnyRelationType::class,
24+
'testToAnyMorphedRelation' => SampleToAnyMorphedRelationType::class,
25+
]);
26+
27+
Config::set('ide-helper.additional_relation_return_types', [
28+
'testToAnyRelation' => 'many',
29+
'testToAnyMorphedRelation' => 'morphTo',
2230
]);
2331
}
2432

tests/Console/ModelsCommand/Relations/Traits/HasTestRelations.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Traits;
66

7+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToAnyRelationType;
8+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToAnyMorphedRelationType;
79
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToManyRelationType;
810
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToOneRelationType;
911

@@ -20,4 +22,16 @@ public function testToManyRelation($related)
2022
$instance = $this->newRelatedInstance($related);
2123
return new SampleToManyRelationType($instance->newQuery(), $this);
2224
}
25+
26+
public function testToAnyRelation($related)
27+
{
28+
$instance = $this->newRelatedInstance($related);
29+
return new SampleToAnyRelationType($instance->newQuery(), $this);
30+
}
31+
32+
public function testToAnyMorphedRelation($related)
33+
{
34+
$instance = $this->newRelatedInstance($related);
35+
return new SampleToAnyMorphedRelationType($instance->newQuery(), $this);
36+
}
2337
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types;
6+
7+
use Illuminate\Database\Eloquent\Collection;
8+
use Illuminate\Database\Eloquent\Relations\Relation;
9+
10+
class SampleToAnyMorphedRelationType extends Relation
11+
{
12+
public function addConstraints()
13+
{
14+
// Fake
15+
}
16+
17+
public function addEagerConstraints(array $models)
18+
{
19+
// Fake
20+
}
21+
22+
public function initRelation(array $models, $relation)
23+
{
24+
// Fake
25+
}
26+
27+
public function match(array $models, Collection $results, $relation)
28+
{
29+
// Fake
30+
}
31+
32+
public function getResults()
33+
{
34+
// Fake
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types;
6+
7+
use Illuminate\Database\Eloquent\Collection;
8+
use Illuminate\Database\Eloquent\Relations\Relation;
9+
10+
class SampleToAnyRelationType extends Relation
11+
{
12+
public function addConstraints()
13+
{
14+
// Fake
15+
}
16+
17+
public function addEagerConstraints(array $models)
18+
{
19+
// Fake
20+
}
21+
22+
public function initRelation(array $models, $relation)
23+
{
24+
// Fake
25+
}
26+
27+
public function match(array $models, Collection $results, $relation)
28+
{
29+
// Fake
30+
}
31+
32+
public function getResults()
33+
{
34+
// Fake
35+
}
36+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ public function nullableColumnWithNoForeignKeyConstraint(): BelongsTo
9494
* @property-read int|null $relation_morphed_by_many_count
9595
* @property-read \Illuminate\Database\Eloquent\Collection|Simple[] $relationSampleRelationType
9696
* @property-read int|null $relation_sample_relation_type_count
97+
* @property-read Model|\Eloquent $relationSampleToAnyMorphedRelationType
98+
* @property-read \Illuminate\Database\Eloquent\Collection|Simple[] $relationSampleToAnyRelationType
99+
* @property-read int|null $relation_sample_to_any_relation_type_count
97100
* @property-read Simple $relationSampleToManyRelationType
98101
* @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery()
99102
* @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery()
@@ -182,4 +185,14 @@ public function relationSampleRelationType()
182185
{
183186
return $this->testToManyRelation(Simple::class);
184187
}
188+
189+
public function relationSampleToAnyRelationType()
190+
{
191+
return $this->testToAnyRelation(Simple::class);
192+
}
193+
194+
public function relationSampleToAnyMorphedRelationType()
195+
{
196+
return $this->testToAnyMorphedRelation(Simple::class);
197+
}
185198
}

0 commit comments

Comments
 (0)