1 | <?php |
||
10 | class MakeEnum extends GeneratorCommand |
||
11 | { |
||
12 | protected $name = 'make:enum'; |
||
13 | protected $description = 'Create a new enum'; |
||
14 | |||
15 | 36 | protected function getStub() |
|
19 | |||
20 | 36 | protected function getDefaultNamespace($rootNamespace) |
|
24 | |||
25 | 36 | public function info($string, $verbosity = null) |
|
26 | { |
||
27 | 36 | parent::info("{$this->argument('name')}$string", $verbosity); |
|
28 | } |
||
29 | 36 | ||
30 | 36 | protected function replaceClass($stub, $name) |
|
31 | { |
||
32 | 36 | $stub = parent::replaceClass($stub, $name); |
|
33 | |||
34 | $stub = str_replace('DummyDocBlock', $this->getDocBlock(), $stub); |
||
35 | 36 | $stub = str_replace('DummyValueMapConst', $this->getValueMapConst(), $stub); |
|
36 | |||
37 | 36 | return $stub; |
|
38 | } |
||
39 | 36 | ||
40 | 32 | protected function getDocBlock(): string |
|
41 | { |
||
42 | 32 | $methods = array_merge($this->option('method'), $this->option('value')); |
|
43 | 32 | ||
44 | 32 | if (! empty($methods)) { |
|
45 | $docBlock = PHP_EOL.'/**'; |
||
46 | $docBlock .= implode('', array_map(function ($method) { |
||
47 | 36 | return PHP_EOL.' * @method static self '.$this->formatValueToMethod($method).'()'; |
|
48 | }, $methods)); |
||
49 | $docBlock .= PHP_EOL.' */'; |
||
50 | 36 | } |
|
51 | |||
52 | 36 | return $docBlock ?? ''; |
|
53 | } |
||
54 | 36 | ||
55 | 16 | protected function getValueMapConst(): string |
|
56 | 16 | { |
|
57 | $values = $this->option('value'); |
||
58 | 16 | ||
59 | 16 | if (! empty($values)) { |
|
60 | $tab = str_repeat(' ', 4); |
||
61 | $constant = $tab.'const MAP_VALUE = ['; |
||
62 | 16 | ||
63 | foreach ($values as $value) { |
||
|
|||
64 | $constant .= PHP_EOL.$tab.$tab.'\''.$this->formatValueToMethod($value).'\' => \''.$value.'\','; |
||
65 | 36 | } |
|
66 | |||
67 | $constant .= PHP_EOL.$tab.'];'; |
||
68 | 32 | } |
|
69 | |||
70 | 32 | return $constant ?? ''; |
|
71 | 32 | } |
|
72 | 8 | ||
73 | 24 | protected function formatValueToMethod(string $value): string |
|
74 | 8 | { |
|
75 | 16 | switch ($this->option('formatter')) { |
|
76 | 8 | case 'const': |
|
77 | 8 | return strtoupper(Str::snake($value)); |
|
78 | case 'snake': |
||
79 | 8 | return Str::snake($value); |
|
80 | case 'studly': |
||
81 | return Str::studly($value); |
||
82 | case 'camel': |
||
83 | 36 | default: |
|
84 | return Str::camel($value); |
||
85 | } |
||
86 | 36 | } |
|
87 | |||
88 | protected function getArguments() |
||
94 | 36 | ||
95 | 36 | protected function getOptions() |
|
96 | { |
||
97 | return [ |
||
98 | ['method', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The method name that should be added to the enum'], |
||
99 | ['value', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The value that should be added to the enum'], |
||
103 | } |
||
104 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.