Completed
Push — master ( 657524...bde6a9 )
by Simone
19s
created

Container   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 111
Duplicated Lines 37.84 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 22
c 6
b 0
f 2
lcom 1
cbo 0
dl 42
loc 111
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 22 6
A countResources() 0 6 1
A create() 10 16 4
A allowed() 0 15 3
A mandatory() 8 8 2
A defaults() 8 8 2
A rules() 8 8 2
A allowedValues() 8 8 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 Sensorario\Resources;
4
5
use \RuntimeException;
6
7
class Container
8
{
9
    private $resources;
10
11
    private $allowed = [
12
        'allowed',
13
        'allowedValues',
14
        'defaults',
15
        'mandatory',
16
        'rules',
17
    ];
18
19
    public function __construct(array $resources)
20
    {
21
        if (!isset($resources['resources'])) {
22
            throw new RuntimeException(
23
                ''
24
            );
25
        }
26
27
        foreach ($resources['resources'] as $item) {
28
            if (isset($item['constraints'])) {
29
                foreach ($item['constraints'] as $name => $value) {
30
                    if (!in_array($name, $this->allowed)) {
31
                        throw new RuntimeException(
32
                            'Invalid constraint'
33
                        );
34
                    }
35
                }
36
            }
37
        }
38
39
        $this->resources = $resources;
40
    }
41
42
    public function countResources()
43
    {
44
        return count(
45
            $this->resources['resources']
46
        );
47
    }
48
49
    public function create($resource, array $constraints)
50
    {
51
        foreach ($constraints as $name => $value) {
52 View Code Duplication
            if (!isset($this->resources['resources'][$resource]['constraints']['allowed'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
53
                throw new RuntimeException(
54
                    'Not allowed `' . $name . '` constraint'
55
                );
56
            }
57
58 View Code Duplication
            if (!in_array($name, $this->resources['resources'][$resource]['constraints']['allowed'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
59
                throw new RuntimeException(
60
                    'Not allowed `' . $name . '` constraint'
61
                );
62
            }
63
        }
64
    }
65
66
    public function allowed($resource)
67
    {
68
        $allowed = [];
69
70
        foreach ($this->allowed as $item) {
71
            if (isset($this->resources['resources'][$resource]['constraints'][$item])) {
72
                $allowed = array_merge(
73
                    $allowed,
74
                    $this->resources['resources'][$resource]['constraints'][$item]
75
                );
76
            }
77
        }
78
79
        return $allowed;
80
    }
81
82 View Code Duplication
    public function mandatory($resource)
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...
83
    {
84
        if (isset($this->resources['resources'][$resource]['constraints']['mandatory'])) {
85
            return $this->resources['resources'][$resource]['constraints']['mandatory'];
86
        }
87
88
        return [];
89
    }
90
91 View Code Duplication
    public function defaults($resource)
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...
92
    {
93
        if (isset($this->resources['resources'][$resource]['constraints']['defaults'])) {
94
            return $this->resources['resources'][$resource]['constraints']['defaults'];
95
        }
96
97
        return [];
98
    }
99
100 View Code Duplication
    public function rules($resource)
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
        if (isset($this->resources['resources'][$resource]['constraints']['rules'])) {
103
            return $this->resources['resources'][$resource]['constraints']['rules'];
104
        }
105
106
        return [];
107
    }
108
109 View Code Duplication
    public function allowedValues($resource)
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...
110
    {
111
        if (isset($this->resources['resources'][$resource]['constraints']['allowedValues'])) {
112
            return $this->resources['resources'][$resource]['constraints']['allowedValues'];
113
        }
114
115
        return [];
116
    }
117
}
118