Skip to content

Commit 2f8283a

Browse files
iPaatbarryvdh
authored andcommitted
Optionally write missing Laravel Model DocBlock (barryvdh#700)
* Fix missing doc-block. * Fixes a bug where a missing DocBlock caused errors. * Change Option Shorthand to W Because it is not allowed to have two char shorthands for options. * Revert code refactoring. * Fix copied config header.
1 parent 2a63b31 commit 2f8283a

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

config/ide-helper.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@
3838

3939
'write_model_magic_where' => true,
4040

41+
/*
42+
|--------------------------------------------------------------------------
43+
| Write Eloquent Model Mixins
44+
|--------------------------------------------------------------------------
45+
|
46+
| This will add the necessary DocBlock mixins to the model class
47+
| contained in the Laravel Framework. This helps the IDE with
48+
| auto-completion.
49+
|
50+
| Please be aware that this setting changes a file within the /vendor directory.
51+
|
52+
*/
53+
54+
'write_eloquent_model_mixins' => false,
55+
4156
/*
4257
|--------------------------------------------------------------------------
4358
| Helper files to include

src/Console/GeneratorCommand.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ public function handle()
115115

116116
if ($written !== false) {
117117
$this->info("A new helper file was written to $filename");
118+
119+
if ($this->option('write_mixins')) {
120+
Eloquent::writeEloquentModelHelper($this, $this->files);
121+
}
118122
} else {
119123
$this->error("The helper file could not be created at $filename");
120124
}
@@ -158,9 +162,11 @@ protected function getArguments()
158162
protected function getOptions()
159163
{
160164
$format = $this->config->get('ide-helper.format');
165+
$writeMixins = $this->config->get('ide-helper.write_eloquent_model_mixins');
161166

162167
return array(
163168
array('format', "F", InputOption::VALUE_OPTIONAL, 'The format for the IDE Helper', $format),
169+
array('write_mixins', "W", InputOption::VALUE_OPTIONAL, 'Write mixins to Laravel Model?', $writeMixins),
164170
array('helpers', "H", InputOption::VALUE_NONE, 'Include the helper files'),
165171
array('memory', "M", InputOption::VALUE_NONE, 'Use sqlite memory driver'),
166172
array('sublime', "S", InputOption::VALUE_NONE, 'DEPRECATED: Use different style for SublimeText CodeIntel'),

src/Eloquent.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,57 @@ public static function writeEloquentModelHelper(Command $command, Filesystem $fi
3232
$reflection = new \ReflectionClass($class);
3333
$namespace = $reflection->getNamespaceName();
3434
$originalDoc = $reflection->getDocComment();
35+
3536
if (!$originalDoc) {
3637
$command->info('Unexpected no document on ' . $class);
3738
}
3839
$phpdoc = new DocBlock($reflection, new Context($namespace));
3940

4041
$mixins = $phpdoc->getTagsByName('mixin');
42+
$expectedMixins = [
43+
'\Eloquent' => false,
44+
'\Illuminate\Database\Eloquent\Builder' => false,
45+
'\Illuminate\Database\Query\Builder' => false,
46+
];
47+
4148
foreach ($mixins as $m) {
42-
if ($m->getContent() === '\Eloquent') {
43-
$command->info('Tag Exists: @mixin \Eloquent in ' . $class);
49+
$mixin = $m->getContent();
50+
51+
if (isset($expectedMixins[$mixin])) {
52+
$command->info('Tag Exists: @mixin ' . $mixin . ' in ' . $class);
53+
54+
$expectedMixins[$mixin] = true;
55+
}
56+
}
4457

45-
return;
58+
$changed = false;
59+
foreach ($expectedMixins as $expectedMixin => $present) {
60+
if ($present === false) {
61+
$phpdoc->appendTag(Tag::createInstance('@mixin ' . $expectedMixin, $phpdoc));
62+
63+
$changed = true;
4664
}
4765
}
4866

49-
// add the Eloquent mixin
50-
$phpdoc->appendTag(Tag::createInstance("@mixin \\Eloquent", $phpdoc));
67+
// If nothing's changed, stop here.
68+
if (!$changed) {
69+
return;
70+
}
5171

5272
$serializer = new DocBlockSerializer();
5373
$serializer->getDocComment($phpdoc);
5474
$docComment = $serializer->getDocComment($phpdoc);
5575

76+
/*
77+
The new DocBlock is appended to the beginning of the class declaration.
78+
Since there is no DocBlock, the declaration is used as a guide.
79+
*/
80+
if (!$originalDoc) {
81+
$originalDoc = 'abstract class Model implements';
82+
83+
$docComment .= "\nabstract class Model implements";
84+
}
85+
5686
$filename = $reflection->getFileName();
5787
if ($filename) {
5888
$contents = $files->get($filename);
@@ -61,7 +91,7 @@ public static function writeEloquentModelHelper(Command $command, Filesystem $fi
6191
$contents = str_replace($originalDoc, $docComment, $contents, $count);
6292
if ($count > 0) {
6393
if ($files->put($filename, $contents)) {
64-
$command->info('Wrote @mixin \Eloquent to ' . $filename);
94+
$command->info('Wrote expected docblock to ' . $filename);
6595
} else {
6696
$command->error('File write failed to ' . $filename);
6797
}

0 commit comments

Comments
 (0)