GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( bd76f9...bf3f88 )
by Tom
11:16 queued 11:15
created

MakeEnum::getValueMapConst()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.7
c 0
b 0
f 0
cc 3
nc 2
nop 0
crap 3
1
<?php
2
3
namespace Spatie\Enum\Laravel\Commands;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Console\GeneratorCommand;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Input\InputArgument;
9
10
class MakeEnum extends GeneratorCommand
11
{
12
    protected $name = 'make:enum';
13
    protected $description = 'Create a new enum';
14
15 36
    protected function getStub()
16
    {
17 36
        return __DIR__.'/../../stubs/enum.stub';
18
    }
19
20 36
    protected function getDefaultNamespace($rootNamespace)
21
    {
22 36
        return $rootNamespace.'\Enums';
23
    }
24
25 36
    protected function replaceClass($stub, $name)
26
    {
27 36
        $stub = parent::replaceClass($stub, $name);
28
29 36
        $stub = str_replace('DummyDocBlock', $this->getDocBlock(), $stub);
30 36
        $stub = str_replace('DummyValueMapConst', $this->getValueMapConst(), $stub);
31
32 36
        return $stub;
33
    }
34
35 36
    protected function getDocBlock(): string
36
    {
37 36
        $methods = array_merge($this->option('method'), $this->option('value'));
38
39 36
        if (! empty($methods)) {
40 32
            $docBlock = PHP_EOL.'/**';
41
            $docBlock .= implode('', array_map(function ($method) {
42 32
                return PHP_EOL.' * @method static self '.$this->formatValueToMethod($method).'()';
43 32
            }, $methods));
44 32
            $docBlock .= PHP_EOL.' */';
45
        }
46
47 36
        return $docBlock ?? '';
48
    }
49
50 36
    protected function getValueMapConst(): string
51
    {
52 36
        $values = $this->option('value');
53
54 36
        if (! empty($values)) {
55 16
            $tab = str_repeat(' ', 4);
56 16
            $constant = $tab.'const MAP_VALUE = [';
57
58 16
            foreach ($values as $value) {
0 ignored issues
show
Bug introduced by
The expression $values of type array|string|boolean 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...
59 16
                $constant .= PHP_EOL.$tab.$tab.'\''.$this->formatValueToMethod($value).'\' => \''.$value.'\',';
60
            }
61
62 16
            $constant .= PHP_EOL.$tab.'];';
63
        }
64
65 36
        return $constant ?? '';
66
    }
67
68 32
    protected function formatValueToMethod(string $value): string
69
    {
70 32
        switch ($this->option('formatter')) {
71 32
            case 'const':
72 8
                return strtoupper(Str::snake($value));
73 24
            case 'snake':
74 8
                return Str::snake($value);
75 16
            case 'studly':
76 8
                return Str::studly($value);
77 8
            case 'camel':
78
            default:
79 8
                return Str::camel($value);
80
        }
81
    }
82
83 36
    protected function getArguments()
84
    {
85
        return [
86 36
            ['name', InputArgument::REQUIRED, 'The name of the enum'],
87
        ];
88
    }
89
90 36
    protected function getOptions()
91
    {
92
        return [
93 36
            ['method', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The method name that should be added to the enum'],
94 36
            ['value', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The value that should be added to the enum'],
95 36
            ['formatter', null, InputOption::VALUE_REQUIRED, 'The formatter to use for the value to method conversion (snake, const, studly, camel)', 'camel'],
96
        ];
97
    }
98
}
99