Repository: laravel-shift/blueprint (php)

Issue:
Do not use FQCN for model relationships
Currently, the generated relationships use FQCN for the model. Usually, the models are in the same namespace like `App\Models\` so I would like to have an option to remove the namespace in the model. 

Current Behavior:
```php
class User extends Model
{
    public function company()
    {
        return $this->belongsTo(\App\Models\Company::class);
    }
}
```

Suggested:
```php
class User extends Model
{
    public function company()
    {
        return $this->belongsTo(Company::class);
    }
}
```

Pull request description:
Resolves #484 - Do not use FQCN for model relationships 
(for models in same namespace)

Patch:
diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php
index 35102d7..5b3a3bb 100644
--- a/src/Generators/ModelGenerator.php
+++ b/src/Generators/ModelGenerator.php
@@ -209,6 +209,7 @@ class ModelGenerator implements Generator
                 }
 
                 $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);


Test patch:
diff --git a/tests/fixtures/models/alias-relationships.php b/tests/fixtures/models/alias-relationships.php
index dc33026..2c671a9 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 3b37a51..e15d9b1 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 f5f2fe4..51a22f2 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 8a2a9c4..91c7e33 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 dc69056..1ec483d 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 @@ class Comment extends Model
      */
     public function author()
     {
-        return $this->belongsTo(\App\User::class);
+        return $this->belongsTo(User::class);
     }
 
     /**
@@ -61,6 +61,6 @@ class Comment extends Model
      */
     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 b47f8bd..2251d05 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 78f0580..50ff7e2 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 @@ class Flag extends Model
 
     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 ac8170e..c8c00d0 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 @@ class Recurrency extends Model
 
     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 859ecb8..2518744 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 696e762..1a127d5 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 3bf515b..fba8617 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 5a02d9b..ca0f876 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 bc6312d..34d8f13 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 c9ffb39..24ede25 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 @@ class Comment extends Model
      */
     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 0cc2f8c..1fe416f 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 241156c..a29c66b 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 7ec4eea..df32ee1 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 ec2b301..202b1be 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 d242a7f..f43464b 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 122530c..af8ee55 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');
     }
 }