Passed
Push — master ( 283a3f...95122b )
by Andrea
17:50
created

VersioneExtension   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 88.46%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 49
ccs 23
cts 26
cp 0.8846
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A versioneTagGit() 0 25 5
A __construct() 0 3 1
A isWindows() 0 6 2
A getFunctions() 0 4 1
1
<?php
2
3
namespace Cdf\BiCoreBundle\Twig\Extension;
4
5
use Symfony\Component\Process\Process;
6
use Symfony\Component\Cache\Simple\FilesystemCache;
7
8
class VersioneExtension extends \Twig_Extension
9
{
10
    private $projectpath;
11
12 26
    public function __construct($projectpath)
13
    {
14 26
        $this->projectpath = $projectpath;
15 26
    }
16
17 1
    public function getFunctions()
18
    {
19
        return array(
20 1
            new \Twig_SimpleFunction('versione_tag_git', array($this, 'versioneTagGit', 'is_safe' => array('html'))),
21
        );
22
    }
23
24 12
    public function versioneTagGit()
25
    {
26 12
        if ($this->isWindows()) {
27
            return 0;
28
        }
29 12
        $cache = new FilesystemCache();
30 12
        if ($cache->has('git_tag')) {
31 12
            $version = $cache->get('git_tag');
32
        } else {
33 2
            $projectDir = $this->projectpath;
34 2
            $process = new Process(array('git', 'describe', '--tags'));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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 2
            $process->setWorkingDirectory($projectDir);
36 2
            $process->setTimeout(60 * 100);
37 2
            $process->run();
38 2
            if ($process->isSuccessful()) {
39 2
                $out = explode(chr(10), $process->getOutput());
40
41 2
                $version = isset($out[0]) ? $out[0] : '0';
42 2
                $cache->set('git_tag', $version);
43
            } else {
44
                $version = '0';
45
            }
46
        }
47
48 12
        return $version;
49
    }
50
51 12
    private function isWindows()
52
    {
53 12
        if (PHP_OS == 'WINNT') {
54
            return true;
55
        } else {
56 12
            return false;
57
        }
58
    }
59
}
60