Skip to content

Commit

Permalink
Fix #1300 relation_return_type must take precedence if it is defined (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
menthol authored Feb 8, 2024
1 parent 9343d3a commit cee441c
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -753,9 +753,14 @@ public function getPropertiesFromMethods($model)
get_class($relationObj->getRelated())
);

$relationReturnType = $this->getRelationReturnTypes()[$relation] ?? false;

if (
strpos(get_class($relationObj), 'Many') !== false ||
($this->getRelationReturnTypes()[$relation] ?? '') === 'many'
$relationReturnType === 'many' ||
(
!$relationReturnType &&
strpos(get_class($relationObj), 'Many') !== false
)
) {
//Collection or array of models (because Collection is Arrayable)
$relatedClass = '\\' . get_class($relationObj->getRelated());
Expand All @@ -782,8 +787,11 @@ public function getPropertiesFromMethods($model)
);
}
} elseif (
$relation === 'morphTo' ||
($this->getRelationReturnTypes()[$relation] ?? '') === 'morphTo'
$relationReturnType === 'morphTo' ||
(
!$relationReturnType &&
$relation === 'morphTo'
)
) {
// Model isn't specified because relation is polymorphic
$this->setProperty(
Expand Down
5 changes: 5 additions & 0 deletions tests/Console/ModelsCommand/Relations/Models/Simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,9 @@ public function relationSampleToAnyMorphedRelationType()
{
return $this->testToAnyMorphedRelation(Simple::class);
}

public function relationSampleToBadlyNamedNotManyRelation()
{
return $this->testToBadlyNamedNotManyRelation(Simple::class);
}
}
3 changes: 3 additions & 0 deletions tests/Console/ModelsCommand/Relations/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToAnyMorphedRelationType;
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToAnyRelationType;
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToBadlyNamedNotManyRelationType;
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToManyRelationType;
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToOneRelationType;
use Illuminate\Support\Facades\Config;
Expand All @@ -23,11 +24,13 @@ protected function setUp(): void
'testToManyRelation' => SampleToManyRelationType::class,
'testToAnyRelation' => SampleToAnyRelationType::class,
'testToAnyMorphedRelation' => SampleToAnyMorphedRelationType::class,
'testToBadlyNamedNotManyRelation' => SampleToBadlyNamedNotManyRelationType::class,
]);

Config::set('ide-helper.additional_relation_return_types', [
'testToAnyRelation' => 'many',
'testToAnyMorphedRelation' => 'morphTo',
'testToBadlyNamedNotManyRelation' => 'one',
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToAnyMorphedRelationType;
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToAnyRelationType;
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToBadlyNamedNotManyRelationType;
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToManyRelationType;
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToOneRelationType;

Expand Down Expand Up @@ -34,4 +35,10 @@ public function testToAnyMorphedRelation($related)
$instance = $this->newRelatedInstance($related);
return new SampleToAnyMorphedRelationType($instance->newQuery(), $this);
}

public function testToBadlyNamedNotManyRelation($related)
{
$instance = $this->newRelatedInstance($related);
return new SampleToBadlyNamedNotManyRelationType($instance->newQuery(), $this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels;
use Illuminate\Database\Eloquent\Relations\Relation;

/**
* Sample for custom relation
*
* the relation is a big fake and only for testing of the docblock generation
*
* @package Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations
*/
class SampleToBadlyNamedNotManyRelationType extends Relation
{
use SupportsDefaultModels;

public function addConstraints()
{
// Fake
}

public function addEagerConstraints(array $models)
{
// Fake
}

public function initRelation(array $models, $relation)
{
// Fake
}

public function match(array $models, Collection $results, $relation)
{
// Fake
}

public function getResults()
{
// Fake
}

protected function newRelatedInstanceFor(Model $parent)
{
// Fake
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public function nullableMixedWithForeignKeyConstraint(): BelongsTo
* @property-read Model|\Eloquent $relationSampleToAnyMorphedRelationType
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationSampleToAnyRelationType
* @property-read int|null $relation_sample_to_any_relation_type_count
* @property-read Simple $relationSampleToBadlyNamedNotManyRelation
* @property-read Simple $relationSampleToManyRelationType
* @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery()
Expand Down Expand Up @@ -257,4 +258,9 @@ public function relationSampleToAnyMorphedRelationType()
{
return $this->testToAnyMorphedRelation(Simple::class);
}

public function relationSampleToBadlyNamedNotManyRelation()
{
return $this->testToBadlyNamedNotManyRelation(Simple::class);
}
}

0 comments on commit cee441c

Please sign in to comment.