Passed
Branch master (2a417c)
by Jean
05:00
created

NotInRule::setPossibilities()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 27

Duplication

Lines 6
Ratio 22.22 %

Code Coverage

Tests 8
CRAP Score 12.9841

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 1
dl 6
loc 27
ccs 8
cts 19
cp 0.4211
crap 12.9841
rs 8.8657
c 0
b 0
f 0
1
<?php
2
namespace JClaveau\LogicalFilter\Rule;
3
4
/**
5
 * a ! in x
6
 */
7
class NotInRule extends NotRule
8
{
9
    /** @var string operator */
10
    const operator = '!in';
11
12
    /**
13
     * @param string $field The field to apply the rule on.
14
     * @param array  $value The value the field can equal to.
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
15
     */
16 15
    public function __construct( $field, $possibilities )
17
    {
18 15
        $this->addOperand(new InRule($field, $possibilities));
19 15
    }
20
21
    /**
22
     */
23 14
    public function isNormalizationAllowed(array $contextual_options=[])
24
    {
25 14
        $operand = $this->getOperandAt(0);
26 14
        if ( ! $operand->getPossibilities()) {
27 1
            return false;
28
        }
29
30 13
        return $operand->isNormalizationAllowed($contextual_options);
31
    }
32
33
    /**
34
     */
35 16
    public function getField()
36
    {
37 16
        if ( ! $this->getOperandAt(0)) {
38
            // TODO a NotRule with no operand should be simplified as
39
            //      a TrueRule
40
            throw new \LogicException(
41
                "Trying to get the field of a negation missing its operand"
42
            );
43
        }
44
45 16
        return $this->getOperandAt(0)->getField();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class JClaveau\LogicalFilter\Rule\AbstractRule as the method getField() does only exist in the following sub-classes of JClaveau\LogicalFilter\Rule\AbstractRule: JClaveau\LogicalFilter\Rule\AboveOrEqualRule, JClaveau\LogicalFilter\Rule\AboveRule, JClaveau\LogicalFilter\Rule\AbstractAtomicRule, JClaveau\LogicalFilter\Rule\BelowOrEqualRule, JClaveau\LogicalFilter\Rule\BelowRule, JClaveau\LogicalFilter\Rule\BetweenOrEqualBothRule, JClaveau\LogicalFilter\R...BetweenOrEqualLowerRule, JClaveau\LogicalFilter\R...BetweenOrEqualUpperRule, JClaveau\LogicalFilter\Rule\BetweenRule, JClaveau\LogicalFilter\Rule\EqualRule, JClaveau\LogicalFilter\Rule\InRule, JClaveau\LogicalFilter\Rule\NotEqualRule, JClaveau\LogicalFilter\Rule\NotInRule, JClaveau\LogicalFilter\Rule\RegexpRule. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
46
    }
47
48
    /**
49
     */
50
    public function setField($field)
51
    {
52
        if ( ! $this->getOperandAt(0)) {
53
            // TODO a NotRule with no operand should be simplified as
54
            //      a TrueRule
55
            throw new \LogicException(
56
                "Trying to get the field of a negation missing its operand"
57
            );
58
        }
59
60
        return $this->getOperandAt(0)->getField($field);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class JClaveau\LogicalFilter\Rule\AbstractRule as the method getField() does only exist in the following sub-classes of JClaveau\LogicalFilter\Rule\AbstractRule: JClaveau\LogicalFilter\Rule\AboveOrEqualRule, JClaveau\LogicalFilter\Rule\AboveRule, JClaveau\LogicalFilter\Rule\AbstractAtomicRule, JClaveau\LogicalFilter\Rule\BelowOrEqualRule, JClaveau\LogicalFilter\Rule\BelowRule, JClaveau\LogicalFilter\Rule\BetweenOrEqualBothRule, JClaveau\LogicalFilter\R...BetweenOrEqualLowerRule, JClaveau\LogicalFilter\R...BetweenOrEqualUpperRule, JClaveau\LogicalFilter\Rule\BetweenRule, JClaveau\LogicalFilter\Rule\EqualRule, JClaveau\LogicalFilter\Rule\InRule, JClaveau\LogicalFilter\Rule\NotEqualRule, JClaveau\LogicalFilter\Rule\NotInRule, JClaveau\LogicalFilter\Rule\RegexpRule. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
61
    }
62
63
    /**
64
     * @return array
65
     */
66 16
    public function getPossibilities()
67
    {
68 16
        if ($this->getOperandAt(0) instanceof EqualRule) {
69
            // In can be simplified in =
70
            return [$this->getOperandAt(0)->getValue()];
71
        }
72
73 16
        return $this->getOperandAt(0)->getPossibilities();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class JClaveau\LogicalFilter\Rule\AbstractRule as the method getPossibilities() does only exist in the following sub-classes of JClaveau\LogicalFilter\Rule\AbstractRule: JClaveau\LogicalFilter\Rule\InRule, JClaveau\LogicalFilter\Rule\NotInRule. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
74
    }
75
76
    /**
77
     * @return $this
78
     */
79 5
    public function setPossibilities($possibilities)
80
    {
81 5 View Code Duplication
        if (    is_object($possibilities)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
82 5
            && $possibilities instanceof \IteratorAggregate
83 5
            && method_exists($possibilities, 'toArray')
84 5
        ) {
85
            $possibilities = $possibilities->toArray();
86
        }
87
88 5
        if ($this->getOperandAt(0) instanceof EqualRule) {
89
            // TODO this case should occure anymore while a NotInRule
90
            // may not have the same class once simplified
91
            $possibilities[] = $this->getOperandAt(0)->getValue();
92
93
            $operands = [
94
                new InRule(
95
                    $this->getOperandAt(0)->getField(),
96
                    array_unique($possibilities)
97
                ),
98
            ];
99
100
            $this->setOperands($operands);
101
        }
102 5
        elseif ($this->getOperandAt(0) instanceof InRule) {
103 5
            return $this->getOperandAt(0)->setPossibilities($possibilities);
104
        }
105
    }
106
107
    /**
108
     */
109 3
    public function getValues()
110
    {
111 3
        return $this->getPossibilities();
112
    }
113
114
    /**
115
     */
116 2
    public function hasSolution(array $contextual_options=[])
117
    {
118 2
        return true;
119
    }
120
121
    /**
122
     * @param array $options   + show_instance=false Display the operator of the rule or its instance id
123
     *
124
     * @return array
125
     */
126 16
    public function toArray(array $options=[])
127
    {
128
        $default_options = [
129 16
            'show_instance' => false,
130 16
        ];
131 16
        foreach ($default_options as $default_option => &$default_value) {
132 16
            if ( ! isset($options[ $default_option ])) {
133 16
                $options[ $default_option ] = $default_value;
134 16
            }
135 16
        }
136
137
        try {
138
            return [
139 16
                $this->getField(),
140 16
                $options['show_instance'] ? $this->getInstanceId() : self::operator,
141 16
                $this->getPossibilities(),
142 16
            ];
143
        }
144
        catch (\LogicException $e) {
145
            return parent::toArray();
146
        }
147
    }
148
149
    /**
150
     * @todo cache support
151
     */
152 1 View Code Duplication
    public function toString(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...
153
    {
154
        try {
155 1
            $operator = self::operator;
156
157 1
            $stringified_possibilities = '[' . implode(', ', array_map(function($possibility) {
158 1
                return var_export($possibility, true);
159 1
            }, $this->getPossibilities()) ) .']';
160
161 1
            return "['{$this->getField()}', '$operator', $stringified_possibilities]";
162
        }
163
        catch (\LogicException $e) {
164
            return parent::toString();
165
        }
166
    }
167
168
    /**/
169
}
170