From fcb7ab169373f76cd604913c061e993e6a868c59 Mon Sep 17 00:00:00 2001 From: amir Date: Wed, 15 Apr 2020 20:26:53 +0430 Subject: [PATCH 1/2] Add PHPDoc for model relationships --- src/Generators/ModelGenerator.php | 6 +++++- stubs/model/method-comment.stub | 3 +++ tests/Feature/Generator/ModelGeneratorTest.php | 15 +++++++++++++++ tests/fixtures/models/model-configured.php | 6 ++++++ tests/fixtures/models/model-relationships.php | 12 ++++++++++++ tests/fixtures/models/relationships.php | 6 ++++++ tests/fixtures/models/soft-deletes-phpdoc.php | 3 +++ tests/fixtures/models/soft-deletes.php | 3 +++ tests/fixtures/models/unconventional.php | 6 ++++++ 9 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 stubs/model/method-comment.stub diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 0cf82899..79ed1dac 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -121,6 +121,7 @@ private function buildRelationships(Model $model) { $methods = ''; $template = $this->files->stub('model/method.stub'); + $commentTemplate = $this->files->stub('model/method-comment.stub'); foreach ($model->relationships() as $type => $references) { foreach ($references as $reference) { @@ -138,7 +139,10 @@ private function buildRelationships(Model $model) $method_name = $type === 'hasMany' ? Str::plural($name) : $name; $method = str_replace('DummyName', Str::camel($method_name), $template); $method = str_replace('null', $relationship, $method); - $methods .= PHP_EOL . $method; + + $phpDoc = str_replace('DummyReturn', '\Illuminate\Database\Eloquent\Relations\\' . Str::ucfirst($type), $commentTemplate); + + $methods .= PHP_EOL . $phpDoc . $method; } } diff --git a/stubs/model/method-comment.stub b/stubs/model/method-comment.stub new file mode 100644 index 00000000..e01695a2 --- /dev/null +++ b/stubs/model/method-comment.stub @@ -0,0 +1,3 @@ + /** + * @return DummyReturn + */ diff --git a/tests/Feature/Generator/ModelGeneratorTest.php b/tests/Feature/Generator/ModelGeneratorTest.php index 64f6e582..a5a4ce2c 100644 --- a/tests/Feature/Generator/ModelGeneratorTest.php +++ b/tests/Feature/Generator/ModelGeneratorTest.php @@ -75,6 +75,10 @@ public function output_generates_models($definition, $path, $model) ->with('model/method.stub') ->andReturn(file_get_contents('stubs/model/method.stub')); + $this->files->shouldReceive('stub') + ->with('model/method-comment.stub') + ->andReturn(file_get_contents('stubs/model/method-comment.stub')); + $this->files->expects('exists') ->with(dirname($path)) ->andReturnTrue(); @@ -104,6 +108,9 @@ public function output_generates_relationships() $this->files->expects('stub') ->with('model/method.stub') ->andReturn(file_get_contents('stubs/model/method.stub')); + $this->files->expects('stub') + ->with('model/method-comment.stub') + ->andReturn(file_get_contents('stubs/model/method-comment.stub')); $this->files->expects('exists') ->with('app') @@ -142,6 +149,10 @@ public function output_respects_configuration() ->with('model/method.stub') ->andReturn(file_get_contents('stubs/model/method.stub')); + $this->files->expects('stub') + ->with('model/method-comment.stub') + ->andReturn(file_get_contents('stubs/model/method-comment.stub')); + $this->files->expects('exists') ->with('src/path/Models') ->andReturnFalse(); @@ -186,6 +197,10 @@ public function output_generates_phpdoc_for_model($definition, $path, $model) ->with('model/method.stub') ->andReturn(file_get_contents('stubs/model/method.stub')); + $this->files->shouldReceive('stub') + ->with('model/method-comment.stub') + ->andReturn(file_get_contents('stubs/model/method-comment.stub')); + $this->files->expects('exists') ->with(dirname($path)) ->andReturnTrue(); diff --git a/tests/fixtures/models/model-configured.php b/tests/fixtures/models/model-configured.php index cc8d9f1d..e99563a8 100644 --- a/tests/fixtures/models/model-configured.php +++ b/tests/fixtures/models/model-configured.php @@ -28,11 +28,17 @@ class Comment extends Model ]; + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ public function post() { return $this->belongsTo(\Some\App\Models\Post::class); } + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ public function author() { return $this->belongsTo(\Some\App\Models\User::class); diff --git a/tests/fixtures/models/model-relationships.php b/tests/fixtures/models/model-relationships.php index e057dbd7..becf003e 100644 --- a/tests/fixtures/models/model-relationships.php +++ b/tests/fixtures/models/model-relationships.php @@ -26,21 +26,33 @@ class Subscription extends Model ]; + /** + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ public function orders() { return $this->hasMany(\App\Order::class); } + /** + * @return \Illuminate\Database\Eloquent\Relations\HasOne + */ public function duration() { return $this->hasOne(\App\Duration::class); } + /** + * @return \Illuminate\Database\Eloquent\Relations\HasOne + */ public function transaction() { return $this->hasOne(\App\Transaction::class); } + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ public function user() { return $this->belongsTo(\App\User::class); diff --git a/tests/fixtures/models/relationships.php b/tests/fixtures/models/relationships.php index fb791565..79b23d6a 100644 --- a/tests/fixtures/models/relationships.php +++ b/tests/fixtures/models/relationships.php @@ -28,11 +28,17 @@ class Comment extends Model ]; + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ public function post() { return $this->belongsTo(\App\Post::class); } + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ public function author() { return $this->belongsTo(\App\User::class); diff --git a/tests/fixtures/models/soft-deletes-phpdoc.php b/tests/fixtures/models/soft-deletes-phpdoc.php index 8a1958ec..ca76e81e 100644 --- a/tests/fixtures/models/soft-deletes-phpdoc.php +++ b/tests/fixtures/models/soft-deletes-phpdoc.php @@ -36,6 +36,9 @@ class Comment extends Model ]; + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ public function post() { return $this->belongsTo(\App\Post::class); diff --git a/tests/fixtures/models/soft-deletes.php b/tests/fixtures/models/soft-deletes.php index 1f3df1b9..f5510c35 100644 --- a/tests/fixtures/models/soft-deletes.php +++ b/tests/fixtures/models/soft-deletes.php @@ -29,6 +29,9 @@ class Comment extends Model ]; + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ public function post() { return $this->belongsTo(\App\Post::class); diff --git a/tests/fixtures/models/unconventional.php b/tests/fixtures/models/unconventional.php index ed640e47..a08a4fbe 100644 --- a/tests/fixtures/models/unconventional.php +++ b/tests/fixtures/models/unconventional.php @@ -31,11 +31,17 @@ class Team extends Model ]; + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ public function owner() { return $this->belongsTo(\App\Owner::class); } + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ public function manager() { return $this->belongsTo(\App\User::class); From 9120aa353b575a6e53770c75621760fc4e051a90 Mon Sep 17 00:00:00 2001 From: amir Date: Fri, 17 Apr 2020 13:03:00 +0430 Subject: [PATCH 2/2] Add phpdoc config for model relations phpdoc --- src/Generators/ModelGenerator.php | 6 ++- .../Feature/Generator/ModelGeneratorTest.php | 8 +-- tests/fixtures/models/model-configured.php | 6 --- tests/fixtures/models/model-relationships.php | 16 +----- .../fixtures/models/relationships-phpdoc.php | 53 +++++++++++++++++++ tests/fixtures/models/relationships.php | 6 --- tests/fixtures/models/soft-deletes.php | 3 -- tests/fixtures/models/unconventional.php | 6 --- 8 files changed, 60 insertions(+), 44 deletions(-) create mode 100644 tests/fixtures/models/relationships-phpdoc.php diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index d37145a7..eefd2d12 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -121,7 +121,11 @@ private function buildRelationships(Model $model) { $methods = ''; $template = $this->files->stub('model/method.stub'); - $commentTemplate = $this->files->stub('model/method-comment.stub'); + $commentTemplate = ''; + + if (config('blueprint.generate_phpdocs')) { + $commentTemplate = $this->files->stub('model/method-comment.stub'); + } foreach ($model->relationships() as $type => $references) { foreach ($references as $reference) { diff --git a/tests/Feature/Generator/ModelGeneratorTest.php b/tests/Feature/Generator/ModelGeneratorTest.php index a5a4ce2c..97a60c47 100644 --- a/tests/Feature/Generator/ModelGeneratorTest.php +++ b/tests/Feature/Generator/ModelGeneratorTest.php @@ -108,9 +108,6 @@ public function output_generates_relationships() $this->files->expects('stub') ->with('model/method.stub') ->andReturn(file_get_contents('stubs/model/method.stub')); - $this->files->expects('stub') - ->with('model/method-comment.stub') - ->andReturn(file_get_contents('stubs/model/method-comment.stub')); $this->files->expects('exists') ->with('app') @@ -149,10 +146,6 @@ public function output_respects_configuration() ->with('model/method.stub') ->andReturn(file_get_contents('stubs/model/method.stub')); - $this->files->expects('stub') - ->with('model/method-comment.stub') - ->andReturn(file_get_contents('stubs/model/method-comment.stub')); - $this->files->expects('exists') ->with('src/path/Models') ->andReturnFalse(); @@ -230,6 +223,7 @@ public function docBlockModelsDataProvider() return [ ['definitions/readme-example.bp', 'app/Post.php', 'models/readme-example-phpdoc.php'], ['definitions/soft-deletes.bp', 'app/Comment.php', 'models/soft-deletes-phpdoc.php'], + ['definitions/relationships.bp', 'app/Comment.php', 'models/relationships-phpdoc.php'], ['definitions/disable-auto-columns.bp', 'app/State.php', 'models/disable-auto-columns-phpdoc.php'], ]; } diff --git a/tests/fixtures/models/model-configured.php b/tests/fixtures/models/model-configured.php index e99563a8..cc8d9f1d 100644 --- a/tests/fixtures/models/model-configured.php +++ b/tests/fixtures/models/model-configured.php @@ -28,17 +28,11 @@ class Comment extends Model ]; - /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ public function post() { return $this->belongsTo(\Some\App\Models\Post::class); } - /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ public function author() { return $this->belongsTo(\Some\App\Models\User::class); diff --git a/tests/fixtures/models/model-relationships.php b/tests/fixtures/models/model-relationships.php index af4523b4..9accc009 100644 --- a/tests/fixtures/models/model-relationships.php +++ b/tests/fixtures/models/model-relationships.php @@ -25,41 +25,27 @@ class Subscription extends Model 'user_id' => 'integer', ]; - /** - * @return \Illuminate\Database\Eloquent\Relations\HasMany - */ + public function teams() { return $this->belongsToMany(\App\Team::class); } - /** - * @return \Illuminate\Database\Eloquent\Relations\HasMany - */ public function orders() { return $this->hasMany(\App\Order::class); } - /** - * @return \Illuminate\Database\Eloquent\Relations\HasOne - */ public function duration() { return $this->hasOne(\App\Duration::class); } - /** - * @return \Illuminate\Database\Eloquent\Relations\HasOne - */ public function transaction() { return $this->hasOne(\App\Transaction::class); } - /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ public function user() { return $this->belongsTo(\App\User::class); diff --git a/tests/fixtures/models/relationships-phpdoc.php b/tests/fixtures/models/relationships-phpdoc.php new file mode 100644 index 00000000..68b0cd87 --- /dev/null +++ b/tests/fixtures/models/relationships-phpdoc.php @@ -0,0 +1,53 @@ + 'integer', + 'post_id' => 'integer', + 'author_id' => 'integer', + ]; + + + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function post() + { + return $this->belongsTo(\App\Post::class); + } + + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function author() + { + return $this->belongsTo(\App\User::class); + } +} diff --git a/tests/fixtures/models/relationships.php b/tests/fixtures/models/relationships.php index 79b23d6a..fb791565 100644 --- a/tests/fixtures/models/relationships.php +++ b/tests/fixtures/models/relationships.php @@ -28,17 +28,11 @@ class Comment extends Model ]; - /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ public function post() { return $this->belongsTo(\App\Post::class); } - /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ public function author() { return $this->belongsTo(\App\User::class); diff --git a/tests/fixtures/models/soft-deletes.php b/tests/fixtures/models/soft-deletes.php index f5510c35..1f3df1b9 100644 --- a/tests/fixtures/models/soft-deletes.php +++ b/tests/fixtures/models/soft-deletes.php @@ -29,9 +29,6 @@ class Comment extends Model ]; - /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ public function post() { return $this->belongsTo(\App\Post::class); diff --git a/tests/fixtures/models/unconventional.php b/tests/fixtures/models/unconventional.php index a08a4fbe..ed640e47 100644 --- a/tests/fixtures/models/unconventional.php +++ b/tests/fixtures/models/unconventional.php @@ -31,17 +31,11 @@ class Team extends Model ]; - /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ public function owner() { return $this->belongsTo(\App\Owner::class); } - /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ public function manager() { return $this->belongsTo(\App\User::class);