Passed
Push — master ( 1d619d...8f091f )
by Andrea
16:20
created

CheckgitversionCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\PannelloAmministrazioneBundle\Command;
4
5
use Symfony\Component\Console\Command\Command;
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
use Cdf\PannelloAmministrazioneBundle\Utils\ProjectPath;
12
13
class CheckgitversionCommand extends Command
14
{
15
16
    protected static $defaultName = 'pannelloamministrazione:checkgitversion';
17
18
    protected function configure()
19
    {
20
        $this
21
                ->setDescription('Controllo versioni bundles')
22
                ->setHelp('Controlla le versioni git dei bundles');
23
    }
24
    // @codeCoverageIgnoreStart
25
    public function __construct(ProjectPath $projectpath)
26
    {
27
        $this->projectpath = $projectpath;
0 ignored issues
show
Bug Best Practice introduced by
The property projectpath does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
28
29
        // you *must* call the parent constructor
30
        parent::__construct();
31
    }
32
    protected function execute(InputInterface $input, OutputInterface $output)
33
    {
34
        if (self::isWindows()) {
35
            $output->writeln('<info>Non previsto in ambiente windows</info>');
36
37
            return 0;
38
        }
39
40
        $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...
41
        $papath = $this->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...
42
        $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...
Coding Style Comprehensibility introduced by
The string literal /../fi 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...
43
        $findercomposerbundle = new Finder();
44
        $findercomposerbundle->in($composerbundlespath)->sortByName()->directories()->depth('== 0');
45
46
        foreach ($findercomposerbundle as $file) {
47
            $fullcomposerbundlepath = $composerbundlespath . DIRECTORY_SEPARATOR . $file->getBasename();
48
            $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...
49
            $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...
50
            $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...
51
            $output->getFormatter()->setStyle('warning', $style);
52
            if ($local !== $remote) {
53
                $remote = '<warning> * ' . $remote . ' * </warning>';
54
            }
55
            $output->writeln('<info>' . $file->getBasename() . '</info> ' . $local . ' -> ' . $remote);
56
57
            $composerbundles[] = array(
58
                'name' => $file->getBasename(),
59
                'path' => $fullcomposerbundlepath,
60
                'version' => $this->getGitVersion($fullcomposerbundlepath),
61
            );
62
        }
63
64
        return 0;
65
    }
66
    private function getGitVersion($path, $remote = false)
67
    {
68
        if (self::isWindows()) {
69
            return '';
70
        }
71
72
        if ($remote) {
73
            //Remote
74
            $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...
75
            $remotetagscmd = "git ls-remote -t | awk '{print $2}' | cut -d '/' -f 3 | cut -d '^' -f 1 | sort --version-sort | tail -1";
76
            $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...
77
            $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...
78
            $process->setTimeout(60 * 100);
79
            $process->run();
80
            if ($process->isSuccessful()) {
81
                $versions = trim($process->getOutput());
82
                return $this->getRemoteVersionString($versions);
83
            }
84
            return '?';
85
        } else {
86
            //Local
87
            $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...
88
            $process = new Process($cmd . ';git branch | ' . "grep ' * '");
89
            $process->setTimeout(60 * 100);
90
            $process->run();
91
            if ($process->isSuccessful()) {
92
                $versions = explode(chr(10), $process->getOutput());
93
                return $this->getLocalVersionString($versions);
94
            } else {
95
                //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...
96
                return '?';
97
            }
98
        }
99
    }
100
    private function getLocalVersionString($versions)
101
    {
102
        foreach ($versions as $line) {
103
            if (strpos($line, '* ') !== false) {
104
                $version = trim(strtolower(str_replace('* ', '', $line)));
105
                return $this->getLocalVersionStringDetail($version);
106
            }
107
        }
108
        return '?';
109
    }
110
    private function getLocalVersionStringDetail($versions)
111
    {
112
        if ($versions == 'master') {
113
            return $versions;
114
        } else {
115
            if (preg_match('/\d+(?:\.\d+)+/', $versions, $matches)) {
116
                return $matches[0]; //returning the first match
117
            }
118
        }
119
        return '?';
120
    }
121
    private function getRemoteVersionString($versions)
122
    {
123
        if (preg_match('/\d+(?:\.\d+)+/', $versions, $matches)) {
124
            return $matches[0]; //returning the first match
125
        }
126
        return '?';
127
    }
128
    public static function isWindows()
129
    {
130
        if (PHP_OS == 'WINNT') {
131
            return true;
132
        } else {
133
            return false;
134
        }
135
    }
136
// @codeCoverageIgnoreEnd
137
}
138