Passed
Push — master ( 5e8bf6...1d619d )
by Andrea
25:03
created

getLocalVersionStringDetail()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 10
ccs 0
cts 0
cp 0
crap 12
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
12
class CheckgitversionCommand extends Command
13
{
14
15
    protected static $defaultName = 'pannelloamministrazione:checkgitversion';
16
17
    protected function configure()
18
    {
19
        $this
20
                ->setDescription('Controllo versioni bundles')
21
                ->setHelp('Controlla le versioni git dei bundles');
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...
Bug introduced by
The method getContainer() does not exist on Cdf\PannelloAmministrazi...\CheckgitversionCommand. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        $papath = $this->/** @scrutinizer ignore-call */ getContainer()->get('pannelloamministrazione.projectpath');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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
    private function getGitVersion($path, $remote = false)
59
    {
60
        if (self::isWindows()) {
61
            return '';
62
        }
63
64
        if ($remote) {
65
            //Remote
66
            $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...
67
            $remotetagscmd = "git ls-remote -t | awk '{print $2}' | cut -d '/' -f 3 | cut -d '^' -f 1 | sort --version-sort | tail -1";
68
            $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...
69
            $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...
70
            $process->setTimeout(60 * 100);
71
            $process->run();
72
            if ($process->isSuccessful()) {
73
                $versions = trim($process->getOutput());
74
                return $this->getRemoteVersionString($versions);
75
            }
76
            return '?';
77
        } else {
78
            //Local
79
            $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...
80
            $process = new Process($cmd . ';git branch | ' . "grep ' * '");
81
            $process->setTimeout(60 * 100);
82
            $process->run();
83
            if ($process->isSuccessful()) {
84
                $versions = explode(chr(10), $process->getOutput());
85
                return $this->getLocalVersionString($versions);
86
            } else {
87
                //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...
88
                return '?';
89
            }
90
        }
91
    }
92
    private function getLocalVersionString($versions)
93
    {
94
        foreach ($versions as $line) {
95
            if (strpos($line, '* ') !== false) {
96
                $version = trim(strtolower(str_replace('* ', '', $line)));
97
                return $this->getLocalVersionStringDetail($version);
98
            }
99
        }
100
        return '?';
101
    }
102
    private function getLocalVersionStringDetail($versions)
103
    {
104
        if ($versions == 'master') {
105
            return $versions;
106
        } else {
107
            if (preg_match('/\d+(?:\.\d+)+/', $versions, $matches)) {
108
                return $matches[0]; //returning the first match
109
            }
110
        }
111
        return '?';
112
    }
113
    private function getRemoteVersionString($versions)
114
    {
115
        if (preg_match('/\d+(?:\.\d+)+/', $versions, $matches)) {
116
            return $matches[0]; //returning the first match
117
        }
118
        return '?';
119
    }
120
    public static function isWindows()
121
    {
122
        if (PHP_OS == 'WINNT') {
123
            return true;
124
        } else {
125
            return false;
126
        }
127
    }
128
// @codeCoverageIgnoreEnd
129
}
130