Completed
Push — master ( 5edf6a...0aabb4 )
by juan
02:25
created

DeployProjectsCommand   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 135
Duplicated Lines 4.44 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 6
loc 135
rs 10
wmc 17
lcom 0
cbo 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 16 1
D execute() 6 106 16

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 Juanber84\Console\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Question\Question;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Helper\ProgressBar;
11
use Symfony\Component\Console\Helper\Table;
12
13
class DeployProjectsCommand extends Command
14
{
15
16
    const DEVELOP = 'develop';
17
    const STAGING = 'staging';
18
    const QUALITY = 'quality';
19
    const MASTER  = 'master';
20
21
    const DIRECTORY = '.dep';
22
    const DB = 'db.json';
23
24
    protected function configure()
25
    {
26
        $this
27
            ->setName('deploy-project')
28
            ->setDescription('Auto Deploy Project')
29
            ->addArgument(
30
                'project',
31
                InputArgument::OPTIONAL,
32
                'What\'s the name of project?'
33
            )
34
            ->addArgument(
35
                'branch',
36
                InputArgument::OPTIONAL,
37
                'What\'s the name of branch?'
38
            );
39
    }
40
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $hieranchy = [self::DEVELOP, self::STAGING, self::QUALITY, self::MASTER];
44
        $validBranchs = [self::STAGING, self::QUALITY, self::MASTER];
45
        $nameOfProject = $input->getArgument('project');
46
        $branchOfProject = $input->getArgument('branch');
47
        $helper = $this->getHelper('question');
48
49 View Code Duplication
        if (!$nameOfProject) {
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...
50
            $question = new Question('<question>What is the project key?</question>: ');
51
            do {
52
                $nameOfProject = trim($helper->ask($input, $output, $question));
53
            } while (empty($nameOfProject));
54
        }
55
56
        if (!$branchOfProject) {
57
            $question = new Question('<question>What is the branch to deploy?</question>: ');
58
            do {
59
                $branchOfProject = trim($helper->ask($input, $output, $question));
60
            } while (empty($branchOfProject) || !in_array($branchOfProject, $validBranchs));
61
        }
62
63
        $db = file_get_contents(getenv("HOME").'/'.self::DIRECTORY.'/'.self::DB);
64
        $jsonDb = json_decode($db,true);
65
66
        if (is_null($jsonDb)) {
67
            $output->writeln('');
68
            $output->writeln('<info>0 projects configurated</info>');
69
        } else {
70
            if (isset($jsonDb[$nameOfProject])) {
71
                $merge = self::DEVELOP;
72
                $final = self::STAGING;
73
                foreach ($hieranchy as $k => $v) {
74
                    if ($v == $branchOfProject) {
75
                        $merge = $hieranchy[$k-1];
76
                        $final = $v;
77
                    }
78
                }
79
80
                chdir($jsonDb[$nameOfProject]);
81
82
                $pipetask = [
83
                    'git checkout ' .$merge,
84
                    'git pull',
85
                    'git checkout ' .$final,
86
                    'git merge '.$merge,
87
                    'git push --progress 2>&1',
88
                    'git checkout develop',
89
                ];
90
91
                $progressBar = new ProgressBar($output, count($pipetask));
92
                $progressBar->setBarCharacter('<fg=magenta>=</>');
93
                $progressBar->setFormat("%message%\n %current%/%max% [%bar%] %percent:3s%%");
94
                //$progressBar->setFormat('verbose');
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
95
                $progressBar->setBarWidth(50);
96
97
                $table = new Table($output);
98
                $table->setHeaders(array('<fg=white>Command</>', '<fg=white>Result</>'));
99
100
                $exitCode = 0;
101
                foreach ($pipetask as $t){
102
                    if ($exitCode == 0){
103
                        $command = new \mikehaertl\shellcommand\Command($t);
104
                        if ($command->execute()) {
105
                            $message = $command->getOutput();
106
                            $message = trim(preg_replace('/\t+/', '', $message));
107
                            $message = trim(preg_replace('/Ma\n+/', '', $message));
108
                            $message = trim(preg_replace('/a\n+/', '', $message));
109
                            $exitCode = $command->getExitCode();
110
                        } else {
111
                            $message = $command->getError();
112
                            $message = trim(preg_replace('/\t+/', '', $message));
113
                            $message = trim(preg_replace('/Ma\n+/', '', $message));
114
                            $message = trim(preg_replace('/a\n+/', '', $message));
115
                            $exitCode = $command->getExitCode();
116
                        }
117
                    } else {
118
                        $message = '';
119
                        $exitCode = -1;
120
                    }
121
122
                    if ($exitCode == 0){
123
                        $exitCodeMessage = '<fg=green>SUCCESS</>';
124
                    } elseif ($exitCode == 1){
125
                        $exitCodeMessage = '<fg=red>FAILED</>';
126
                    } elseif ($exitCode == -1){
127
                        $exitCodeMessage = '<fg=blue>ABORTED</>';
128
                    }
129
130
                    $command = '<fg=magenta>'.$t.'</>'."\n".$message;
131
                    $table->addRow([$command, $exitCodeMessage]);
0 ignored issues
show
Bug introduced by
The variable $exitCodeMessage does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
132
                    usleep(300000);
133
                    $progressBar->setMessage($t);
134
                    $progressBar->advance();
135
                }
136
                $progressBar->setMessage("");
137
138
                $progressBar->finish();
139
140
                $output->writeln('');
141
                $table->render();
142
            }
143
144
        }
145
146
    }
147
}