Action   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 33.96 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 18
loc 53
ccs 16
cts 18
cp 0.8889
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getActions() 0 4 1
B __construct() 18 18 5
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 Action
6
{
7
    /**
8
     * @var array
9
     */
10
    private $actions;
11
12
    /**
13
     * @param array|string $actions
14
     */
15 23 View Code Duplication
    public function __construct($actions)
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...
16
    {
17 23
        if ( ! is_array($actions)) {
18 21
            $actions = (array)$actions;
19
        }
20
21 23
        foreach ($actions as $action) {
22 23
            if ($action === '*') {
23 2
                continue;
24
            }
25
26 21
            if (strpos($action, ':') === false) {
27 21
                throw new \InvalidArgumentException('Invalid service prefix for action "' . $action . '".');
28
            }
29
        }
30
31 21
        $this->actions = $actions;
32 21
    }
33
34
    /**
35
     * @return array
36
     */
37
    public function getActions()
38
    {
39
        return $this->actions;
40
    }
41
42
    /**
43
     * @param string $requestedAction
44
     * @return bool
45
     */
46 16
    public function matches($requestedAction)
47
    {
48 16
        foreach ($this->actions as $action) {
49 16
            $actionRegex = '/^'.str_replace('\*', '.*', preg_quote($action, '/')).'$/';
50 16
            if (preg_match($actionRegex, $requestedAction)) {
51 16
                return true;
52
            }
53
        }
54
55 6
        return false;
56
    }
57
}
58