Completed
Pull Request — master (#85)
by Simone
02:05
created

Container   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 122
Duplicated Lines 15.57 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 1
dl 19
loc 122
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 27 6
B ensureValidConstraints() 0 16 5
A countResources() 0 6 1
A create() 10 16 4
A allowed() 6 15 3
A getConstraints() 3 10 2
A rewrites() 0 4 1
A globals() 0 4 1

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
    extends ContainerBase
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
9
{
10
    private $resources;
11
12
    private $rewrites;
13
14
    private $allowed = [
15
        'allowed',
16
        'allowedRanges',
17
        'allowedValues',
18
        'defaults',
19
        'mandatory',
20
        'rules',
21
    ];
22
23
    public function __construct(array $resources)
24
    {
25
        if (!isset($resources['resources'])) {
26
            throw new RuntimeException(
27
                'resources element is not defined'
28
            );
29
        }
30
31
        $this->rewrites = [];
32
        $this->globals  = [];
0 ignored issues
show
Bug introduced by
The property globals does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
34
        $this->ensureValidConstraints($resources);
35
36
        foreach ($resources['resources'] as $item) {
37
            if (isset($item['rewrite'])) {
38
                foreach ($item['rewrite'] as $field => $rewriteRule) {
39
                    $this->rewrites[$field] = $rewriteRule;
40
                }
41
            }
42
        }
43
44
        if (isset($resources['globals'])) {
45
            $this->globals = $resources['globals'];
46
        }
47
48
        $this->resources = $resources;
49
    }
50
51
    private function ensureValidConstraints($resources)
52
    {
53
        foreach ($resources['resources'] as $item) {
54
            if (isset($item['constraints'])) {
55
                foreach ($item['constraints'] as $name => $value) {
56
                    if (!in_array($name, $this->allowed)) {
57
                        throw new RuntimeException(
58
                            'Invalid constraint: '
59
                            . 'name ' . $name
60
                            . '; value ' . $value
61
                        );
62
                    }
63
                }
64
            }
65
        }
66
    }
67
68
    public function countResources()
69
    {
70
        return count(
71
            $this->resources['resources']
72
        );
73
    }
74
75
    public function create($resource, array $constraints)
76
    {
77
        foreach ($constraints as $name => $value) {
78 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...
79
                throw new RuntimeException(
80
                    'Not allowed `' . $name . '` constraint with value `' . $value . '`'
81
                );
82
            }
83
84 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...
85
                throw new RuntimeException(
86
                    'Not allowed `' . $name . '` constraint with value `' . $value . '`'
87
                );
88
            }
89
        }
90
    }
91
92
    public function allowed($resource)
93
    {
94
        $allowed = [];
95
96
        foreach ($this->allowed as $item) {
97 View Code Duplication
            if (isset($this->resources['resources'][$resource]['constraints'][$item])) {
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...
98
                $allowed = array_merge(
99
                    $allowed,
100
                    $this->resources['resources'][$resource]['constraints'][$item]
101
                );
102
            }
103
        }
104
105
        return $allowed;
106
    }
107
108
    public function getConstraints(
109
        $constraintName,
110
        $resource
111
    ) {
112 View Code Duplication
        if (isset($this->resources['resources'][$resource]['constraints'][$constraintName])) {
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...
113
            return $this->resources['resources'][$resource]['constraints'][$constraintName];
114
        }
115
116
        return [];
117
    }
118
119
    public function rewrites()
120
    {
121
        return $this->rewrites;
122
    }
123
124
    public function globals()
125
    {
126
        return $this->globals;
127
    }
128
}
129