Skip to content

Commit f6be94c

Browse files
authored
Add additional phpdocs annotation (#427)
1 parent e966567 commit f6be94c

File tree

5 files changed

+99
-7
lines changed

5 files changed

+99
-7
lines changed

src/Generators/ModelGenerator.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,30 @@ protected function buildClassPhpDoc(Model $model)
9191
$phpDoc .= PHP_EOL;
9292
/** @var Column $column */
9393
foreach ($model->columns() as $column) {
94-
$phpDoc .= sprintf(' * @property %s $%s', $this->phpDataType($column->dataType()), $column->name());
95-
$phpDoc .= PHP_EOL;
94+
if ($column->dataType() === 'morphs') {
95+
$phpDoc .= ' * @property int $'. $column->name() . '_id';
96+
$phpDoc .= PHP_EOL;
97+
$phpDoc .= ' * @property string $'. $column->name() . '_type';
98+
$phpDoc .= PHP_EOL;
99+
} elseif ($column->dataType() === 'nullableMorphs') {
100+
$phpDoc .= ' * @property int|null $'. $column->name() . '_id';
101+
$phpDoc .= PHP_EOL;
102+
$phpDoc .= ' * @property string|null $'. $column->name() . '_type';
103+
$phpDoc .= PHP_EOL;
104+
} elseif ($column->dataType() === 'uuidMorphs') {
105+
$phpDoc .= ' * @property string $'. $column->name() . '_id';
106+
$phpDoc .= PHP_EOL;
107+
$phpDoc .= ' * @property string $'. $column->name() . '_type';
108+
$phpDoc .= PHP_EOL;
109+
} elseif ($column->dataType() === 'nullableUuidMorphs') {
110+
$phpDoc .= ' * @property string|null $'. $column->name() . '_id';
111+
$phpDoc .= PHP_EOL;
112+
$phpDoc .= ' * @property string|null $'. $column->name() . '_type';
113+
$phpDoc .= PHP_EOL;
114+
} else {
115+
$phpDoc .= sprintf(' * @property %s $%s', $this->phpDataType($column->dataType()), $column->name());
116+
$phpDoc .= PHP_EOL;
117+
}
96118
}
97119

98120
if ($model->usesSoftDeletes()) {

tests/Feature/Generators/ModelGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@ public function docBlockModelsDataProvider()
676676
['drafts/relationships.yaml', 'app/Comment.php', 'models/relationships-phpdoc.php'],
677677
['drafts/disable-auto-columns.yaml', 'app/State.php', 'models/disable-auto-columns-phpdoc.php'],
678678
['drafts/foreign-key-shorthand.yaml', 'app/Comment.php', 'models/foreign-key-shorthand-phpdoc.php'],
679+
['drafts/optimize.yaml', 'app/Optimize.php', 'models/optimize.php'],
679680
];
680681
}
681682

tests/fixtures/drafts/optimize.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ models:
66
int: integer unsigned
77
dec: decimal:8,2 unsigned
88
big: bigInteger unsigned
9-
foo: morphs nullable
10-
foobar: uuidMorphs nullable
11-
timestamps: false
9+
foo: morphs
10+
bar: uuidMorphs nullable
11+
baz: nullableMorphs
12+
foobar: uuidMorphs
13+
foobarbaz: nullableUuidMorphs
14+
timestamps: true

tests/fixtures/migrations/optimize.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ public function up()
2121
$table->unsignedInteger('int');
2222
$table->unsignedDecimal('dec', 8, 2);
2323
$table->unsignedBigInteger('big');
24-
$table->nullableMorphs('foo');
25-
$table->nullableUuidMorphs('foobar');
24+
$table->morphs('foo');
25+
$table->nullableUuidMorphs('bar');
26+
$table->nullableMorphs('baz');
27+
$table->uuidMorphs('foobar');
28+
$table->nullableUuidMorphs('foobarbaz');
29+
$table->timestamps();
2630
});
2731
}
2832

tests/fixtures/models/optimize.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
/**
8+
* @property int $id
9+
* @property int $tiny
10+
* @property int $small
11+
* @property int $medium
12+
* @property int $int
13+
* @property float $dec
14+
* @property int $big
15+
* @property int $foo_id
16+
* @property string $foo_type
17+
* @property string $bar_id
18+
* @property string $bar_type
19+
* @property int|null $baz_id
20+
* @property string|null $baz_type
21+
* @property string $foobar_id
22+
* @property string $foobar_type
23+
* @property string|null $foobarbaz_id
24+
* @property string|null $foobarbaz_type
25+
* @property \Carbon\Carbon $created_at
26+
* @property \Carbon\Carbon $updated_at
27+
*/
28+
class Optimize extends Model
29+
{
30+
/**
31+
* The attributes that are mass assignable.
32+
*
33+
* @var array
34+
*/
35+
protected $fillable = [
36+
'tiny',
37+
'small',
38+
'medium',
39+
'int',
40+
'dec',
41+
'big',
42+
'foo',
43+
'bar',
44+
'baz',
45+
'foobar',
46+
'foobarbaz',
47+
];
48+
49+
/**
50+
* The attributes that should be cast to native types.
51+
*
52+
* @var array
53+
*/
54+
protected $casts = [
55+
'id' => 'integer',
56+
'tiny' => 'integer',
57+
'small' => 'integer',
58+
'medium' => 'integer',
59+
'dec' => 'decimal:2',
60+
'big' => 'integer',
61+
];
62+
}

0 commit comments

Comments
 (0)