EnumMakeCommand   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 136
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getStub() 0 4 1
A getDefaultNamespace() 0 4 1
A buildClass() 0 10 1
A getArguments() 0 6 1
A getConstants() 0 17 3
A replaceDocblock() 0 11 2
A replaceConstants() 0 11 3
1
<?php
2
3
namespace MadWeb\Enum\Console;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
8
class EnumMakeCommand extends GeneratorCommand
9
{
10
    /**
11
     * The console command name.
12
     *
13
     * @var string
14
     */
15
    protected $name = 'make:enum';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Create a new enum class';
23
24
    /**
25
     * The type of class being generated.
26
     *
27
     * @var string
28
     */
29
    protected $type = 'Enum';
30
31
    /**
32
     * Get the stub file for the generator.
33
     *
34
     * @return string
35
     */
36 12
    protected function getStub()
37
    {
38 12
        return __DIR__.'/stubs/enum.stub';
39
    }
40
41
    /**
42
     * Get the default namespace for the class.
43
     *
44
     * @param string $rootNamespace
45
     *
46
     * @return string
47
     */
48 12
    protected function getDefaultNamespace($rootNamespace)
49
    {
50 12
        return $rootNamespace.'\Enums';
51
    }
52
53
    /**
54
     * Build the class with the given name.
55
     *
56
     * @param  string  $name
57
     * @return string
58
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
59
     */
60 12
    protected function buildClass($name)
61
    {
62 12
        $stub = $this->files->get($this->getStub());
63
64
        return $this
65 12
          ->replaceDocblock($stub)
66 12
          ->replaceConstants($stub)
67 12
          ->replaceNamespace($stub, $name)
68 12
          ->replaceClass($stub, $name);
69
    }
70
71
    /**
72
     * Get the console command arguments.
73
     *
74
     * @return array
75
     */
76 12
    protected function getArguments()
77
    {
78 12
        return array_merge(parent::getArguments(), [
79 12
            ['values', InputArgument::IS_ARRAY, 'The const maps e.g. ACTIVE=active DELETED=deleted; or FOO BAR BAZ for FOO=0 BAR=1 BAZ=2'],
80
        ]);
81
    }
82
83
    /**
84
     * Get the constants for the enum.
85
     *
86
     * @return array
87
     */
88 12
    protected function getConstants()
89
    {
90 12
        $inputValues = $this->argument('values');
91 12
        if (empty($inputValues)) {
92 3
            $inputValues = ['FOO', 'BAR', 'BAZ'];
93
        }
94
95 12
        $outputValues = [];
96 12
        $mapIndex = 0;
97
98 12
        foreach ($inputValues as $inputValue) {
0 ignored issues
show
Bug introduced by
The expression $inputValues of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. 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:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
99 12
            $parts = explode('=', $inputValue);
100 12
            $outputValues[$parts[0]] = ($parts[1] ?? $mapIndex++);
101
        }
102
103 12
        return $outputValues;
104
    }
105
106
    /**
107
     * Replace the docblock for the given stub.
108
     *
109
     * @param string $stub
110
     *
111
     * @return $this
112
     */
113 12
    protected function replaceDocblock(&$stub)
114
    {
115 12
        $docBlock = [];
116 12
        foreach ($this->getConstants() as $const => $value) {
117 12
            $docBlock[] = " * @method static DummyClass {$const}()";
118
        }
119
120 12
        $stub = str_replace('DOCBLOCK', implode(PHP_EOL, $docBlock), $stub);
121
122 12
        return $this;
123
    }
124
125
    /**
126
     * Replace the constants for the given stub.
127
     *
128
     * @param string $stub
129
     *
130
     * @return $this
131
     */
132 12
    protected function replaceConstants(&$stub)
133
    {
134 12
        $constList = [];
135 12
        foreach ($this->getConstants() as $const => $value) {
136 12
            $constList[] = is_int($value) ? "    const {$const} = {$value};" : "    const {$const} = '{$value}';";
137
        }
138
139 12
        $stub = str_replace(['DEFAULTVALUE', 'CONSTLIST'], [array_keys($this->getConstants())[0], implode(PHP_EOL, $constList)], $stub);
140
141 12
        return $this;
142
    }
143
}
144