Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,30 @@ protected function buildClassPhpDoc(Model $model)
$phpDoc .= PHP_EOL;
/** @var Column $column */
foreach ($model->columns() as $column) {
$phpDoc .= sprintf(' * @property %s $%s', $this->phpDataType($column->dataType()), $column->name());
$phpDoc .= PHP_EOL;
if ($column->dataType() === 'morphs') {
$phpDoc .= ' * @property int $'. $column->name() . '_id';
$phpDoc .= PHP_EOL;
$phpDoc .= ' * @property string $'. $column->name() . '_type';
$phpDoc .= PHP_EOL;
} elseif ($column->dataType() === 'nullableMorphs') {
$phpDoc .= ' * @property int|null $'. $column->name() . '_id';
$phpDoc .= PHP_EOL;
$phpDoc .= ' * @property string|null $'. $column->name() . '_type';
$phpDoc .= PHP_EOL;
} elseif ($column->dataType() === 'uuidMorphs') {
$phpDoc .= ' * @property string $'. $column->name() . '_id';
$phpDoc .= PHP_EOL;
$phpDoc .= ' * @property string $'. $column->name() . '_type';
$phpDoc .= PHP_EOL;
} elseif ($column->dataType() === 'nullableUuidMorphs') {
$phpDoc .= ' * @property string|null $'. $column->name() . '_id';
$phpDoc .= PHP_EOL;
$phpDoc .= ' * @property string|null $'. $column->name() . '_type';
$phpDoc .= PHP_EOL;
} else {
$phpDoc .= sprintf(' * @property %s $%s', $this->phpDataType($column->dataType()), $column->name());
$phpDoc .= PHP_EOL;
}
}

if ($model->usesSoftDeletes()) {
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Generators/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ public function docBlockModelsDataProvider()
['drafts/relationships.yaml', 'app/Comment.php', 'models/relationships-phpdoc.php'],
['drafts/disable-auto-columns.yaml', 'app/State.php', 'models/disable-auto-columns-phpdoc.php'],
['drafts/foreign-key-shorthand.yaml', 'app/Comment.php', 'models/foreign-key-shorthand-phpdoc.php'],
['drafts/optimize.yaml', 'app/Optimize.php', 'models/optimize.php'],
];
}

Expand Down
9 changes: 6 additions & 3 deletions tests/fixtures/drafts/optimize.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ models:
int: integer unsigned
dec: decimal:8,2 unsigned
big: bigInteger unsigned
foo: morphs nullable
foobar: uuidMorphs nullable
timestamps: false
foo: morphs
bar: uuidMorphs nullable
baz: nullableMorphs
foobar: uuidMorphs
foobarbaz: nullableUuidMorphs
timestamps: true
8 changes: 6 additions & 2 deletions tests/fixtures/migrations/optimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ public function up()
$table->unsignedInteger('int');
$table->unsignedDecimal('dec', 8, 2);
$table->unsignedBigInteger('big');
$table->nullableMorphs('foo');
$table->nullableUuidMorphs('foobar');
$table->morphs('foo');
$table->nullableUuidMorphs('bar');
$table->nullableMorphs('baz');
$table->uuidMorphs('foobar');
$table->nullableUuidMorphs('foobarbaz');
$table->timestamps();
});
}

Expand Down
62 changes: 62 additions & 0 deletions tests/fixtures/models/optimize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
* @property int $id
* @property int $tiny
* @property int $small
* @property int $medium
* @property int $int
* @property float $dec
* @property int $big
* @property int $foo_id
* @property string $foo_type
* @property string $bar_id
* @property string $bar_type
* @property int|null $baz_id
* @property string|null $baz_type
* @property string $foobar_id
* @property string $foobar_type
* @property string|null $foobarbaz_id
* @property string|null $foobarbaz_type
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
*/
class Optimize extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'tiny',
'small',
'medium',
'int',
'dec',
'big',
'foo',
'bar',
'baz',
'foobar',
'foobarbaz',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
'tiny' => 'integer',
'small' => 'integer',
'medium' => 'integer',
'dec' => 'decimal:2',
'big' => 'integer',
];
}