Test Failed
Push — master ( 9c106a...88fe25 )
by Jay
06:44
created

ProjectVersion::getEnv()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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
    final public static function getInstance()
17
    {
18
        if (!self::$instance) {
19
            $class = get_called_class();
20
            self::$instance = new $class;
21
        }
22
        return self::$instance;
23
    }
24
25
    /**
26
     * private construct - singleton
27
     */
28
    private function __construct()
29
    {
30
    }
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
    public function get()
45
    {
46
        $version = $this->getEnv('PROJECT_VERSION');
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getEnv('PROJECT_VERSION'); of type array|false|string adds the type array to the return on line 48 which is incompatible with the return type documented by FMUP\ProjectVersion::get of type string.
Loading history...
47
        if ($version) {
48
            return $version;
49
        }
50
        try {
51
            if (isset($this->getStructure()->version)) {
52
                return $this->getStructure()->version;
53
            }
54
        } catch (\LogicException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
55
        }
56
        $rootPath = $this->getGitHeadFilePath();
57
        if (file_exists($rootPath)) {
58
            $stringFromFile = file($rootPath);
59
            $firstLine = $stringFromFile[0]; //get the string from the array
60
            $explodedString = explode("/", $firstLine, 3); //seperate out by the "/" in the string
61
            return trim($explodedString[2]); //get the one that is always the branch name
62
        }
63
        return 'v0.0.0';
64
    }
65
66
    /**
67
     * @return string
68
     * @codeCoverageIgnore
69
     */
70
    protected function getGitHeadFilePath()
71
    {
72
        return implode(DIRECTORY_SEPARATOR, array(__DIR__, '..', '..', '..', '..', '.git', 'HEAD'));
73
    }
74
75
    /**
76
     * Retrieve environment variable
77
     * @param string $name
78
     * @return array|false|string
79
     * @codeCoverageIgnore
80
     */
81
    protected function getEnv($name)
82
    {
83
        return getenv($name);
84
    }
85
86
    public function name()
87
    {
88
        return $this->getStructure()->name;
89
    }
90
91
    /**
92
     * Return composer.json path to project
93
     * @return string
94
     */
95
    protected function getComposerPath()
96
    {
97
        return implode(DIRECTORY_SEPARATOR, array(__DIR__, '..', '..', '..', '..', 'composer.json'));
98
    }
99
100
    /**
101
     * Get composer file structure
102
     * @return Object
103
     */
104 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...
105
    {
106
        if (!$this->structure) {
107
            if (!is_file($this->getComposerPath())) {
108
                throw new \LogicException('composer.json does not exist');
109
            }
110
            $this->structure = json_decode(file_get_contents($this->getComposerPath()));
111
            if (!$this->structure) {
112
                throw new \LogicException('composer.json invalid structure');
113
            }
114
        }
115
        return $this->structure;
116
    }
117
}
118