Completed
Push — master ( 5b3e8e...9e1e3c )
by Jay
04:05
created

ProjectVersion   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 106
Duplicated Lines 12.26 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 96.55%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 13
loc 106
ccs 28
cts 29
cp 0.9655
rs 10
wmc 18
lcom 2
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 8 2
A __construct() 0 3 1
A __clone() 0 3 1
B get() 0 15 5
A getFromGit() 0 6 2
A getEnv() 0 4 1
A name() 0 4 1
A getComposerPath() 0 4 1
A getStructure() 13 13 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace FMUP;
3
4
/**
5
 * Class ProjectVersion
6
 * @package LogiCE
7
 */
8
class ProjectVersion
9
{
10
    private static $instance;
11
    private $structure;
12
13
    /**
14
     * @return $this
15
     */
16 2
    final public static function getInstance()
17
    {
18 2
        if (!self::$instance) {
19 1
            $class = get_called_class();
20 1
            self::$instance = new $class;
21 1
        }
22 2
        return self::$instance;
23
    }
24
25
    /**
26
     * private construct - singleton
27
     */
28 1
    private function __construct()
29
    {
30 1
    }
31
32
    /**
33
     * private clone - singleton
34
     * @codeCoverageIgnore
35
     */
36
    private function __clone()
37
    {
38
    }
39
40
    /**
41
     * Get version name
42
     * @return string
43
     */
44 6
    public function get()
45
    {
46 6
        $version = (string)$this->getEnv('PROJECT_VERSION');
47 6
        if ($version) {
48 1
            return $version;
49
        }
50
        try {
51 5
            if (isset($this->getStructure()->version)) {
52 1
                return $this->getStructure()->version;
53
            }
54 4
        } catch (\LogicException $e) {
55
            //Do nothing since we have a fallback
56
        }
57 4
        return trim($this->getFromGit()) ?: 'v0.0.0';
58
    }
59
60
    /**
61
     * @return string
62
     * @codeCoverageIgnore
63
     */
64
    protected function getFromGit()
65
    {
66
        $rootPath = implode(DIRECTORY_SEPARATOR, array(__DIR__, '..', '..', '..', '..'));
67
        exec("cd $rootPath && git describe", $gitVersion, $errorCode);
68
        return !$errorCode ? $gitVersion : "";
69
    }
70
71
    /**
72
     * Retrieve environment variable
73
     * @param string $name
74
     * @return array|false|string
75
     * @codeCoverageIgnore
76
     */
77
    protected function getEnv($name)
78
    {
79
        return getenv($name);
80
    }
81
82 1
    public function name()
83
    {
84 1
        return $this->getStructure()->name;
85
    }
86
87
    /**
88
     * Return composer.json path to project
89
     * @return string
90
     */
91 1
    protected function getComposerPath()
92
    {
93 1
        return implode(DIRECTORY_SEPARATOR, array(__DIR__, '..', '..', '..', '..', 'composer.json'));
94
    }
95
96
    /**
97
     * Get composer file structure
98
     * @return Object
99
     */
100 3 View Code Duplication
    protected function getStructure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102 3
        if (!$this->structure) {
103 3
            if (!is_file($this->getComposerPath())) {
104 2
                throw new \LogicException('composer.json does not exist');
105
            }
106 1
            $this->structure = json_decode(file_get_contents($this->getComposerPath()));
107 1
            if (!$this->structure) {
108
                throw new \LogicException('composer.json invalid structure');
109
            }
110 1
        }
111 1
        return $this->structure;
112
    }
113
}
114