Skip to content

Commit

Permalink
Add PHPDoc for model relationships (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
amir9480 authored Apr 17, 2020
1 parent 6faf201 commit 0c056fd
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}

Expand Down
3 changes: 3 additions & 0 deletions stubs/model/method-comment.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**
* @return DummyReturn
*/
9 changes: 9 additions & 0 deletions tests/Feature/Generator/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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'],
];
}
Expand Down
53 changes: 53 additions & 0 deletions tests/fixtures/models/relationships-phpdoc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
* @property int $id
* @property int $post_id
* @property int $author_id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
*/
class Comment extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'post_id',
'author_id',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => '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);
}
}
3 changes: 3 additions & 0 deletions tests/fixtures/models/soft-deletes-phpdoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class Comment extends Model
];


/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function post()
{
return $this->belongsTo(\App\Post::class);
Expand Down

0 comments on commit 0c056fd

Please sign in to comment.