From 0c056fd0223f402c25e8e01c52109db4fddc1e0f Mon Sep 17 00:00:00 2001 From: amir Date: Fri, 17 Apr 2020 20:41:53 +0430 Subject: [PATCH] Add PHPDoc for model relationships (#106) --- src/Generators/ModelGenerator.php | 10 +++- stubs/model/method-comment.stub | 3 ++ .../Feature/Generator/ModelGeneratorTest.php | 9 ++++ .../fixtures/models/relationships-phpdoc.php | 53 +++++++++++++++++++ tests/fixtures/models/soft-deletes-phpdoc.php | 3 ++ 5 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 stubs/model/method-comment.stub create mode 100644 tests/fixtures/models/relationships-phpdoc.php diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index d0250370..eefd2d12 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -121,6 +121,11 @@ private function buildRelationships(Model $model) { $methods = ''; $template = $this->files->stub('model/method.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) { @@ -138,7 +143,10 @@ private function buildRelationships(Model $model) $method_name = $type === 'hasMany' || $type === 'belongsToMany' ? 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..97a60c47 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(); @@ -186,6 +190,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(); @@ -215,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/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/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);