Trait_RuleWithField::setField()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.243

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 16
ccs 7
cts 10
cp 0.7
crap 3.243
rs 9.7333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Trait_RuleWithField
4
 *
5
 * @package php-logical-filter
6
 * @author  Jean Claveau
7
 */
8
namespace JClaveau\LogicalFilter\Rule;
9
10
trait Trait_RuleWithField
11
{
12
    /** @var string $field The field to apply the rule on */
13
    protected $field;
14
15
    /**
16
     * @return string $field
17
     */
18 301
    final public function getField()
19
    {
20 301
        return $this->field;
21
    }
22
23
    /**
24
     * @return string $field
0 ignored issues
show
Documentation introduced by
Should the return type not be Trait_RuleWithField?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
25
     */
26 9
    final public function setField( $field )
27
    {
28 9
        if ( ! is_scalar($field)) {
29
            throw new \InvalidArgumentEXception(
30
                "\$field property of a logical rule must be a scalar contrary to: "
31
                .var_export($field, true)
32
            );
33
        }
34
35 9
        if ($this->field != $field) {
36 4
            $this->field = $field;
37 4
            $this->flushCache();
0 ignored issues
show
Bug introduced by
It seems like flushCache() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
38 4
        }
39
40 9
        return $this;
41
    }
42
43
    /**
44
     * Changes the field property of the rule.
45
     *
46
     * @param  array|callable Associative array of renamings or callable
47
     *                        that would rename the fields.
48
     *
49
     * @return AbstractAtomicRule $this
0 ignored issues
show
Documentation introduced by
Should the return type not be Trait_RuleWithField?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
50
     */
51
    final public function renameField($renamings)
52
    {
53
        $this->renameFields_andReturnIsChanged($renamings);
54
        return $this;
55
    }
56
57
    /**
58
     * Changes the field property of the rule.
59
     *
60
     * @param  array|callable Associative array of renamings or callable
61
     *                        that would rename the fields.
62
     *
63
     * @return bool Whether or not the field changed
64
     */
65 1
    final public function renameFields_andReturnIsChanged($renamings)
66
    {
67 1
        $old_field = $this->field;
68
69 1
        if (is_callable($renamings)) {
70 1
            $this->setField( call_user_func($renamings, $this->field) );
71 1
        }
72 1
        elseif (is_array($renamings)) {
73 1
            if (isset($renamings[$this->field])) {
74 1
                $this->setField( $renamings[$this->field] );
75 1
            }
76 1
        }
77
        else {
78 1
            throw new \InvalidArgumentException(
79
                "\$renamings MUST be a callable or an associative array "
80 1
                ."instead of: " . var_export($renamings, true)
81 1
            );
82
        }
83
84 1
        if ($old_field != $this->field) {
85 1
            $this->flushCache();
0 ignored issues
show
Bug introduced by
It seems like flushCache() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
86 1
            return true;
87
        }
88
89
        // TODO remove this forced cache flushing ONLY when carefully
90
        // unit tested
91 1
        $this->flushCache();
0 ignored issues
show
Bug introduced by
It seems like flushCache() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
92 1
        return false;
93
    }
94
95
    /**/
96
}
97