Resource   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 106
Duplicated Lines 16.98 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 94.29%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 18
loc 106
ccs 33
cts 35
cp 0.9429
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getResources() 0 4 1
B __construct() 18 18 5
A getResourcesCompiled() 0 11 2
A matches() 0 15 3
A mapVariables() 0 8 2
A replaceVariables() 0 4 1
A replaceWildcard() 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 tomzx\PolicyEvaluator;
4
5
class Resource
6
{
7
    public static $prefix = 'urn';
8
9
    /**
10
     * @var array
11
     */
12
    private $resources;
13
14
    /**
15
     * @param array|string $resources
16
     */
17 24 View Code Duplication
    public function __construct($resources)
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...
18
    {
19 24
        if ( ! is_array($resources)) {
20 23
            $resources = (array)$resources;
21
        }
22
23 24
        foreach ($resources as $resource) {
24 24
            if ($resource === '*') {
25 2
                continue;
26
            }
27
28 22
            if (strpos($resource, self::$prefix . ':') !== 0) {
29 22
                throw new \InvalidArgumentException('Invalid resource "' . $resource . '".');
30
            }
31
        }
32
33 23
        $this->resources = $resources;
34 23
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function getResources()
40
    {
41
        return $this->resources;
42
    }
43
44
    /**
45
     * @return array
46
     */
47 17
    public function getResourcesCompiled(array $variables = [])
48
    {
49 17
        $variableStrings = $this->mapVariables($variables);
50 17
        $resources = [];
51
52 17
        foreach ($this->resources as $resource) {
53 17
            $resources[] = $this->replaceVariables($resource, $variableStrings);
54
        }
55
56 17
        return $resources;
57
    }
58
59
    /**
60
     * @param string $requestedResource
61
     * @return bool
62
     */
63 17
    public function matches($requestedResource, array $variables = [])
64
    {
65 17
        $resources = $this->getResourcesCompiled($variables);
66
67 17
        foreach ($resources as $resource) {
68 17
            $preparedResource = $this->replaceWildcard($resource);
69 17
            $resourceRegex    = '/^'.$preparedResource.'$/';
70
71 17
            if (preg_match($resourceRegex, $requestedResource)) {
72 17
                return true;
73
            }
74
        }
75
76 7
        return false;
77
    }
78
79
    /**
80
     * @param array $variables
81
     * @return array
82
     */
83 17
    private function mapVariables(array $variables)
84
    {
85 17
        $mappedVariables = [];
86 17
        foreach ($variables as $key => $value) {
87 3
            $mappedVariables['${'.$key.'}'] = $value;
88
        }
89 17
        return $mappedVariables;
90
    }
91
92
    /**
93
     * @param string $resource
94
     * @param array $variables
95
     * @return string
96
     */
97 17
    private function replaceVariables($resource, array $variables)
98
    {
99 17
        return strtr($resource, $variables);
100
    }
101
102
    /**
103
     * @param string $resource
104
     * @return string
105
     */
106 17
    private function replaceWildcard($resource)
107
    {
108 17
        return str_replace('\*', '.*', preg_quote($resource, '/'));
109
    }
110
}
111