Completed
Push — master ( e09f8c...50fa0d )
by Fumio
10:35
created

AddonNameCommand::setClassNamespace()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
ccs 12
cts 12
cp 1
rs 8.9713
cc 3
eloc 13
nc 4
nop 0
crap 3
1
<?php
2
3
namespace Jumilla\Addomnipot\Laravel\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
use Symfony\Component\Finder\Finder;
8
use Jumilla\Addomnipot\Laravel\Environment as AddonEnvironment;
9
use Jumilla\Addomnipot\Laravel\Addon;
10
use UnexpectedValueException;
11
12
class AddonNameCommand extends Command
13
{
14
    /**
15
     * The console command signature.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'addon:name
20
        {addon : The desired namespace.}
21
        {namespace : The desired namespace.}
22
    ';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Set the addon PHP namespace';
30
31
    /**
32
     * @var \Illuminate\Filesystem\Filesystem
33
     */
34
    protected $filesystem;
35
36
    /**
37
     * @var \Jumilla\Addomnipot\Laravel\Addons\Addon
38
     */
39
    protected $addon;
40
41
    /**
42
     * @var string
43
     */
44
    protected $currentNamespace;
45
46
    /**
47
     * @var string
48
     */
49
    protected $newNamespace;
50
51
    /**
52
     * Execute the console command.
53
     *
54
     * @return mixed
55
     */
56 2
    public function handle(Filesystem $filesystem, AddonEnvironment $env)
57
    {
58 2
        $this->filesystem = $filesystem;
59
60 2
        $addonName = $this->argument('addon');
61
62 2
        if (!$env->exists($addonName)) {
0 ignored issues
show
Bug introduced by
It seems like $addonName defined by $this->argument('addon') on line 60 can also be of type array; however, Jumilla\Addomnipot\Laravel\Environment::exists() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
63 1
            throw new UnexpectedValueException("Addon '$addonName' is not found.");
64
        }
65
66 1
        $this->addon = Addon::create($env->path($addonName));
0 ignored issues
show
Bug introduced by
It seems like $addonName defined by $this->argument('addon') on line 60 can also be of type array; however, Jumilla\Addomnipot\Laravel\Environment::path() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Documentation Bug introduced by
It seems like \Jumilla\Addomnipot\Lara...$env->path($addonName)) of type object<Jumilla\Addomnipot\Laravel\Addon> is incompatible with the declared type object<Jumilla\Addomnipot\Laravel\Addons\Addon> of property $addon.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
67
68 1
        $this->currentNamespace = trim($this->addon->phpNamespace(), '\\');
69
70 1
        $this->newNamespace = str_replace('/', '\\', $this->argument('namespace'));
71
72 1
        $this->setAddonNamespaces();
73
74 1
        $this->setComposerNamespace();
75
76 1
        $this->setClassNamespace();
77
78 1
        $this->setConfigNamespaces();
79
80 1
        $this->info('Addon namespace set!');
81 1
    }
82
83
    /**
84
     * Set the namespace in addon.php, adon.json file.
85
     */
86 1
    protected function setAddonNamespaces()
87
    {
88 1
        $this->setAddonConfigNamespaces();
89 1
        $this->setAddonJsonNamespaces();
90 1
    }
91
92
    /**
93
     * Set the namespace in addon.php file.
94
     */
95 1
    protected function setAddonConfigNamespaces()
96
    {
97 1
        if (file_exists($this->addon->path('addon.php'))) {
98
            $search = [
99 1
                "namespace {$this->currentNamespace}",
100 1
                "'namespace' => '{$this->currentNamespace}'",
101 1
                "'{$this->currentNamespace}\\",
102 1
                "\"{$this->currentNamespace}\\",
103 1
                "\\{$this->currentNamespace}\\",
104
            ];
105
106
            $replace = [
107 1
                "namespace {$this->newNamespace}",
108 1
                "'namespace' => '{$this->newNamespace}'",
109 1
                "'{$this->newNamespace}\\",
110 1
                "\"{$this->newNamespace}\\",
111 1
                "\\{$this->newNamespace}\\",
112
            ];
113
114 1
            $this->replaceIn($this->addon->path('addon.php'), $search, $replace);
115
        }
116 1
    }
117
118
    /**
119
     * Set the namespace in addon.json file.
120
     */
121 1
    protected function setAddonJsonNamespaces()
122
    {
123 1
        if (file_exists($this->addon->path('addon.json'))) {
124
            $currentNamespace = str_replace('\\', '\\\\', $this->currentNamespace);
125
            $newNamespace = str_replace('\\', '\\\\', $this->newNamespace);
126
127
            $search = [
128
                "\"namespace\": \"{$currentNamespace}\"",
129
                "\"{$currentNamespace}\\\\",
130
                "\\\\{$currentNamespace}\\\\",
131
            ];
132
133
            $replace = [
134
                "\"namespace\": \"{$newNamespace}\"",
135
                "\"{$newNamespace}\\\\",
136
                "\\\\{$newNamespace}\\\\",
137
            ];
138
139
            $this->replaceIn($this->addon->path('addon.json'), $search, $replace);
140
        }
141 1
    }
142
143
    /**
144
     * Set the PSR-4 namespace in the Composer file.
145
     */
146 1
    protected function setComposerNamespace()
147
    {
148 1
        if (file_exists($this->addon->path('composer.json'))) {
149
            $this->replaceIn(
150
                $this->addon->path('composer.json'), $this->currentNamespace.'\\\\', str_replace('\\', '\\\\', $this->newNamespace).'\\\\'
151
            );
152
        }
153 1
    }
154
155
    /**
156
     * Set the namespace on the files in the class directory.
157
     */
158 1
    protected function setClassNamespace()
159
    {
160 1
        $files = Finder::create();
161
162 1
        foreach ($this->addon->config('addon.directories') as $path) {
163 1
            $files->in($this->addon->path($path));
164
        }
165
166 1
        $files->name('*.php');
167
168
        $search = [
169 1
            $this->currentNamespace.'\\',
170 1
            'namespace '.$this->currentNamespace.';',
171
        ];
172
173
        $replace = [
174 1
            $this->newNamespace.'\\',
175 1
            'namespace '.$this->newNamespace.';',
176
        ];
177
178 1
        foreach ($files as $file) {
179 1
            $this->replaceIn($file, $search, $replace);
180
        }
181 1
    }
182
183
    /**
184
     * Set the namespace in the appropriate configuration files.
185
     */
186 1
    protected function setConfigNamespaces()
187
    {
188 1
        $configPath = $this->addon->path($this->addon->config('paths.config', 'config'));
189
190 1
        if ($this->filesystem->isDirectory($configPath)) {
191 1
            $files = Finder::create()
192 1
                ->in($configPath)
193 1
                ->name('*.php');
194
195 1
            foreach ($files as $file) {
196
                $this->replaceConfigNamespaces($file->getRealPath());
197
            }
198
        }
199 1
    }
200
201
    /**
202
     * Replace the namespace in PHP configuration file.
203
     *
204
     * @param string $path
205
     */
206
    protected function replaceConfigNamespaces($path)
207
    {
208
        $search = [
209
            "'{$this->currentNamespace}\\",
210
            "\"{$this->currentNamespace}\\",
211
            "\\{$this->currentNamespace}\\",
212
        ];
213
214
        $replace = [
215
            "'{$this->newNamespace}\\",
216
            "\"{$this->newNamespace}\\",
217
            "\\{$this->newNamespace}\\",
218
        ];
219
220
        $this->replaceIn($path, $search, $replace);
221
    }
222
223
    /**
224
     * Replace the given string in the given file.
225
     *
226
     * @param string $path
227
     * @param string | array $search
228
     * @param string | array $replace
229
     */
230 1
    protected function replaceIn($path, $search, $replace)
231
    {
232 1
        if ($this->output->isVerbose()) {
233
            $this->line("{$path} ...");
234
        }
235
236 1
        $this->filesystem->put($path, str_replace($search, $replace, $this->filesystem->get($path)));
237 1
    }
238
}
239