Skip to content

Commit 0c056fd

Browse files
authored
Add PHPDoc for model relationships (#106)
1 parent 6faf201 commit 0c056fd

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

src/Generators/ModelGenerator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ private function buildRelationships(Model $model)
121121
{
122122
$methods = '';
123123
$template = $this->files->stub('model/method.stub');
124+
$commentTemplate = '';
125+
126+
if (config('blueprint.generate_phpdocs')) {
127+
$commentTemplate = $this->files->stub('model/method-comment.stub');
128+
}
124129

125130
foreach ($model->relationships() as $type => $references) {
126131
foreach ($references as $reference) {
@@ -138,7 +143,10 @@ private function buildRelationships(Model $model)
138143
$method_name = $type === 'hasMany' || $type === 'belongsToMany' ? Str::plural($name) : $name;
139144
$method = str_replace('DummyName', Str::camel($method_name), $template);
140145
$method = str_replace('null', $relationship, $method);
141-
$methods .= PHP_EOL . $method;
146+
147+
$phpDoc = str_replace('DummyReturn', '\Illuminate\Database\Eloquent\Relations\\' . Str::ucfirst($type), $commentTemplate);
148+
149+
$methods .= PHP_EOL . $phpDoc . $method;
142150
}
143151
}
144152

stubs/model/method-comment.stub

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/**
2+
* @return DummyReturn
3+
*/

tests/Feature/Generator/ModelGeneratorTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public function output_generates_models($definition, $path, $model)
7575
->with('model/method.stub')
7676
->andReturn(file_get_contents('stubs/model/method.stub'));
7777

78+
$this->files->shouldReceive('stub')
79+
->with('model/method-comment.stub')
80+
->andReturn(file_get_contents('stubs/model/method-comment.stub'));
81+
7882
$this->files->expects('exists')
7983
->with(dirname($path))
8084
->andReturnTrue();
@@ -186,6 +190,10 @@ public function output_generates_phpdoc_for_model($definition, $path, $model)
186190
->with('model/method.stub')
187191
->andReturn(file_get_contents('stubs/model/method.stub'));
188192

193+
$this->files->shouldReceive('stub')
194+
->with('model/method-comment.stub')
195+
->andReturn(file_get_contents('stubs/model/method-comment.stub'));
196+
189197
$this->files->expects('exists')
190198
->with(dirname($path))
191199
->andReturnTrue();
@@ -215,6 +223,7 @@ public function docBlockModelsDataProvider()
215223
return [
216224
['definitions/readme-example.bp', 'app/Post.php', 'models/readme-example-phpdoc.php'],
217225
['definitions/soft-deletes.bp', 'app/Comment.php', 'models/soft-deletes-phpdoc.php'],
226+
['definitions/relationships.bp', 'app/Comment.php', 'models/relationships-phpdoc.php'],
218227
['definitions/disable-auto-columns.bp', 'app/State.php', 'models/disable-auto-columns-phpdoc.php'],
219228
];
220229
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
/**
8+
* @property int $id
9+
* @property int $post_id
10+
* @property int $author_id
11+
* @property \Carbon\Carbon $created_at
12+
* @property \Carbon\Carbon $updated_at
13+
*/
14+
class Comment extends Model
15+
{
16+
/**
17+
* The attributes that are mass assignable.
18+
*
19+
* @var array
20+
*/
21+
protected $fillable = [
22+
'post_id',
23+
'author_id',
24+
];
25+
26+
/**
27+
* The attributes that should be cast to native types.
28+
*
29+
* @var array
30+
*/
31+
protected $casts = [
32+
'id' => 'integer',
33+
'post_id' => 'integer',
34+
'author_id' => 'integer',
35+
];
36+
37+
38+
/**
39+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
40+
*/
41+
public function post()
42+
{
43+
return $this->belongsTo(\App\Post::class);
44+
}
45+
46+
/**
47+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
48+
*/
49+
public function author()
50+
{
51+
return $this->belongsTo(\App\User::class);
52+
}
53+
}

tests/fixtures/models/soft-deletes-phpdoc.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class Comment extends Model
3636
];
3737

3838

39+
/**
40+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
41+
*/
3942
public function post()
4043
{
4144
return $this->belongsTo(\App\Post::class);

0 commit comments

Comments
 (0)