NotEqualRule::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
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);
0 ignored issues
show
Unused Code introduced by
$operand is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
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=[])
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...
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