InValidator   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 49
Duplicated Lines 6.12 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 3
loc 49
ccs 0
cts 39
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setValidSet() 0 13 4
C validate() 3 26 13

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 declare(strict_types=1);
2
3
namespace indigerd\scenarios\validation\validator;
4
5
class InValidator extends AbstractValidator
6
{
7
    protected $validSet = [];
8
9
    protected $notIn = false;
10
11
    protected $message = 'Value do not belongs to valid set';
12
13
    public function setValidSet($value) : self
14
    {
15
        if (!\is_array($value)
16
            and !($value instanceof \Closure)
17
            and !($value instanceof \Traversable)
18
        ) {
19
            throw new \InvalidArgumentException(
20
                'Invalid validSet property value. Value must be either array or \Closure or \Traversable'
21
            );
22
        }
23
        $this->validSet = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value can also be of type object<Closure> or object<Traversable>. However, the property $validSet is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
24
        return $this;
25
    }
26
27
    public function validate($value, array $context = []) : bool
28
    {
29 View Code Duplication
        if ($this->skipOnEmpty and ($value === null || $value === [] || $value === '')) {
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...
30
            return true;
31
        }
32
        if ($this->validSet instanceof \Closure) {
33
            $this->validSet = $this->validSet();
0 ignored issues
show
Bug introduced by
The method validSet() does not exist on indigerd\scenarios\valid...n\validator\InValidator. Did you maybe mean setValidSet()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
34
            if (!\is_array($this->validSet) and !($this->validSet instanceof \Traversable)) {
35
                throw new \InvalidArgumentException(
36
                    'Invalid validSet callback.'
37
                );
38
            }
39
        }
40
        $in = false;
41
        if ($this->validSet instanceof \Traversable) {
42
            foreach ($this->validSet as $v) {
43
                if ($value == $v) {
44
                    $in = true;
45
                }
46
            }
47
        }
48
        if (\is_array($this->validSet)) {
49
            $in = \in_array($value, $this->validSet);
50
        }
51
        return $this->notIn !== $in ? true : false;
52
    }
53
}
54