Completed
Pull Request — master (#397)
by De Cramer
02:34
created

Version   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 28.21 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 22
loc 78
rs 10
c 0
b 0
f 0
ccs 0
cts 25
cp 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getExpansionVersion() 0 20 3
A getShortExpansionVersion() 0 4 1
A getBranch() 11 11 2
A getSha() 11 11 2

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
3
namespace eXpansion\Framework\Core\Helpers;
4
5
use PackageVersions\Versions;
6
use Symfony\Component\Process\Process;
7
8
/**
9
 * Class Version
10
 *
11
 * @author    de Cramer Oliver<[email protected]>
12
 * @copyright 2018 Oliverde8
13
 * @package eXpansion\Framework\Core\Helpers
14
 */
15
class Version
16
{
17
    protected $expansionVersion = null;
18
19
    protected $expansionBranch = null;
20
21
    protected $expansionSha = null;
22
23
    /**
24
     * Get current expansion version.
25
     *
26
     * @return null|string
27
     */
28
    public function getExpansionVersion()
29
    {
30
        if (!is_null($this->expansionVersion)) {
31
            return $this->expansionVersion;
32
        }
33
34
        // First check if we are in a git directory. This would be the case if we are in a
35
        // full manual install(not recommended) or on a dev machine.
36
        $process = new Process('git --version');
37
        $process->run();
38
        if ($process->isSuccessful()) {
39
            $branch = $this->getBranch();
40
            $sha = $this->getSha();
41
            $this->expansionVersion = "2.0-$branch@$sha";
42
        } else {
43
            $this->expansionVersion = Versions::getVersion('expansion-mp/expansion');
44
        }
45
46
        return $this->expansionVersion;
47
    }
48
49
    /**
50
     * Get a shorten version of the expansion version.
51
     *
52
     * @return mixed
53
     */
54
    public function getShortExpansionVersion()
55
    {
56
        return explode('@', $this->getExpansionVersion())[0];
57
    }
58
59
    /**
60
     * Get current branch.
61
     *
62
     * @return null|string
63
     */
64 View Code Duplication
    protected function getBranch()
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...
65
    {
66
        if (is_null($this->expansionBranch)) {
67
            $process = new Process('git rev-parse --abbrev-ref HEAD');
68
            $process->run();
69
70
            $this->expansionBranch = trim($process->getOutput());
71
        }
72
73
        return $this->expansionBranch;
74
    }
75
76
    /**
77
     * Get current commit sha.
78
     *
79
     * @return null|string
80
     */
81 View Code Duplication
    protected function getSha()
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...
82
    {
83
        if (is_null($this->expansionSha)) {
84
            $process = new Process('git rev-parse HEAD');
85
            $process->run();
86
87
            $this->expansionSha = trim($process->getOutput());
88
        }
89
90
        return $this->expansionSha;
91
    }
92
}