Completed
Push — master ( d2e239...75a482 )
by Jean
03:27
created

NotInRule::setPossibilities()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 8.125

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 1
dl 0
loc 26
ccs 9
cts 18
cp 0.5
crap 8.125
rs 9.1928
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 26
    public function __construct( $field, $possibilities )
17
    {
18 26
        $this->addOperand(new InRule($field, $possibilities));
19 26
    }
20
21
    /**
22
     */
23 14
    public function isNormalizationAllowed(array $contextual_options=[])
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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 25
    public function getField()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
36
    {
37 25
        if (! $this->getOperandAt(0)) {
38
            // TODO a NotRule with no operand should be simplified as
39
            //      a TrueRule
40 1
            throw new \LogicException(
41
                "Trying to get the field of an InRule negation missing its operand"
42 1
            );
43
        }
44
45 24
        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 2
    public function setField($field)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
51
    {
52 2
        if (! $this->getOperandAt(0)) {
53
            // TODO a NotRule with no operand should be simplified as
54
            //      a TrueRule
55 1
            throw new \LogicException(
56
                "Trying to set the field of an InRule negation missing its operand"
57 1
            );
58
        }
59
60 1
        return $this->getOperandAt(0)->setField($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 setField() 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\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 23
    public function getPossibilities()
67
    {
68 23
        if ($this->getOperandAt(0) instanceof EqualRule) {
69
            // In can be simplified in =
70
            return [$this->getOperandAt(0)->getValue()];
71
        }
72
73 23
        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
0 ignored issues
show
Documentation introduced by
Should the return type not be null|InRule?

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