From 8f4235cacb74511a46e1e3e0397744b74c57bdfa Mon Sep 17 00:00:00 2001 From: Ben James Date: Wed, 29 Dec 2021 11:21:20 +0000 Subject: [PATCH] Resolves #484 - Do not use FQCN for model relationships (for models in same namespace) Signed-off-by: Ben James --- src/Generators/ModelGenerator.php | 1 + tests/fixtures/models/alias-relationships.php | 6 +++--- .../models/certificate-pascal-case-example.php | 2 +- .../models/certificate-type-pascal-case-example.php | 2 +- tests/fixtures/models/custom-pivot-table-name.php | 2 +- .../fixtures/models/foreign-key-shorthand-phpdoc.php | 6 +++--- tests/fixtures/models/model-configured.php | 4 ++-- ...del-relationships-morphone-morphmany-with-fqn.php | 2 +- .../model-relationships-with-full-namespace.php | 4 ++-- tests/fixtures/models/model-relationships.php | 12 ++++++------ tests/fixtures/models/nested-models.php | 2 +- .../models/post-polymorphic-relationship.php | 2 +- tests/fixtures/models/readme-example-phpdoc.php | 2 +- tests/fixtures/models/readme-example.php | 2 +- tests/fixtures/models/relationships-phpdoc.php | 4 ++-- tests/fixtures/models/relationships.php | 4 ++-- tests/fixtures/models/return-type-declarations.php | 8 ++++---- tests/fixtures/models/soft-deletes-phpdoc.php | 2 +- tests/fixtures/models/soft-deletes.php | 2 +- tests/fixtures/models/unconventional.php | 4 ++-- .../models/user-polymorphic-relationship.php | 2 +- 21 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 35102d7b..5b3a3bb0 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -209,6 +209,7 @@ protected function buildRelationships(Model $model) } $fqcn = Str::startsWith($fqcn, '\\') ? $fqcn : '\\' . $fqcn; + $fqcn = Str::is($fqcn, "\\{$model->fullyQualifiedNamespace()}\\{$class_name}") ? $class_name : $fqcn; if ($type === 'morphTo') { $relationship = sprintf('$this->%s()', $type); diff --git a/tests/fixtures/models/alias-relationships.php b/tests/fixtures/models/alias-relationships.php index dc330267..2c671a97 100644 --- a/tests/fixtures/models/alias-relationships.php +++ b/tests/fixtures/models/alias-relationships.php @@ -29,16 +29,16 @@ class Salesman extends Model public function lead() { - return $this->hasOne(\App\User::class); + return $this->hasOne(User::class); } public function methodNames() { - return $this->hasMany(\App\ClassName::class); + return $this->hasMany(ClassName::class); } public function methodName() { - return $this->belongsTo(\App\ClassName::class); + return $this->belongsTo(ClassName::class); } } diff --git a/tests/fixtures/models/certificate-pascal-case-example.php b/tests/fixtures/models/certificate-pascal-case-example.php index 3b37a51c..e15d9b18 100644 --- a/tests/fixtures/models/certificate-pascal-case-example.php +++ b/tests/fixtures/models/certificate-pascal-case-example.php @@ -36,6 +36,6 @@ class Certificate extends Model public function certificateType() { - return $this->belongsTo(\App\CertificateType::class); + return $this->belongsTo(CertificateType::class); } } diff --git a/tests/fixtures/models/certificate-type-pascal-case-example.php b/tests/fixtures/models/certificate-type-pascal-case-example.php index f5f2fe47..51a22f24 100644 --- a/tests/fixtures/models/certificate-type-pascal-case-example.php +++ b/tests/fixtures/models/certificate-type-pascal-case-example.php @@ -29,6 +29,6 @@ class CertificateType extends Model public function certificates() { - return $this->hasMany(\App\Certificate::class); + return $this->hasMany(Certificate::class); } } diff --git a/tests/fixtures/models/custom-pivot-table-name.php b/tests/fixtures/models/custom-pivot-table-name.php index 8a2a9c4f..91c7e332 100644 --- a/tests/fixtures/models/custom-pivot-table-name.php +++ b/tests/fixtures/models/custom-pivot-table-name.php @@ -38,6 +38,6 @@ class User extends Model public function accounts() { - return $this->belongsToMany(\App\Account::class, 'test'); + return $this->belongsToMany(Account::class, 'test'); } } diff --git a/tests/fixtures/models/foreign-key-shorthand-phpdoc.php b/tests/fixtures/models/foreign-key-shorthand-phpdoc.php index dc690561..1ec483d8 100644 --- a/tests/fixtures/models/foreign-key-shorthand-phpdoc.php +++ b/tests/fixtures/models/foreign-key-shorthand-phpdoc.php @@ -45,7 +45,7 @@ class Comment extends Model */ public function post() { - return $this->belongsTo(\App\Post::class); + return $this->belongsTo(Post::class); } /** @@ -53,7 +53,7 @@ public function post() */ public function author() { - return $this->belongsTo(\App\User::class); + return $this->belongsTo(User::class); } /** @@ -61,6 +61,6 @@ public function author() */ public function country() { - return $this->belongsTo(\App\Country::class, 'ccid', 'code'); + return $this->belongsTo(Country::class, 'ccid', 'code'); } } diff --git a/tests/fixtures/models/model-configured.php b/tests/fixtures/models/model-configured.php index b47f8bd3..2251d058 100644 --- a/tests/fixtures/models/model-configured.php +++ b/tests/fixtures/models/model-configured.php @@ -32,11 +32,11 @@ class Comment extends Model public function post() { - return $this->belongsTo(\Some\App\Models\Post::class); + return $this->belongsTo(Post::class); } public function author() { - return $this->belongsTo(\Some\App\Models\User::class); + return $this->belongsTo(User::class); } } diff --git a/tests/fixtures/models/model-relationships-morphone-morphmany-with-fqn.php b/tests/fixtures/models/model-relationships-morphone-morphmany-with-fqn.php index 78f05806..50ff7e20 100644 --- a/tests/fixtures/models/model-relationships-morphone-morphmany-with-fqn.php +++ b/tests/fixtures/models/model-relationships-morphone-morphmany-with-fqn.php @@ -45,6 +45,6 @@ public function lines() public function user() { - return $this->belongsTo(\App\User::class); + return $this->belongsTo(User::class); } } diff --git a/tests/fixtures/models/model-relationships-with-full-namespace.php b/tests/fixtures/models/model-relationships-with-full-namespace.php index ac8170ef..c8c00d03 100644 --- a/tests/fixtures/models/model-relationships-with-full-namespace.php +++ b/tests/fixtures/models/model-relationships-with-full-namespace.php @@ -51,11 +51,11 @@ public function transaction() public function user() { - return $this->belongsTo(\App\User::class); + return $this->belongsTo(User::class); } public function product() { - return $this->belongsTo(\App\Product::class); + return $this->belongsTo(Product::class); } } diff --git a/tests/fixtures/models/model-relationships.php b/tests/fixtures/models/model-relationships.php index 859ecb81..2518744a 100644 --- a/tests/fixtures/models/model-relationships.php +++ b/tests/fixtures/models/model-relationships.php @@ -31,31 +31,31 @@ class Subscription extends Model public function teams() { - return $this->belongsToMany(\App\Team::class); + return $this->belongsToMany(Team::class); } public function orders() { - return $this->hasMany(\App\Order::class); + return $this->hasMany(Order::class); } public function duration() { - return $this->hasOne(\App\Duration::class); + return $this->hasOne(Duration::class); } public function transaction() { - return $this->hasOne(\App\Transaction::class); + return $this->hasOne(Transaction::class); } public function user() { - return $this->belongsTo(\App\User::class); + return $this->belongsTo(User::class); } public function product() { - return $this->belongsTo(\App\Product::class); + return $this->belongsTo(Product::class); } } diff --git a/tests/fixtures/models/nested-models.php b/tests/fixtures/models/nested-models.php index 696e7621..1a127d51 100644 --- a/tests/fixtures/models/nested-models.php +++ b/tests/fixtures/models/nested-models.php @@ -34,7 +34,7 @@ class ScreeningQuestion extends Model public function report() { - return $this->belongsTo(\App\Models\Screening\Report::class); + return $this->belongsTo(Report::class); } public function appointmentType() diff --git a/tests/fixtures/models/post-polymorphic-relationship.php b/tests/fixtures/models/post-polymorphic-relationship.php index 3bf515b7..fba86178 100644 --- a/tests/fixtures/models/post-polymorphic-relationship.php +++ b/tests/fixtures/models/post-polymorphic-relationship.php @@ -29,6 +29,6 @@ class Post extends Model public function images() { - return $this->morphMany(\App\Image::class, 'imageable'); + return $this->morphMany(Image::class, 'imageable'); } } diff --git a/tests/fixtures/models/readme-example-phpdoc.php b/tests/fixtures/models/readme-example-phpdoc.php index 5a02d9bc..ca0f8761 100644 --- a/tests/fixtures/models/readme-example-phpdoc.php +++ b/tests/fixtures/models/readme-example-phpdoc.php @@ -46,6 +46,6 @@ class Post extends Model */ public function author() { - return $this->belongsTo(\App\User::class); + return $this->belongsTo(User::class); } } diff --git a/tests/fixtures/models/readme-example.php b/tests/fixtures/models/readme-example.php index bc6312da..34d8f13f 100644 --- a/tests/fixtures/models/readme-example.php +++ b/tests/fixtures/models/readme-example.php @@ -34,6 +34,6 @@ class Post extends Model public function author() { - return $this->belongsTo(\App\User::class); + return $this->belongsTo(User::class); } } diff --git a/tests/fixtures/models/relationships-phpdoc.php b/tests/fixtures/models/relationships-phpdoc.php index c9ffb396..24ede252 100644 --- a/tests/fixtures/models/relationships-phpdoc.php +++ b/tests/fixtures/models/relationships-phpdoc.php @@ -42,7 +42,7 @@ class Comment extends Model */ public function post() { - return $this->belongsTo(\App\Post::class); + return $this->belongsTo(Post::class); } /** @@ -50,6 +50,6 @@ public function post() */ public function author() { - return $this->belongsTo(\App\User::class); + return $this->belongsTo(User::class); } } diff --git a/tests/fixtures/models/relationships.php b/tests/fixtures/models/relationships.php index 0cc2f8c4..1fe416ff 100644 --- a/tests/fixtures/models/relationships.php +++ b/tests/fixtures/models/relationships.php @@ -32,11 +32,11 @@ class Comment extends Model public function post() { - return $this->belongsTo(\App\Post::class); + return $this->belongsTo(Post::class); } public function author() { - return $this->belongsTo(\App\User::class); + return $this->belongsTo(User::class); } } diff --git a/tests/fixtures/models/return-type-declarations.php b/tests/fixtures/models/return-type-declarations.php index 241156c1..a29c66b2 100644 --- a/tests/fixtures/models/return-type-declarations.php +++ b/tests/fixtures/models/return-type-declarations.php @@ -43,21 +43,21 @@ class Term extends Model public function organizers(): \Illuminate\Database\Eloquent\Relations\BelongsToMany { - return $this->belongsToMany(\App\Organizer::class); + return $this->belongsToMany(Organizer::class); } public function events(): \Illuminate\Database\Eloquent\Relations\BelongsToMany { - return $this->belongsToMany(\App\Event::class); + return $this->belongsToMany(Event::class); } public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo { - return $this->belongsTo(\App\User::class); + return $this->belongsTo(User::class); } public function team(): \Illuminate\Database\Eloquent\Relations\BelongsTo { - return $this->belongsTo(\App\Team::class); + return $this->belongsTo(Team::class); } } diff --git a/tests/fixtures/models/soft-deletes-phpdoc.php b/tests/fixtures/models/soft-deletes-phpdoc.php index 7ec4eea8..df32ee11 100644 --- a/tests/fixtures/models/soft-deletes-phpdoc.php +++ b/tests/fixtures/models/soft-deletes-phpdoc.php @@ -41,6 +41,6 @@ class Comment extends Model */ public function post() { - return $this->belongsTo(\App\Post::class); + return $this->belongsTo(Post::class); } } diff --git a/tests/fixtures/models/soft-deletes.php b/tests/fixtures/models/soft-deletes.php index ec2b301f..202b1be9 100644 --- a/tests/fixtures/models/soft-deletes.php +++ b/tests/fixtures/models/soft-deletes.php @@ -31,6 +31,6 @@ class Comment extends Model public function post() { - return $this->belongsTo(\App\Post::class); + return $this->belongsTo(Post::class); } } diff --git a/tests/fixtures/models/unconventional.php b/tests/fixtures/models/unconventional.php index d242a7f5..f43464b8 100644 --- a/tests/fixtures/models/unconventional.php +++ b/tests/fixtures/models/unconventional.php @@ -35,11 +35,11 @@ class Team extends Model public function owner() { - return $this->belongsTo(\App\Owner::class); + return $this->belongsTo(Owner::class); } public function manager() { - return $this->belongsTo(\App\User::class); + return $this->belongsTo(User::class); } } diff --git a/tests/fixtures/models/user-polymorphic-relationship.php b/tests/fixtures/models/user-polymorphic-relationship.php index 122530cc..af8ee557 100644 --- a/tests/fixtures/models/user-polymorphic-relationship.php +++ b/tests/fixtures/models/user-polymorphic-relationship.php @@ -29,6 +29,6 @@ class User extends Model public function images() { - return $this->morphMany(\App\Image::class, 'imageable'); + return $this->morphMany(Image::class, 'imageable'); } }