Completed
Push — stable ( ef4570...c41d70 )
by Nuno
03:23
created

Renamer   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 180
Duplicated Lines 21.11 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 38
loc 180
ccs 56
cts 63
cp 0.8889
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 6 1
A rename() 0 12 2
A asksForApplicationName() 0 12 3
A getCurrentBinaryName() 0 6 1
A getComposer() 0 10 2
A getConfig() 0 10 2
A updateComposer() 38 48 3
A renameBinary() 0 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * This file is part of Laravel Zero.
5
 *
6
 * (c) Nuno Maduro <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 */
11
12
namespace LaravelZero\Framework\Commands\App;
13
14
use Illuminate\Support\Str;
15
use Illuminate\Support\Facades\File;
16
use LaravelZero\Framework\Commands\Command;
17
18
/**
19
 * This is the Laravel Zero Framework Renamer Command implementation.
20
 */
21
class Renamer extends Command
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected $signature = 'app:rename {name? : The new name}';
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected $description = 'Perform an application rename';
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 1
    public function handle(): void
37
    {
38 1
        $this->info('Renaming the application...');
39
40 1
        $this->rename();
41 1
    }
42
43
    /**
44
     * Updates the binary name and the application
45
     * name on the composer.json.
46
     *
47
     * @return $this
48
     */
49 1
    protected function rename(): Renamer
50
    {
51 1
        $name = $this->asksForApplicationName();
52
53 1
        if (File::exists(base_path($name))) {
54
            $this->app->abort(403, 'Folder or file already exists.');
55
        } else {
56 1
            $this->renameBinary($name)->updateComposer($name);
57
        }
58
59 1
        return $this;
60
    }
61
62
    /**
63
     * Asks for the application name.
64
     *
65
     * If there is no interaction, we take the folder basename.
66
     *
67
     * @return string
68
     */
69 1
    protected function asksForApplicationName(): string
70
    {
71 1
        if (empty($name = $this->input->getArgument('name'))) {
72
            $name = $this->ask('What is your application name?');
73
        }
74
75 1
        if (empty($name)) {
76
            $name = trim(basename($this->app->basePath()));
77
        }
78
79 1
        return Str::lower($name);
80
    }
81
82
    /**
83
     * Update composer json with related information.
84
     *
85
     * @param string $name
86
     *
87
     * @return $this
88
     */
89 1
    protected function updateComposer(string $name): Renamer
90
    {
91 1
        $this->task(
0 ignored issues
show
Documentation Bug introduced by
The method task does not exist on object<LaravelZero\Frame...k\Commands\App\Renamer>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
92 1
            'Updating config/app.php "name" property',
93 View Code Duplication
            function () use ($name) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94 1
                $neededLine = "'name' => '".Str::ucfirst($this->getCurrentBinaryName())."'";
95
96 1
                if (Str::contains($contents = $this->getConfig(), $neededLine)) {
97 1
                    File::put(
98 1
                        config_path('app.php'),
99 1
                        Str::replaceFirst(
100 1
                            $neededLine,
101 1
                            "'name' => '".Str::ucfirst($name)."'",
102 1
                            $contents
103
                        )
104
                    );
105
106 1
                    return true;
107
                }
108
109
                return false;
110 1
            }
111
        );
112
113 1
        $this->task(
0 ignored issues
show
Documentation Bug introduced by
The method task does not exist on object<LaravelZero\Frame...k\Commands\App\Renamer>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
114 1
            'Updating composer "bin"',
115 View Code Duplication
            function () use ($name) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116 1
                $neededLine = '"bin": ["'.$this->getCurrentBinaryName().'"]';
117
118 1
                if (Str::contains($contents = $this->getComposer(), $neededLine)) {
119 1
                    File::put(
120 1
                        base_path('composer.json'),
121 1
                        Str::replaceFirst(
122 1
                            $neededLine,
123 1
                            '"bin": ["'.$name.'"]',
124 1
                            $contents
125
                        )
126
                    );
127
128 1
                    return true;
129
                }
130
131
                return false;
132 1
            }
133
        );
134
135 1
        return $this;
136
    }
137
138
    /**
139
     * Renames the application binary.
140
     *
141
     * @param string $name
142
     *
143
     * @return $this
144
     */
145 1
    protected function renameBinary(string $name): Renamer
146
    {
147 1
        $this->task(
0 ignored issues
show
Documentation Bug introduced by
The method task does not exist on object<LaravelZero\Frame...k\Commands\App\Renamer>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
148 1
            sprintf('Renaming application to "%s"', $name),
149
            function () use ($name) {
150 1
                return File::move($this->app->basePath($this->getCurrentBinaryName()), $this->app->basePath($name));
0 ignored issues
show
Unused Code introduced by
The call to Application::basePath() has too many arguments starting with $this->getCurrentBinaryName().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to Application::basePath() has too many arguments starting with $name.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
151 1
            }
152
        );
153
154 1
        return $this;
155
    }
156
157
    /**
158
     * Returns the current binary name.
159
     *
160
     * @return string
161
     */
162 1
    protected function getCurrentBinaryName(): string
163
    {
164 1
        $composer = $this->getComposer();
165
166 1
        return current(@json_decode($composer)->bin);
167
    }
168
169
    /**
170
     * Get composer file.
171
     *
172
     * @return string
173
     */
174 1
    protected function getComposer(): string
175
    {
176 1
        $file = $this->app->basePath('composer.json');
0 ignored issues
show
Unused Code introduced by
The call to Application::basePath() has too many arguments starting with 'composer.json'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
177
178 1
        if (! File::exists($file)) {
179
            abort(400, 'The file composer.json not found');
180
        }
181
182 1
        return File::get($file);
183
    }
184
185
    /**
186
     * Get config file.
187
     *
188
     * @return string
189
     */
190 1
    protected function getConfig(): string
191
    {
192 1
        $file = config_path('app.php');
193
194 1
        if (! File::exists($file)) {
195
            abort(400, 'The file config/app.php not found');
196
        }
197
198 1
        return File::get($file);
199
    }
200
}
201