forked from BenSampo/laravel-enum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumAnnotateCommand.php
74 lines (61 loc) · 2.24 KB
/
EnumAnnotateCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace BenSampo\Enum\Commands;
use ReflectionClass;
use BenSampo\Enum\Enum;
use Symfony\Component\Finder\Finder;
use Laminas\Code\Generator\DocBlockGenerator;
use Laminas\Code\Reflection\DocBlockReflection;
use Laminas\Code\Generator\DocBlock\Tag\MethodTag;
use Laminas\Code\Generator\DocBlock\Tag\TagInterface;
class EnumAnnotateCommand extends AbstractAnnotationCommand
{
const DEFAULT_SCAN_FOLDER = 'Enums';
const PARENT_CLASS = Enum::class;
/**
* The console command name.
*
* @var string
*/
protected $name = 'enum:annotate';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate DocBlock annotations for enum classes';
/**
* Apply annotations to a reflected class
*
* @param \ReflectionClass $reflectionClass
* @return void
*/
protected function annotate(ReflectionClass $reflectionClass)
{
$docBlock = DocBlockGenerator::fromArray([]);
$originalDocBlock = null;
if (strlen($reflectionClass->getDocComment()) !== 0) {
$originalDocBlock = DocBlockGenerator::fromReflection(new DocBlockReflection($reflectionClass));
$docBlock->setShortDescription($originalDocBlock->getShortDescription());
}
$this->updateClassDocblock($reflectionClass, $this->getDocBlock($reflectionClass));
}
protected function getDocblockTags(array $originalTags, ReflectionClass $reflectionClass): array
{
$constants = $reflectionClass->getConstants();
$existingTags = array_filter($originalTags, function (TagInterface $tag) use ($constants) {
return !$tag instanceof MethodTag || !in_array($tag->getMethodName(), array_keys($constants), true);
});
return collect($constants)
->map(function ($value, $constantName) {
return new MethodTag($constantName, ['static'], null, true);
})
->merge($existingTags)
->toArray();
}
protected function getClassFinder(): Finder
{
$finder = new Finder();
$scanPath = $this->option('folder') ?? app_path(self::DEFAULT_SCAN_FOLDER);
return $finder->files()->in($scanPath)->name('*.php');
}
}