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

ChangeBranchTask::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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