Test Failed
Push — master ( 45bb0c...c4c0d6 )
by Jean
03:30
created

AbstractAtomicRule::toString()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 4
nop 1
dl 0
loc 20
rs 9.5222
c 0
b 0
f 0
1
<?php
2
namespace JClaveau\LogicalFilter\Rule;
3
use       JClaveau\LogicalFilter\FilteredValue;
4
use       JClaveau\LogicalFilter\FilteredKey;
5
6
/**
7
 * Atomic rules are those who cannot be simplified (so already are):
8
 * + null
9
 * + not null
10
 * + equal
11
 * + above
12
 * + below
13
 * Atomic rules are related to a field.
14
 */
15
abstract class AbstractAtomicRule extends AbstractRule
16
{
17
    use Trait_RuleWithField;
18
19
    /**
20
     * @param array $options   + show_instance=false Display the operator of the rule or its instance id
21
     *
22
     * @return array
23
     */
24
    public function toArray(array $options=[])
25
    {
26
        $default_options = [
27
            'show_instance' => false,
28
        ];
29
        foreach ($default_options as $default_option => &$default_value) {
30
            if (!isset($options[ $default_option ]))
31
                $options[ $default_option ] = $default_value;
32
        }
33
34
35
        if (!$options['show_instance'] && !empty($this->cache['array']))
36
            return $this->cache['array'];
37
38
        $class = get_class($this);
39
40
        $array = [
41
            $this->getField(),
42
            $options['show_instance'] ? $this->getInstanceId() : $class::operator,
43
            $this->getValues(),
0 ignored issues
show
Bug introduced by
The method getValues() does not exist on JClaveau\LogicalFilter\Rule\AbstractAtomicRule. Since it exists in all sub-types, consider adding an abstract or default implementation to JClaveau\LogicalFilter\Rule\AbstractAtomicRule. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
            $this->/** @scrutinizer ignore-call */ 
44
                   getValues(),
Loading history...
44
        ];
45
46
        if (!$options['show_instance'])
47
            return $this->cache['array'] = $array;
48
        else
49
            return $array;
50
    }
51
52
    /**
53
     */
54
    public function toString(array $options=[])
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

54
    public function toString(/** @scrutinizer ignore-unused */ array $options=[])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56
        if (!empty($this->cache['string']))
57
            return $this->cache['string'];
58
59
        $class = get_class($this);
60
        $operator = $class::operator;
61
62
        $stringified_value = var_export($this->getValues(), true);
63
64
        $field = $this->getField();
65
66
        if ($field instanceof FilteredValue || $field instanceof FilteredKey)
0 ignored issues
show
introduced by
$field is never a sub-type of JClaveau\LogicalFilter\FilteredKey.
Loading history...
67
            $field = "$field";
68
        elseif ($field instanceof \Closure)
0 ignored issues
show
introduced by
$field is never a sub-type of Closure.
Loading history...
69
            throw new \Exception("Closures dump not implemented");
70
        else
71
            $field = "'$field'";
72
73
        return $this->cache['string'] = "[$field, '$operator', $stringified_value]";
74
    }
75
76
    /**/
77
}
78