mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 01:57:13 +00:00
* composer require --dev friendsofphp/php-cs-fixer:^2 * php-cs-fixer: ignore cache and custom local config file * php-cs-fixer: initial config * php-cs-fixer: replace commands in composer * php-cs-fixer: add PSR12 "as good as it currently gets" * php-cs-fixer: apply PSR12 to codebase * tests: add workaround to keep unused imports for snapshot testing * php-cs-fixer: apply no_unused_imports * php-cs-fixer: apply array_syntax short * php-cs-fixer: apply single_quote * php-cs-fixer: switch ordered_imports sort_algorithm to alpha Let's be opinionated here for consistency * php-cs-fixer: split between non-tests and tests and share config * php-cs-fixer: apply declare_strict_types for tests * php-cs-fixer: apply fully_qualified_strict_types * php-cs-fixer: apply space_after_semicolon * php-cs-fixer: apply trailing_comma_in_multiline_array * php-cs-fixer: apply trim_array_spaces * php-cs-fixer: apply unary_operator_spaces * php-cs-fixer: apply whitespace_after_comma_in_array * php-cs-fixer: apply native_function_invocation * php-cs-fixer: apply concat_space * grumphp: reflect we're using phpcsfixer now * composer remove --dev squizlabs/php_codesniffer * gha: simplify fix-style approach Not really necessary to remove packages and then manually prevent unrelated commits creeping in Co-authored-by: Barry vd. Heuvel <[email protected]>
114 lines
3.3 KiB
PHP
114 lines
3.3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Laravel IDE Helper to add \Eloquent mixin to Eloquent\Model
|
|
*
|
|
* @author Charles A. Peterson <[email protected]>
|
|
*/
|
|
|
|
namespace Barryvdh\LaravelIdeHelper;
|
|
|
|
use Barryvdh\Reflection\DocBlock;
|
|
use Barryvdh\Reflection\DocBlock\Context;
|
|
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
|
|
use Barryvdh\Reflection\DocBlock\Tag;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Filesystem\Filesystem;
|
|
|
|
class Eloquent
|
|
{
|
|
/**
|
|
* Write mixin helper to the Eloquent\Model
|
|
* This is needed since laravel/framework v5.4.29
|
|
*
|
|
* @param Command $command
|
|
* @param Filesystem $files
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function writeEloquentModelHelper(Command $command, Filesystem $files)
|
|
{
|
|
$class = 'Illuminate\Database\Eloquent\Model';
|
|
|
|
$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) {
|
|
$mixin = $m->getContent();
|
|
|
|
if (isset($expectedMixins[$mixin])) {
|
|
$command->info('Tag Exists: @mixin ' . $mixin . ' in ' . $class);
|
|
|
|
$expectedMixins[$mixin] = true;
|
|
}
|
|
}
|
|
|
|
$changed = false;
|
|
foreach ($expectedMixins as $expectedMixin => $present) {
|
|
if ($present === false) {
|
|
$phpdoc->appendTag(Tag::createInstance('@mixin ' . $expectedMixin, $phpdoc));
|
|
|
|
$changed = true;
|
|
}
|
|
}
|
|
|
|
// 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) {
|
|
$command->error('Filename not found ' . $class);
|
|
return;
|
|
}
|
|
|
|
$contents = $files->get($filename);
|
|
if (!$contents) {
|
|
$command->error('No file contents found ' . $filename);
|
|
return;
|
|
}
|
|
|
|
$count = 0;
|
|
$contents = str_replace($originalDoc, $docComment, $contents, $count);
|
|
if ($count <= 0) {
|
|
$command->error('Content did not change ' . $contents);
|
|
return;
|
|
}
|
|
|
|
if (!$files->put($filename, $contents)) {
|
|
$command->error('File write failed to ' . $filename);
|
|
return;
|
|
}
|
|
|
|
$command->info('Wrote expected docblock to ' . $filename);
|
|
}
|
|
}
|