Completed
Push — master ( b77c5c...4fbeac )
by Jeroen
10s
created

StatusCommand::execute()   C

Complexity

Conditions 9
Paths 17

Size

Total Lines 67
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 67
ccs 0
cts 2
cp 0
rs 6.3448
c 0
b 0
f 0
cc 9
eloc 42
nc 17
nop 2
crap 90

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Conveyor package.
5
 *
6
 * (c) Jeroen Fiege <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webcreate\Conveyor\Command;
13
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class StatusCommand extends AbstractCommand
19
{
20 2
    protected function configure()
21
    {
22
        $this
23 2
            ->setName('status')
24 2
            ->setDescription('Show status for each target')
25 2
            ->addArgument('target', InputArgument::OPTIONAL, 'Show the status for a specific target')
26
        ;
27 2
    }
28
29
    protected function execute(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Coding Style introduced by
execute uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
30
    {
31
        $deploy = $this->getConveyor($input, $output, $this->getHelperSet());
32
33
        $target = $input->getArgument('target');
34
35
        $status = $deploy->status($target);
36
37
        if (count($status) > 0) {
38
            $output->writeln('<comment>Available targets:</comment>');
39
40
            // calculate max string length of targets
41
            $maxStrlen = max(array_map(function ($item) { return strlen($item); }, array_keys($status)));
0 ignored issues
show
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
42
43
            foreach ($status as $target => $info) {
44
                $abortMessage = null;
45
46
                if (isset($info['error'])) {
47
                    $abortMessage = sprintf('<error>Error: %s</error>', $info['error']->getMessage());
48
                } elseif (false === $info) {
49
                    $abortMessage = 'Not deployed';
50
                }
51
52
                if (null !== $abortMessage) {
53
                    $output->writeln(
54
                        sprintf(
55
                            '  <info>%-'.($maxStrlen + 2).'s</info> %s',
56
                            $target,
57
                            $abortMessage
58
                        )
59
                    );
60
61
                    continue;
62
                }
63
64
                $version        = $info['remoteVersion'];
65
                $localVersion   = $info['localVersion'];
66
                $versionCompare = $info['compare'];
67
68
                $statusTxt = '';
69
                if (0 === $versionCompare) {
70
                    $statusTxt = 'Up-to-date';
71
                } elseif (1 === $versionCompare) {
72
                    $statusTxt = sprintf('Behind by %d commit(s) on %s', count($info['changelog']), $localVersion->getName());
73
                } elseif (-1 === $versionCompare) {
74
                    $statusTxt = sprintf('Ahead by %d commit(s) on %s', count($info['changelog']), $localVersion->getName());
75
                }
76
77
                $output->writeln(
78
                    sprintf(
79
                        '  <info>%-'.($maxStrlen + 2).'s</info> <info>%s</info> (<comment>%s</comment>) %s',
80
                        $target,
81
                        $version->getName(),
82
                        $version->getBuild(),
83
                        $statusTxt
84
                    )
85
                );
86
            }
87
88
            $baseVersion = 'dev-' . $deploy->getRepository()->getMasterBranch();
89
90
            $output->writeln('');
91
            $output->writeln(sprintf('Run `%s diff <target> %s` to view changed files', basename($_SERVER['argv'][0]), $baseVersion));
92
        } else {
93
            $output->writeln('No targets defined');
94
        }
95
    }
96
}
97