Skip to content

Commit cee441c

Browse files
authored
Fix #1300 relation_return_type must take precedence if it is defined (#1394)
1 parent 9343d3a commit cee441c

File tree

6 files changed

+85
-4
lines changed

6 files changed

+85
-4
lines changed

src/Console/ModelsCommand.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -753,9 +753,14 @@ public function getPropertiesFromMethods($model)
753753
get_class($relationObj->getRelated())
754754
);
755755

756+
$relationReturnType = $this->getRelationReturnTypes()[$relation] ?? false;
757+
756758
if (
757-
strpos(get_class($relationObj), 'Many') !== false ||
758-
($this->getRelationReturnTypes()[$relation] ?? '') === 'many'
759+
$relationReturnType === 'many' ||
760+
(
761+
!$relationReturnType &&
762+
strpos(get_class($relationObj), 'Many') !== false
763+
)
759764
) {
760765
//Collection or array of models (because Collection is Arrayable)
761766
$relatedClass = '\\' . get_class($relationObj->getRelated());
@@ -782,8 +787,11 @@ public function getPropertiesFromMethods($model)
782787
);
783788
}
784789
} elseif (
785-
$relation === 'morphTo' ||
786-
($this->getRelationReturnTypes()[$relation] ?? '') === 'morphTo'
790+
$relationReturnType === 'morphTo' ||
791+
(
792+
!$relationReturnType &&
793+
$relation === 'morphTo'
794+
)
787795
) {
788796
// Model isn't specified because relation is polymorphic
789797
$this->setProperty(

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,9 @@ public function relationSampleToAnyMorphedRelationType()
107107
{
108108
return $this->testToAnyMorphedRelation(Simple::class);
109109
}
110+
111+
public function relationSampleToBadlyNamedNotManyRelation()
112+
{
113+
return $this->testToBadlyNamedNotManyRelation(Simple::class);
114+
}
110115
}

tests/Console/ModelsCommand/Relations/Test.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
99
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToAnyMorphedRelationType;
1010
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToAnyRelationType;
11+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToBadlyNamedNotManyRelationType;
1112
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToManyRelationType;
1213
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToOneRelationType;
1314
use Illuminate\Support\Facades\Config;
@@ -23,11 +24,13 @@ protected function setUp(): void
2324
'testToManyRelation' => SampleToManyRelationType::class,
2425
'testToAnyRelation' => SampleToAnyRelationType::class,
2526
'testToAnyMorphedRelation' => SampleToAnyMorphedRelationType::class,
27+
'testToBadlyNamedNotManyRelation' => SampleToBadlyNamedNotManyRelationType::class,
2628
]);
2729

2830
Config::set('ide-helper.additional_relation_return_types', [
2931
'testToAnyRelation' => 'many',
3032
'testToAnyMorphedRelation' => 'morphTo',
33+
'testToBadlyNamedNotManyRelation' => 'one',
3134
]);
3235
}
3336

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

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

77
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToAnyMorphedRelationType;
88
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToAnyRelationType;
9+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToBadlyNamedNotManyRelationType;
910
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToManyRelationType;
1011
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToOneRelationType;
1112

@@ -34,4 +35,10 @@ public function testToAnyMorphedRelation($related)
3435
$instance = $this->newRelatedInstance($related);
3536
return new SampleToAnyMorphedRelationType($instance->newQuery(), $this);
3637
}
38+
39+
public function testToBadlyNamedNotManyRelation($related)
40+
{
41+
$instance = $this->newRelatedInstance($related);
42+
return new SampleToBadlyNamedNotManyRelationType($instance->newQuery(), $this);
43+
}
3744
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\Model;
9+
use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels;
10+
use Illuminate\Database\Eloquent\Relations\Relation;
11+
12+
/**
13+
* Sample for custom relation
14+
*
15+
* the relation is a big fake and only for testing of the docblock generation
16+
*
17+
* @package Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations
18+
*/
19+
class SampleToBadlyNamedNotManyRelationType extends Relation
20+
{
21+
use SupportsDefaultModels;
22+
23+
public function addConstraints()
24+
{
25+
// Fake
26+
}
27+
28+
public function addEagerConstraints(array $models)
29+
{
30+
// Fake
31+
}
32+
33+
public function initRelation(array $models, $relation)
34+
{
35+
// Fake
36+
}
37+
38+
public function match(array $models, Collection $results, $relation)
39+
{
40+
// Fake
41+
}
42+
43+
public function getResults()
44+
{
45+
// Fake
46+
}
47+
48+
protected function newRelatedInstanceFor(Model $parent)
49+
{
50+
// Fake
51+
}
52+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ public function nullableMixedWithForeignKeyConstraint(): BelongsTo
159159
* @property-read Model|\Eloquent $relationSampleToAnyMorphedRelationType
160160
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationSampleToAnyRelationType
161161
* @property-read int|null $relation_sample_to_any_relation_type_count
162+
* @property-read Simple $relationSampleToBadlyNamedNotManyRelation
162163
* @property-read Simple $relationSampleToManyRelationType
163164
* @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery()
164165
* @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery()
@@ -257,4 +258,9 @@ public function relationSampleToAnyMorphedRelationType()
257258
{
258259
return $this->testToAnyMorphedRelation(Simple::class);
259260
}
261+
262+
public function relationSampleToBadlyNamedNotManyRelation()
263+
{
264+
return $this->testToBadlyNamedNotManyRelation(Simple::class);
265+
}
260266
}

0 commit comments

Comments
 (0)