Completed
Branch master (458557)
by T
02:07
created

Resource   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 32.73 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 18
loc 55
ccs 19
cts 21
cp 0.9048
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 18 18 5
A getResources() 0 4 1
A matches() 0 11 3

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 20 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 20
        if ( ! is_array($resources)) {
20 19
            $resources = (array)$resources;
21 19
        }
22
23 20
        foreach ($resources as $resource) {
24 20
            if ($resource === '*') {
25 2
                continue;
26
            }
27
28 18
            if (strpos($resource, self::$prefix . ':') !== 0) {
29 1
                throw new \InvalidArgumentException('Invalid resource "' . $resource . '".');
30
            }
31 19
        }
32
33 19
        $this->resources = $resources;
34 19
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function getResources()
40
    {
41
        return $this->resources;
42
    }
43
44
    /**
45
     * @param string $requestedResource
46
     * @return bool
47
     */
48 13
    public function matches($requestedResource)
49
    {
50 13
        foreach ($this->resources as $resource) {
51 13
            $resourceRegex = '/^'.str_replace('\*', '.*', preg_quote($resource, '/')).'$/';
52 13
            if (preg_match($resourceRegex, $requestedResource)) {
53 7
                return true;
54
            }
55 6
        }
56
57 6
        return false;
58
    }
59
}
60