Completed
Push — dev ( 6bece5...cad8d6 )
by Zach
02:11
created

CommandCreator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 15.87 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 10
loc 63
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 21 2
A getStub() 0 8 1
A resolveCommandNamespace() 10 10 2

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
namespace Yarak\Console;
4
5
use Yarak\Helpers\Creator;
6
use Yarak\Exceptions\WriteError;
7
8
class CommandCreator extends Creator
9
{
10
    /**
11
     * Create a new command with given name.
12
     *
13
     * @param string $name
14
     *
15
     * @return string
16
     */
17
    public function create($name)
18
    {
19
        if (class_exists($name)) {
20
            throw WriteError::classExists($name);
21
        }
22
23
        $directoryCreator = new DirectoryCreator($this->config, $this->output);
24
25
        $directoryCreator->create();
26
27
        $commandsDir = $this->config->getCommandsDirectory();
28
29
        $this->writeFile(
30
            $path = $commandsDir.$name.'.php',
31
            $this->getStub($name)
32
        );
33
34
        $this->output->writeInfo("Successfully created command {$name}.");
35
36
        return $path;
37
    }
38
39
    /**
40
     * Get the stub file and insert name.
41
     *
42
     * @param string $name
43
     *
44
     * @return string
45
     */
46
    protected function getStub($name)
47
    {
48
        $stub = file_get_contents(__DIR__.'/Stubs/command.stub');
49
50
        $stub = str_replace('CLASSNAME', $name, $stub);
51
52
        return $this->setNamespace($stub, $this->resolveCommandNamespace());
53
    }
54
55
    /**
56
     * Resolve the command namespace.
57
     *
58
     * @return string
59
     */
60 View Code Duplication
    protected function resolveCommandNamespace()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
61
    {
62
        if ($this->config->has(['namespaces', 'consoleNamespace'])) {
63
            return $this->config->get(
64
                ['namespaces', 'consoleNamespace']
65
            ).'\Commands';
66
        }
67
68
        return $this->guessNamespace($this->config->getCommandsDirectory());
69
    }
70
}
71