Completed
Pull Request — master (#56)
by Simone
02:28
created

Container   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 91
Duplicated Lines 28.57 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 18
c 4
b 0
f 1
lcom 1
cbo 0
dl 26
loc 91
rs 10

6 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

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
        'mandatory',
14
        'defaults',
15
    ];
16
17
    public function __construct(array $resources)
18
    {
19
        if (!isset($resources['resources'])) {
20
            throw new RuntimeException(
21
                ''
22
            );
23
        }
24
25
        foreach ($resources['resources'] as $item) {
26
            if (isset($item['constraints'])) {
27
                foreach ($item['constraints'] as $name => $value) {
28
                    if (!in_array($name, $this->allowed)) {
29
                        throw new RuntimeException(
30
                            'Invalid constraint'
31
                        );
32
                    }
33
                }
34
            }
35
        }
36
37
        $this->resources = $resources;
38
    }
39
40
    public function countResources()
41
    {
42
        return count(
43
            $this->resources['resources']
44
        );
45
    }
46
47
    public function create($resource, array $constraints)
48
    {
49
        foreach ($constraints as $name => $value) {
50 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...
51
                throw new RuntimeException(
52
                    'Not allowed `' . $name . '` constraint'
53
                );
54
            }
55
56 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...
57
                throw new RuntimeException(
58
                    'Not allowed `' . $name . '` constraint'
59
                );
60
            }
61
        }
62
    }
63
64
    public function allowed($resource)
65
    {
66
        $allowed = [];
67
68
        foreach ($this->allowed as $item) {
69
            if (isset($this->resources['resources'][$resource]['constraints'][$item])) {
70
                $allowed = array_merge(
71
                    $allowed,
72
                    $this->resources['resources'][$resource]['constraints'][$item]
73
                );
74
            }
75
        }
76
77
        return $allowed;
78
    }
79
80 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...
81
    {
82
        if (isset($this->resources['resources'][$resource]['constraints']['mandatory'])) {
83
            return $this->resources['resources'][$resource]['constraints']['mandatory'];
84
        }
85
86
        return [];
87
    }
88
89 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...
90
    {
91
        if (isset($this->resources['resources'][$resource]['constraints']['defaults'])) {
92
            return $this->resources['resources'][$resource]['constraints']['defaults'];
93
        }
94
95
        return [];
96
    }
97
}
98