Skip to content

Commit

Permalink
Merge pull request #624 from iPaat/Fix/623
Browse files Browse the repository at this point in the history
Fixes a bug where a missing DocBlock caused errors
  • Loading branch information
barryvdh authored Feb 8, 2018
2 parents 387efb9 + 1fc4284 commit 5c304db
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions src/Eloquent.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,57 @@ public static function writeEloquentModelHelper(Command $command, Filesystem $fi
$reflection = new \ReflectionClass($class);
$namespace = $reflection->getNamespaceName();
$originalDoc = $reflection->getDocComment();

if (!$originalDoc) {
$command->info('Unexpected no document on ' . $class);
}
$phpdoc = new DocBlock($reflection, new Context($namespace));

$mixins = $phpdoc->getTagsByName('mixin');
$expectedMixins = [
'\Eloquent' => false,
'\Illuminate\Database\Eloquent\Builder' => false,
'\Illuminate\Database\Query\Builder' => false,
];

foreach ($mixins as $m) {
if ($m->getContent() === '\Eloquent') {
$command->info('Tag Exists: @mixin \Eloquent in ' . $class);
$mixin = $m->getContent();

if (isset($expectedMixins[$mixin])) {
$command->info('Tag Exists: @mixin ' . $mixin . ' in ' . $class);

$expectedMixins[$mixin] = true;
}
}

return;
$changed = false;
foreach ($expectedMixins as $expectedMixin => $present) {
if ($present === false) {
$phpdoc->appendTag(Tag::createInstance('@mixin ' . $expectedMixin, $phpdoc));

$changed = true;
}
}

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

$serializer = new DocBlockSerializer();
$serializer->getDocComment($phpdoc);
$docComment = $serializer->getDocComment($phpdoc);

/*
The new DocBlock is appended to the beginning of the class declaration.
Since there is no DocBlock, the declaration is used as a guide.
*/
if (!$originalDoc) {
$originalDoc = 'abstract class Model implements';

$docComment .= "\nabstract class Model implements";
}

$filename = $reflection->getFileName();
if ($filename) {
$contents = $files->get($filename);
Expand All @@ -61,7 +91,7 @@ public static function writeEloquentModelHelper(Command $command, Filesystem $fi
$contents = str_replace($originalDoc, $docComment, $contents, $count);
if ($count > 0) {
if ($files->put($filename, $contents)) {
$command->info('Wrote @mixin \Eloquent to ' . $filename);
$command->info('Wrote expected docblock to ' . $filename);
} else {
$command->error('File write failed to ' . $filename);
}
Expand Down

2 comments on commit 5c304db

@it-can
Copy link
Contributor

@it-can it-can commented on 5c304db Feb 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@barryvdh can you release a new version?

@barryvdh
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Please sign in to comment.