1
|
|
|
<?php |
2
|
|
|
namespace JClaveau\LogicalFilter\Rule; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* a != x |
6
|
|
|
*/ |
7
|
|
|
class NotEqualRule extends NotRule |
8
|
|
|
{ |
9
|
|
|
use Trait_RuleWithField; |
10
|
|
|
|
11
|
|
|
/** @var string operator */ |
12
|
|
|
const operator = '!='; |
13
|
|
|
|
14
|
|
|
/** @var mixed value */ |
15
|
|
|
protected $value; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string $field The field to apply the rule on. |
19
|
|
|
* @param mixed $value The value the field can equal to. |
20
|
|
|
*/ |
21
|
40 |
|
public function __construct( $field, $value, array $options=[] ) |
22
|
|
|
{ |
23
|
40 |
|
$this->field = $field; |
24
|
40 |
|
$this->value = $value; |
25
|
|
|
|
26
|
|
|
// TODO use the operand instead of properties or don't create EqualRule |
27
|
40 |
|
parent::__construct(new EqualRule($field, $value), $options); |
28
|
40 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
*/ |
32
|
23 |
|
public function isNormalizationAllowed(array $contextual_options=[]) |
33
|
|
|
{ |
34
|
23 |
|
$operand = $this->getOperandAt(0); |
|
|
|
|
35
|
|
|
|
36
|
23 |
|
$out = parent::isNormalizationAllowed() |
37
|
23 |
|
&& $this->getOption('not_equal.normalization', $contextual_options) |
38
|
23 |
|
; |
39
|
23 |
|
return $out; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
*/ |
44
|
40 |
|
public function getValue() |
45
|
|
|
{ |
46
|
40 |
|
return $this->value; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
*/ |
51
|
12 |
|
public function getValues() |
52
|
|
|
{ |
53
|
12 |
|
return $this->getValue(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array $options + show_instance=false Display the operator of the rule or its instance id |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
40 |
View Code Duplication |
public function toArray(array $options=[]) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$default_options = [ |
64
|
40 |
|
'show_instance' => false, |
65
|
40 |
|
]; |
66
|
40 |
|
foreach ($default_options as $default_option => &$default_value) { |
67
|
40 |
|
if ( ! isset($options[ $default_option ])) { |
68
|
40 |
|
$options[ $default_option ] = $default_value; |
69
|
40 |
|
} |
70
|
40 |
|
} |
71
|
|
|
|
72
|
40 |
|
if ( ! $options['show_instance'] && isset($this->cache['array'])) { |
73
|
34 |
|
return $this->cache['array']; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$array = [ |
77
|
40 |
|
$this->getField(), |
78
|
40 |
|
$options['show_instance'] ? $this->getInstanceId() : self::operator, |
79
|
40 |
|
$this->getValue(), |
80
|
40 |
|
]; |
81
|
|
|
|
82
|
40 |
|
if ( ! $options['show_instance']) { |
83
|
40 |
|
return $this->cache['array'] = $array; |
84
|
|
|
} |
85
|
|
|
else { |
86
|
|
|
return $array; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
*/ |
92
|
2 |
|
public function toString(array $options=[]) |
93
|
|
|
{ |
94
|
2 |
|
if ( ! empty($this->cache['string'])) { |
95
|
1 |
|
return $this->cache['string']; |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
$class = get_class($this); |
99
|
2 |
|
$operator = $class::operator; |
100
|
|
|
|
101
|
2 |
|
$stringified_value = var_export($this->getValues(), true); |
102
|
|
|
|
103
|
2 |
|
return $this->cache['string'] = "['{$this->getField()}', '$operator', $stringified_value]"; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* By default, every atomic rule can have a solution by itself |
108
|
|
|
* |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
13 |
|
public function hasSolution(array $simplification_options=[]) |
112
|
|
|
{ |
113
|
13 |
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/**/ |
117
|
|
|
} |
118
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.