diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index a503022a6..8f6242c30 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -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()); @@ -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( diff --git a/tests/Console/ModelsCommand/Relations/Models/Simple.php b/tests/Console/ModelsCommand/Relations/Models/Simple.php index 12e8c8774..bb8387cc8 100644 --- a/tests/Console/ModelsCommand/Relations/Models/Simple.php +++ b/tests/Console/ModelsCommand/Relations/Models/Simple.php @@ -107,4 +107,9 @@ public function relationSampleToAnyMorphedRelationType() { return $this->testToAnyMorphedRelation(Simple::class); } + + public function relationSampleToBadlyNamedNotManyRelation() + { + return $this->testToBadlyNamedNotManyRelation(Simple::class); + } } diff --git a/tests/Console/ModelsCommand/Relations/Test.php b/tests/Console/ModelsCommand/Relations/Test.php index 399574712..3c5e41c56 100644 --- a/tests/Console/ModelsCommand/Relations/Test.php +++ b/tests/Console/ModelsCommand/Relations/Test.php @@ -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; @@ -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', ]); } diff --git a/tests/Console/ModelsCommand/Relations/Traits/HasTestRelations.php b/tests/Console/ModelsCommand/Relations/Traits/HasTestRelations.php index 0d252605e..d63b9042d 100644 --- a/tests/Console/ModelsCommand/Relations/Traits/HasTestRelations.php +++ b/tests/Console/ModelsCommand/Relations/Traits/HasTestRelations.php @@ -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; @@ -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); + } } diff --git a/tests/Console/ModelsCommand/Relations/Types/SampleToBadlyNamedNotManyRelationType.php b/tests/Console/ModelsCommand/Relations/Types/SampleToBadlyNamedNotManyRelationType.php new file mode 100644 index 000000000..a8a753a60 --- /dev/null +++ b/tests/Console/ModelsCommand/Relations/Types/SampleToBadlyNamedNotManyRelationType.php @@ -0,0 +1,52 @@ + $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() @@ -257,4 +258,9 @@ public function relationSampleToAnyMorphedRelationType() { return $this->testToAnyMorphedRelation(Simple::class); } + + public function relationSampleToBadlyNamedNotManyRelation() + { + return $this->testToBadlyNamedNotManyRelation(Simple::class); + } }