Test Failed
Pull Request — master (#448)
by
unknown
05:18
created

ChangeBranchTask   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 10
loc 60
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A getDescription() 0 11 2
A execute() 0 29 4
A getOptions() 10 10 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
 * This file is part of the Magallanes package.
4
 *
5
 * (c) Andrés Montañez <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Mage\Task\BuiltIn\Git;
12
13
use Mage\Task\Exception\SkipException;
14
use Symfony\Component\Process\Process;
15
use Mage\Task\AbstractTask;
16
17
/**
18
 * Git Task - Checkout Branch
19
 *
20
 * @author Andrés Montañez <[email protected]>
21
 */
22
class ChangeBranchTask extends AbstractTask
23
{
24
    public function getName()
25
    {
26
        return 'git/change-branch';
27
    }
28
29
    public function getDescription()
30
    {
31
        $options = $this->getOptions();
32
        $branch = $options['branch'];
33
34
        if ($this->runtime->getVar('git_revert_branch', false)) {
35
            $branch = $this->runtime->getVar('git_revert_branch');
36
        }
37
38
        return sprintf('[Git] Change Branch (%s)', $branch);
39
    }
40
41
    public function execute()
42
    {
43
        $options = $this->getOptions();
44
        $branch = $this->runtime->getVar('git_revert_branch', false);
45
46
        if ($branch === false) {
47
            $cmdGetCurrent = sprintf('%s branch | grep "*"', $options['path']);
48
49
            /** @var Process $process */
50
            $process = $this->runtime->runLocalCommand($cmdGetCurrent);
51
            if (!$process->isSuccessful()) {
52
                return false;
53
            }
54
55
            $currentBranch = str_replace('* ', '', trim($process->getOutput()));
56
            if ($currentBranch == $options['branch']) {
57
                throw new SkipException();
58
            }
59
60
            $branch = $options['branch'];
61
            $this->runtime->setVar('git_revert_branch', $currentBranch);
62
        }
63
64
        $cmdChange = sprintf('%s checkout %s', $options['path'], $branch);
65
66
        /** @var Process $process */
67
        $process = $this->runtime->runLocalCommand($cmdChange);
68
        return $process->isSuccessful();
69
    }
70
71 View Code Duplication
    protected function getOptions()
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...
72
    {
73
        $branch = $this->runtime->getEnvOption('branch', 'master');
74
        $options = array_merge(
75
            ['path' => 'git', 'branch' => $branch],
76
            $this->options
77
        );
78
79
        return $options;
80
    }
81
}
82