Passed
Push — 4.1 ( c5cc9d...87d7e6 )
by Andrea
13:29
created

CheckgitversionCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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
15 2
    protected function configure()
16
    {
17
        $this
18 2
                ->setName('pannelloamministrazione:checkgitversion')
19 2
                ->setDescription('Controllo versioni bundles')
20 2
                ->setHelp('Controlla le versioni git dei bundles');
21 2
    }
22
23
    // @codeCoverageIgnoreStart
24
    protected function execute(InputInterface $input, OutputInterface $output)
25
    {
26
        if (self::isWindows()) {
27
            $output->writeln('<info>Non previsto in ambiente windows</info>');
28
29
            return 0;
30
        }
31
32
        $composerbundles = array();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
33
        $papath = $this->getContainer()->get('pannelloamministrazione.projectpath');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 15 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
34
        $composerbundlespath =  $papath->getVendorBinPath(). '/../fi';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
35
        $findercomposerbundle = new Finder();
36
        $findercomposerbundle->in($composerbundlespath)->sortByName()->directories()->depth('== 0');
37
38
        foreach ($findercomposerbundle as $file) {
39
            $fullcomposerbundlepath = $composerbundlespath . DIRECTORY_SEPARATOR . $file->getBasename();
40
            $local = $this->getGitVersion($fullcomposerbundlepath, false);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 18 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
41
            $remote = $this->getGitVersion($fullcomposerbundlepath, true);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 17 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
42
            $style = new OutputFormatterStyle('blue', 'white', array('bold', 'blink'));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 18 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
43
            $output->getFormatter()->setStyle('warning', $style);
44
            if ($local !== $remote) {
45
                $remote = '<warning> * ' . $remote . ' * </warning>';
46
            }
47
            $output->writeln('<info>' . $file->getBasename() . '</info> ' . $local . ' -> ' . $remote);
48
49
            $composerbundles[] = array(
50
                'name' => $file->getBasename(),
51
                'path' => $fullcomposerbundlepath,
52
                'version' => $this->getGitVersion($fullcomposerbundlepath),
53
            );
54
        }
55
56
        return 0;
57
    }
58
59
    private function getGitVersion($path, $remote = false)
60
    {
61
        if (self::isWindows()) {
62
            return '';
63
        }
64
65
        if ($remote) {
66
            //Remote
67
            $cmd = 'cd ' . $path;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
68
            $remotetagscmd = "git ls-remote -t | awk '{print $2}' | cut -d '/' -f 3 | cut -d '^' -f 1 | sort --version-sort | tail -1";
69
            $remotetag = $cmd . ";" . $remotetagscmd;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style Comprehensibility introduced by
The string literal ; does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
70
            $process = new Process($remotetag);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
71
            $process->setTimeout(60 * 100);
72
            $process->run();
73
            if ($process->isSuccessful()) {
74
                $versions = trim($process->getOutput());
75
                return $this->getRemoteVersionString($versions);
76
            }
77
            return '?';
78
        } else {
79
            //Local
80
            $cmd = 'cd ' . $path;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
81
            $process = new Process($cmd . ';git branch | ' . "grep ' * '");
82
            $process->setTimeout(60 * 100);
83
            $process->run();
84
            if ($process->isSuccessful()) {
85
                $versions = explode(chr(10), $process->getOutput());
86
                return $this->getLocalVersionString($versions);
87
            } else {
88
                //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...
89
                return '?';
90
            }
91
        }
92
    }
93
94
    private function getLocalVersionString($versions)
95
    {
96
        foreach ($versions as $line) {
97
            if (strpos($line, '* ') !== false) {
98
                $version = trim(strtolower(str_replace('* ', '', $line)));
99
                return $this->getLocalVersionStringDetail($version);
100
            }
101
        }
102
        return '?';
103
    }
104
105
    private function getLocalVersionStringDetail($versions)
106
    {
107
        if ($versions == 'master') {
108
            return $versions;
109
        } else {
110
            if (preg_match('/\d+(?:\.\d+)+/', $versions, $matches)) {
111
                return $matches[0]; //returning the first match
112
            }
113
        }
114
        return '?';
115
    }
116
117
    private function getRemoteVersionString($versions)
118
    {
119
        if (preg_match('/\d+(?:\.\d+)+/', $versions, $matches)) {
120
            return $matches[0]; //returning the first match
121
        }
122
        return '?';
123
    }
124
125
    public static function isWindows()
126
    {
127
        if (PHP_OS == 'WINNT') {
128
            return true;
129
        } else {
130
            return false;
131
        }
132
    }
133
// @codeCoverageIgnoreEnd
134
}
135