Skip to content

Commit 5c304db

Browse files
authored
Merge pull request #624 from iPaat/Fix/623
Fixes a bug where a missing DocBlock caused errors
2 parents 387efb9 + 1fc4284 commit 5c304db

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

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)