Version   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 17.33 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
dl 13
loc 75
ccs 20
cts 20
cp 1
rs 10
c 2
b 2
f 0
wmc 12
lcom 2
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __clone() 0 3 1
A getComposerPath() 0 4 1
A getInstance() 0 8 2
A get() 0 9 3
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
 * Retrieve FMUP Version
6
 * @package FMUP
7
 */
8
class Version
9
{
10
    private static $instance;
11
    private $structure;
12
13
    /**
14
     * @return $this
15
     */
16 3
    final public static function getInstance()
17
    {
18 3
        if (!self::$instance) {
19 1
            $class = get_called_class();
20 1
            self::$instance = new $class;
21
        }
22 3
        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 4
    public function get()
45
    {
46 4
        foreach ($this->getStructure()->packages as $package) {
47 2
            if ($package->name == 'fmup/fmup') {
48 2
                return $package->version;
49
            }
50
        }
51 1
        return "v0.0.0";
52
    }
53
54
    /**
55
     * Get composer json path
56
     * @return string
57
     * @codeCoverageIgnore
58
     */
59
    protected function getComposerPath()
60
    {
61
        return implode(DIRECTORY_SEPARATOR, array(__DIR__, '..', '..', '..', '..', 'composer.lock'));
62
    }
63
64
    /**
65
     * Get composer file structure
66
     * @return Object
67
     * @throws Exception
68
     */
69 4 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...
70
    {
71 4
        if (!$this->structure) {
72 4
            if (!is_file($this->getComposerPath())) {
73 1
                throw new Exception('composer.lock does not exist');
74
            }
75 3
            $this->structure = json_decode(file_get_contents($this->getComposerPath()));
76 3
            if (!$this->structure) {
77 1
                throw new Exception('composer.lock invalid structure');
78
            }
79
        }
80 2
        return $this->structure;
81
    }
82
}
83