Skip to content

Commit 0956e01

Browse files
Alex Stoicajasonmccreary
authored andcommitted
Add Class PHPDoc for help IDE
1 parent 790a021 commit 0956e01

File tree

9 files changed

+90
-0
lines changed

9 files changed

+90
-0
lines changed

src/Generators/ModelGenerator.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ protected function populateStub(string $stub, Model $model)
4242
{
4343
$stub = str_replace('DummyNamespace', $model->fullyQualifiedNamespace(), $stub);
4444
$stub = str_replace('DummyClass', $model->name(), $stub);
45+
$stub = str_replace('/** DummyPHPDocClass **/', $this->buildClassPhpDoc($model), $stub);
4546

4647
$body = $this->buildProperties($model);
4748
$body .= PHP_EOL . PHP_EOL;
@@ -53,6 +54,19 @@ protected function populateStub(string $stub, Model $model)
5354
return $stub;
5455
}
5556

57+
private function buildClassPhpDoc(Model $model)
58+
{
59+
$phpDoc = '/**' . PHP_EOL;
60+
/** @var Column $column */
61+
foreach ($model->columns() as $column) {
62+
$phpDoc .= sprintf(' * @property %s $%s', $this->phpDataType($column->dataType()), $column->name());
63+
$phpDoc .= PHP_EOL;
64+
}
65+
$phpDoc .= ' */';
66+
67+
return $phpDoc;
68+
}
69+
5670
private function buildProperties(Model $model)
5771
{
5872
$properties = '';
@@ -188,4 +202,46 @@ private function addTraits(Model $model, $stub)
188202

189203
return $stub;
190204
}
205+
206+
private function phpDataType(string $dataType)
207+
{
208+
static $php_data_types = [
209+
'id' => 'int',
210+
'bigincrements' => 'int',
211+
'biginteger' => 'int',
212+
'boolean' => 'bool',
213+
'date' => '\Carbon\Carbon',
214+
'datetime' => '\Carbon\Carbon',
215+
'datetimetz' => '\Carbon\Carbon',
216+
'decimal' => 'float',
217+
'double' => 'double',
218+
'float' => 'float',
219+
'increments' => 'int',
220+
'integer' => 'int',
221+
'mediumincrements' => 'int',
222+
'mediuminteger' => 'int',
223+
'nullabletimestamps' => '\Carbon\Carbon',
224+
'smallincrements' => 'int',
225+
'smallinteger' => 'int',
226+
'softdeletes' => '\Carbon\Carbon',
227+
'softdeletestz' => '\Carbon\Carbon',
228+
'time' => '\Carbon\Carbon',
229+
'timetz' => '\Carbon\Carbon',
230+
'timestamp' => '\Carbon\Carbon',
231+
'timestamptz' => '\Carbon\Carbon',
232+
'timestamps' => '\Carbon\Carbon',
233+
'timestampstz' => '\Carbon\Carbon',
234+
'tinyincrements' => 'integer',
235+
'tinyinteger' => 'int',
236+
'unsignedbiginteger' => 'int',
237+
'unsigneddecimal' => 'float',
238+
'unsignedinteger' => 'int',
239+
'unsignedmediuminteger' => 'int',
240+
'unsignedsmallinteger' => 'int',
241+
'unsignedtinyinteger' => 'int',
242+
'year' => 'int',
243+
];
244+
245+
return $php_data_types[strtolower($dataType)] ?? 'string';
246+
}
191247
}

stubs/model/class.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace DummyNamespace;
44

55
use Illuminate\Database\Eloquent\Model;
66

7+
/** DummyPHPDocClass **/
78
class DummyClass extends Model
89
{
910
// ...

tests/fixtures/models/comment.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use Illuminate\Database\Eloquent\Model;
66
use Illuminate\Database\Eloquent\SoftDeletes;
77

8+
/**
9+
* @property integer $id
10+
*/
811
class Comment extends Model
912
{
1013
use SoftDeletes;

tests/fixtures/models/model-configured.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
use Illuminate\Database\Eloquent\Model;
66

7+
/**
8+
* @property integer $id
9+
* @property integer $post_id
10+
* @property integer $author_id
11+
*/
712
class Comment extends Model
813
{
914
/**

tests/fixtures/models/nested-components.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
use Illuminate\Database\Eloquent\Model;
66

7+
/**
8+
* @property integer $id
9+
* @property string $name
10+
*/
711
class User extends Model
812
{
913
/**

tests/fixtures/models/readme-example.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
use Illuminate\Database\Eloquent\Model;
66

7+
/**
8+
* @property integer $id
9+
* @property string $title
10+
* @property string $content
11+
* @property Carbon $published_at
12+
*/
713
class Post extends Model
814
{
915
/**

tests/fixtures/models/relationships.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
use Illuminate\Database\Eloquent\Model;
66

7+
/**
8+
* @property integer $id
9+
* @property integer $post_id
10+
* @property integer $author_id
11+
*/
712
class Comment extends Model
813
{
914
/**

tests/fixtures/models/soft-deletes.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
use Illuminate\Database\Eloquent\Model;
66
use Illuminate\Database\Eloquent\SoftDeletes;
77

8+
/**
9+
* @property integer $id
10+
* @property integer $post_id
11+
*/
812
class Comment extends Model
913
{
1014
use SoftDeletes;

tests/fixtures/models/unconventional.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
use Illuminate\Database\Eloquent\Model;
66

7+
/**
8+
* @property integer $id
9+
* @property string $name
10+
* @property integer $owner
11+
* @property integer $manager
12+
*/
713
class Team extends Model
814
{
915
/**

0 commit comments

Comments
 (0)