Completed
Pull Request — master (#11)
by Andrea Marco
06:26
created

MakeTransformerCommand::getModelName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Rexlabs\Laravel\Smokescreen\Console;
4
5
use Illuminate\Console\GeneratorCommand;
6
7
class MakeTransformerCommand extends GeneratorCommand
8
{
9
    /**
10
     * The name and signature of the console command.
11
     *
12
     * @var string
13
     */
14
    protected $signature = 'make:transformer {model : The namespaced model to transform. e.g. "App\User"}';
15
16
    /**
17
     * The console command description.
18
     *
19
     * @var string
20
     */
21
    protected $description = 'Create a new transformer class';
22
23
    /**
24
     * The type of class being generated.
25
     *
26
     * @var string
27
     */
28
    protected $type = 'Transformer';
29
30
    /**
31
     * Get the stub file for the generator.
32
     *
33
     * @return string
34
     */
35
    protected function getStub()
36
    {
37
        return __DIR__ . '/transformer.stub';
38
    }
39
40
    /**
41
     * Execute the console command.
42
     *
43
     * @return bool|null
44
     */
45
    public function handle()
46
    {
47
        if (!class_exists($name = $this->argument('model'))) {
0 ignored issues
show
Bug introduced by
It seems like $name = $this->argument('model') can also be of type array; however, parameter $class_name of class_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

47
        if (!class_exists(/** @scrutinizer ignore-type */ $name = $this->argument('model'))) {
Loading history...
48
            $confirmed = $this->confirm(
49
                'The model to transform does not exist, do you want to generate it?'
50
            );
51
52
            if ($confirmed) {
53
                $this->call('make:model', compact('name'));
54
            }
55
        }
56
57
        parent::handle();
58
    }
59
60
    /**
61
     * Get the default namespace for the class.
62
     *
63
     * @param  string  $rootNamespace
64
     * @return string
65
     */
66
    protected function getDefaultNamespace($rootNamespace)
67
    {
68
        return config('smokescreen.transformer_namespace');
69
    }
70
71
    /**
72
     * Build the class with the given name.
73
     *
74
     * @param  string  $name
75
     * @return string
76
     */
77
    protected function buildClass($name)
78
    {
79
        $stub = parent::buildClass($name);
80
81
        return $this->replaceModel($stub);
82
    }
83
84
    /**
85
     * Retrieve the given content replacing the model in it.
86
     *
87
     * @param string $content
88
     * @return string
89
     */
90
    protected function replaceModel(string $content) : string
91
    {
92
        $from = [
93
            'DummyModelName',
94
            'dummyModelName',
95
            'DummyModel',
96
        ];
97
98
        $to = [
99
            $this->getModelName(),
100
            lcfirst($this->getModelName()),
101
            $this->argument('model'),
102
        ];
103
104
        return str_replace($from, $to, $content);
0 ignored issues
show
Bug introduced by
$to of type array<integer,string|array> is incompatible with the type string|string[] expected by parameter $replace of str_replace(). ( Ignorable by Annotation )

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

104
        return str_replace($from, /** @scrutinizer ignore-type */ $to, $content);
Loading history...
105
    }
106
107
    /**
108
     * Get the desired class name from the input.
109
     *
110
     * @return string
111
     */
112
    protected function getNameInput()
113
    {
114
        return $this->getModelName() . 'Transformer';
115
    }
116
117
    /**
118
     * Retrieve the model name.
119
     *
120
     * @return string
121
     */
122
    protected function getModelName() : string
123
    {
124
        return class_basename($this->argument('model'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('model') can also be of type array; however, parameter $class of class_basename() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

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

124
        return class_basename(/** @scrutinizer ignore-type */ $this->argument('model'));
Loading history...
125
    }
126
}
127