Completed
Push — master ( cfe8fe...f9982c )
by Rougin
04:59
created

CreateScaffoldCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 120
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 7
c 6
b 1
f 0
lcom 0
cbo 5
dl 15
loc 120
ccs 72
cts 72
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isEnabled() 0 4 1
B configure() 0 40 1
B execute() 15 51 5

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 Rougin\Combustor\Commands;
4
5
use Rougin\Combustor\Common\Tools;
6
use Symfony\Component\Console\Input\ArrayInput;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * Create Scaffold Command
14
 *
15
 * Generates a Wildfire or Doctrine-based controller,
16
 * model and view files for CodeIgniter
17
 * 
18
 * @package Combustor
19
 * @author  Rougin Royce Gutib <[email protected]>
20
 */
21
class CreateScaffoldCommand extends AbstractCommand
22
{
23
    /**
24
     * Checks whether the command is enabled or not in the current environment.
25
     *
26
     * Override this to check for x or y and return false if the command can not
27
     * run properly under the current conditions.
28
     *
29
     * @return bool
30
     */
31 6
    public function isEnabled()
32
    {
33 6
        return Tools::isCommandEnabled();
34
    }
35
36
    /**
37
     * Sets the configurations of the specified command.
38
     *
39
     * @return void
40
     */
41 6
    protected function configure()
42
    {
43 6
        $this->setName('create:scaffold')
44 6
            ->setDescription('Creates a new controller, model and view')
45 6
            ->addArgument(
46 6
                'name',
47 6
                InputArgument::REQUIRED,
48
                'Name of the controller, model and view'
49 6
            )->addOption(
50 6
                'bootstrap',
51 6
                NULL,
52 6
                InputOption::VALUE_NONE,
53
                'Includes the Bootstrap CSS/JS Framework tags'
54 6
            )->addOption(
55 6
                'camel',
56 6
                NULL,
57 6
                InputOption::VALUE_NONE,
58
                'Uses the camel case naming convention'
59 6
            )->addOption(
60 6
                'doctrine',
61 6
                NULL,
62 6
                InputOption::VALUE_NONE,
63
                'Uses the Doctrine\'s specifications'
64 6
            )->addOption(
65 6
                'keep',
66 6
                null,
67 6
                InputOption::VALUE_NONE,
68
                'Keeps the name to be used'
69 6
            )->addOption(
70 6
                'lowercase',
71 6
                null,
72 6
                InputOption::VALUE_NONE,
73
                'Keeps the first character of the name to lowercase'
74 6
            )->addOption(
75 6
                'wildfire',
76 6
                NULL,
77 6
                InputOption::VALUE_NONE,
78
                'Uses the Wildfire\'s specifications'
79 6
            );
80 6
    }
81
82
    /**
83
     * Executes the command.
84
     * 
85
     * @param \Symfony\Component\Console\Input\InputInterface   $input
86
     * @param \Symfony\Component\Console\Output\OutputInterface $output
87
     * @return object|\Symfony\Component\Console\Output\OutputInterface
88
     */
89 3
    protected function execute(InputInterface $input, OutputInterface $output)
90
    {
91
        $commands = [
92 3
            'create:controller',
93 3
            'create:model',
94
            'create:view'
95 3
        ];
96
97 3
        $bootstrap = $input->getOption('bootstrap');
98 3
        $camel = $input->getOption('camel');
99 3
        $doctrine = $input->getOption('doctrine');
100 3
        $keep = $input->getOption('keep');
101 3
        $lowercase = $input->getOption('lowercase');
0 ignored issues
show
Unused Code introduced by
$lowercase is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
102 3
        $lowercase = $input->getOption('lowercase');
103 3
        $wildfire = $input->getOption('wildfire');
104
105 3
        foreach ($commands as $command) {
106
            $arguments = [
107 3
                'command' => $command,
108 3
                'name' => $input->getArgument('name')
109 3
            ];
110
111
            switch ($command) {
112 3 View Code Duplication
                case 'create:controller':
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...
113 3
                    $arguments['--camel'] = $camel;
114 3
                    $arguments['--doctrine'] = $doctrine;
115 3
                    $arguments['--keep'] = $keep;
116 3
                    $arguments['--lowercase'] = $lowercase;
117 3
                    $arguments['--wildfire'] = $wildfire;
118
119 3
                    break;
120 3 View Code Duplication
                case 'create:model':
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...
121 3
                    $arguments['--camel'] = $camel;
122 3
                    $arguments['--doctrine'] = $doctrine;
123 3
                    $arguments['--lowercase'] = $lowercase;
124 3
                    $arguments['--wildfire'] = $wildfire;
125
126 3
                    break;
127 3
                case 'create:view':
128 3
                    $arguments['--bootstrap'] = $bootstrap;
129 3
                    $arguments['--camel'] = $camel;
130 3
                    $arguments['--keep'] = $keep;
131
132 3
                    break;
133
            }
134
135 3
            $input = new ArrayInput($arguments);
136 3
            $application = $this->getApplication()->find($command);
137 3
            $application->run($input, $output);
138 3
        }
139 3
    }
140
}
141