Passed
Push — master ( 88d705...44ae42 )
by Andrea
12:16
created

CheckgitversionCommand::getGitVersion()   D

Complexity

Conditions 10
Paths 8

Size

Total Lines 46
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 30
nc 8
nop 2
dl 0
loc 46
ccs 0
cts 32
cp 0
crap 110
rs 4.983
c 0
b 0
f 0

How to fix   Complexity   

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
namespace Fi\PannelloAmministrazioneBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Finder\Finder;
9
use Symfony\Component\Process\Process;
10
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
11
12
class CheckgitversionCommand extends ContainerAwareCommand
13
{
14 3
    protected function configure()
15
    {
16 3
        $this
17 3
            ->setName('pannelloamministrazione:checkgitversion')
18 3
            ->setDescription('Controllo versioni bundles')
19 3
            ->setHelp('Controlla le versioni git dei bundles');
20 3
    }
21
22
    protected function execute(InputInterface $input, OutputInterface $output)
23
    {
24
        if (self::isWindows()) {
25
            $output->writeln('<info>Non previsto in ambiente windows</info>');
26
27
            return 0;
28
        }
29
30
        $projectDir = substr($this->getContainer()->get('kernel')->getRootDir(), 0, -4);
31
32
        $composerbundles = array();
33
        $composerbundlespath = $projectDir.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'fi';
34
        $findercomposerbundle = new Finder();
35
        $findercomposerbundle->in($composerbundlespath)->sortByName()->directories()->depth('== 0');
36
37
        foreach ($findercomposerbundle as $file) {
38
            $fullcomposerbundlepath = $composerbundlespath.DIRECTORY_SEPARATOR.$file->getBasename();
39
            $local = $this->getGitVersion($fullcomposerbundlepath, false);
40
            $remote = $this->getGitVersion($fullcomposerbundlepath, true);
41
            $style = new OutputFormatterStyle('blue', 'white', array('bold', 'blink'));
42
            $output->getFormatter()->setStyle('warning', $style);
43
            if ($local !== $remote) {
44
                $remote = '<warning> * '.$remote.' * </warning>';
45
            }
46
            $output->writeln('<info>'.$file->getBasename().'</info> '.$local.' -> '.$remote);
47
48
            $composerbundles[] = array(
49
                'name' => $file->getBasename(),
50
                'path' => $fullcomposerbundlepath,
51
                'version' => $this->getGitVersion($fullcomposerbundlepath),
52
            );
53
        }
54
55
        return 0;
56
    }
57
58
    private function getGitVersion($path, $remote = false)
59
    {
60
        if (self::isWindows()) {
61
            return '';
62
        }
63
64
        if ($remote) {
65
            //Remote
66
            $cmd = 'cd '.$path;
67
            $remotetag = $cmd.";git ls-remote -t | awk '{print $2}' | cut -d '/' -f 3 | cut -d '^' -f 1 | sort --version-sort | tail -1";
68
            $process = new Process($remotetag);
69
            $process->setTimeout(60 * 100);
70
            $process->run();
71
            if ($process->isSuccessful()) {
72
                $version = trim($process->getOutput());
73
                if (preg_match('/\d+(?:\.\d+)+/', $version, $matches)) {
74
                    return $matches[0]; //returning the first match
75
                }
76
            }
77
78
            return '?';
79
        } else {
80
            //Local
81
            $cmd = 'cd '.$path;
82
            $process = new Process($cmd.';git branch | '."grep ' * '");
83
            $process->setTimeout(60 * 100);
84
            $process->run();
85
            if ($process->isSuccessful()) {
86
                $out = explode(chr(10), $process->getOutput());
87
                foreach ($out as $line) {
88
                    if (strpos($line, '* ') !== false) {
89
                        $version = trim(strtolower(str_replace('* ', '', $line)));
90
                        if ($version == 'master') {
91
                            return $version;
92
                        } else {
93
                            if (preg_match('/\d+(?:\.\d+)+/', $version, $matches)) {
94
                                return $matches[0]; //returning the first match
95
                            }
96
                        }
97
                    }
98
                }
99
            } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
100
                //echo $process->getErrorOutput();
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
101
            }
102
103
            return '?';
104
        }
105
    }
106
107
    public static function isWindows()
108
    {
109
        if (PHP_OS == 'WINNT') {
110
            return true;
111
        } else {
112
            return false;
113
        }
114
    }
115
}
116