Rule   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 13
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 131
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A test() 0 5 2
A methodMatches() 0 4 1
A argumentListMatches() 0 15 4
B comparePattern() 0 25 5
1
<?php
2
3
namespace EloquentJs\Query\Guard\Policy;
4
5
use EloquentJs\Query\MethodCall;
6
7
class Rule
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $method;
13
14
    /**
15
     * @var array
16
     */
17
    protected $arguments;
18
19
    /**
20
     * @var array
21
     */
22
    protected $sugarMap = [
23
        'select' => [
24
            'select', 'addSelect'
25
        ],
26
        'where' => [
27
            'where', 'orWhere',
28
            'whereBetween', 'orWhereBetween', 'whereNotBetween', 'orWhereNotBetween',
29
            'whereExists', 'orWhereExists', 'whereNotExists', 'orWhereNotExists',
30
            'whereIn', 'orWhereIn', 'whereNotIn', 'orWhereNotIn',
31
            'whereNull', 'orWhereNull', 'whereNotNull', 'orWhereNotNull',
32
            'whereDate', 'whereDay', 'whereMonth', 'whereYear',
33
        ],
34
        'having' => [
35
            'having', 'orHaving',
36
        ],
37
        'orderBy' => [
38
            'orderBy', 'latest', 'oldest',
39
        ],
40
        'offset' => [
41
            'offset', 'skip',
42
        ],
43
        'limit' => [
44
            'limit', 'take', 'forPage',
45
        ],
46
    ];
47
48
    /**
49
     * Create a new rule instance.
50
     *
51
     * @param string $method
52
     * @param array $arguments
53
     */
54
    public function __construct($method, array $arguments = [])
55
    {
56
        $this->method = $method;
57
        $this->arguments = $arguments;
58
    }
59
60
    /**
61
     * Test if the method call passes this rule.
62
     *
63
     * @param MethodCall $call
64
     * @return bool
65
     */
66
    public function test(MethodCall $call)
67
    {
68
        return $this->methodMatches($call->method)
69
            and $this->argumentListMatches($call->arguments);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
70
    }
71
72
    /**
73
     * Test if the given value matches the required method pattern.
74
     *
75
     * @param string $value
76
     * @return bool
77
     */
78
    protected function methodMatches($value)
79
    {
80
        return $this->comparePattern($this->method, $value);
81
    }
82
83
    /**
84
     * Test if the given arguments match the required arguments signature.
85
     *
86
     * @param array $arguments
87
     * @return bool
88
     */
89
    protected function argumentListMatches(array $arguments)
90
    {
91
        foreach ($arguments as $index => $argument) {
92
93
            if (empty($this->arguments[$index])) {
94
                continue;
95
            }
96
97
            if ( ! $this->comparePattern($this->arguments[$index], $argument)) {
98
                return false;
99
            }
100
        }
101
102
        return true;
103
    }
104
105
    /**
106
     * Compare the given value to the given pattern.
107
     *
108
     * @param string $pattern
109
     * @param string $value
110
     * @return bool
111
     */
112
    protected function comparePattern($pattern, $value)
113
    {
114
        if (is_callable($pattern)) {
115
            return $pattern($value);
116
        }
117
118
        $alternates = explode('|', $pattern);
119
120
        return ! ! array_first($alternates, function($index, $pattern) use ($value) {
121
122
            $firstCharacter = substr($pattern, 0, 1);
123
            $remainder = substr($pattern, 1);
124
125
            switch ($firstCharacter) {
126
                case '>':
127
                    return $remainder < $value;
128
                case '<':
129
                    return $remainder > $value;
130
                case '!':
131
                    return ! str_is($remainder, $value);
132
            }
133
134
            return str_is($pattern, $value);
135
        });
136
    }
137
}