Completed
Pull Request — develop (#2)
by
unknown
04:30
created

EnumMakeCommand::handle()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Cerbero\LaravelEnum\Console\Commands;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Support\Str;
7
use Cerbero\LaravelEnum\StubAssembler;
8
use Cerbero\LaravelEnum\Parser;
9
use Symfony\Component\Console\Exception\InvalidArgumentException;
10
11
/**
12
 * The Artisan command to generate Enum classes.
13
 *
14
 */
15
class EnumMakeCommand extends GeneratorCommand
16
{
17
    /**
18
     * The name and signature of the console command.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'make:enum
23
                            {name : The name of the class}
24
                            {enum : The definition of the enum}
25
                            {--numeric : Create a numeric enum}
26
                            {--bitwise : Create a bitwise enum}
27
                            {--p|path= : The path to generate enums in}
28
                            {--f|force : Create the class even if the enum already exists}';
29
30
    /**
31
     * The console command description.
32
     *
33
     * @var string
34
     */
35
    protected $description = 'Create a new enum class';
36
37
    /**
38
     * The type of class being generated.
39
     *
40
     * @var string
41
     */
42
    protected $type = 'Enum';
43
44
    /**
45
     * Get the stub file for the generator.
46
     *
47
     * @return string
48
     */
49 9
    protected function getStub()
50
    {
51 9
        return __DIR__ . '/../../../stubs/enum.stub';
52
    }
53
54
    /**
55
     * Execute the console command.
56
     *
57
     * @return bool|null
58
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
59
     */
60 12
    public function handle()
61
    {
62 12
        if ($this->option('numeric') && $this->option('bitwise')) {
63 3
            throw new InvalidArgumentException('Only one option of "numeric" or "bitwise" can be set');
64
        }
65
66 9
        return parent::handle();
67
    }
68
69
    /**
70
     * Get the default namespace for the class.
71
     *
72
     * @param  string  $rootNamespace
73
     * @return string
74
     */
75 9
    protected function getDefaultNamespace($rootNamespace)
76
    {
77 9
        if ($path = $this->option('path')) {
78
            // Ensure the path starts with "app/"
79 3
            $path = Str::start(ltrim($path, '/'), 'app/');
80
            // Remove "app/" from the beginning of the path
81 3
            $path = preg_replace('#^app\/#', '', $path);
82
            // Convert the path into namespace
83 3
            $namespace = implode('\\', array_map('ucfirst', explode('/', $path)));
84
            // Prepend the root namespace
85 3
            return $rootNamespace . '\\' . $namespace;
86
        }
87
88 6
        return $rootNamespace . '\Enums';
89
    }
90
91
    /**
92
     * Build the class with the given name.
93
     *
94
     * @param  string  $name
95
     * @return string
96
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
97
     */
98 9
    protected function buildClass($name)
99
    {
100 9
        $stub = parent::buildClass($name);
101 9
        $enums = $this->parseEnums();
102
103 9
        return (new StubAssembler($stub, $enums))
104 9
            ->replaceMethodTags()
105 9
            ->replaceConstants()
106 9
            ->replaceMap()
107 9
            ->getStub();
108
    }
109
110
    /**
111
     * Parse the command syntax and retrieve the enums
112
     *
113
     * @return array
114
     */
115 9
    private function parseEnums() : array
116
    {
117
        // Normalise definition as argument() may return an array
118 9
        $enum = (array) $this->argument('enum');
119 9
        $definition = trim($enum[0]);
120
121 9
        return (new Parser)->parseDefinition($definition, $this->option('numeric'), $this->option('bitwise'));
0 ignored issues
show
Bug introduced by
$this->option('bitwise') of type string is incompatible with the type boolean expected by parameter $bitwise of Cerbero\LaravelEnum\Parser::parseDefinition(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
        return (new Parser)->parseDefinition($definition, $this->option('numeric'), /** @scrutinizer ignore-type */ $this->option('bitwise'));
Loading history...
Bug introduced by
$this->option('numeric') of type string is incompatible with the type boolean expected by parameter $numeric of Cerbero\LaravelEnum\Parser::parseDefinition(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
        return (new Parser)->parseDefinition($definition, /** @scrutinizer ignore-type */ $this->option('numeric'), $this->option('bitwise'));
Loading history...
122
    }
123
}
124