NotEqualRule   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 111
Duplicated Lines 25.23 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.56%

Importance

Changes 0
Metric Value
dl 28
loc 111
ccs 40
cts 41
cp 0.9756
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A isNormalizationAllowed() 0 9 2
A getValue() 0 4 1
A getValues() 0 4 1
B toArray() 28 28 7
A toString() 0 13 2
A hasSolution() 0 4 1

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
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