Completed
Push — master ( 5df011...747e00 )
by Changwan
03:50
created

NotValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 1
1
<?php
2
namespace Wandu\Validator\Rules;
3
4
use Wandu\Validator\Contracts\ValidatorInterface;
5
use Wandu\Validator\Exception\InvalidValueException;
6
7
class NotValidator extends ValidatorAbstract
8
{
9
    const ERROR_TYPE = 'not';
10
11
    /** @var \Wandu\Validator\Contracts\ValidatorInterface */
12
    protected $next;
13
14
    /**
15
     * @param \Wandu\Validator\Contracts\ValidatorInterface $next
16
     */
17 4
    public function __construct(ValidatorInterface $next)
18
    {
19 4
        $this->next = $next;
20 4
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    function test($item)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
26
    {
27
        // nothing
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 2
    public function assert($item)
34
    {
35 2
        if (!isset($item)) return;
36
        try {
37 2
            $this->next->assert($item);
38 2
        } catch (InvalidValueException $exception) {
39 2
            return;
40
        }
41 2
        $errorType = ValidatorAbstract::ERROR_TYPE;
42 2
        if ($this->next instanceof ValidatorAbstract) {
43 2
            $errorType = $this->next->getErrorType();
44
        }
45 2
        $suffix = isset($this->name) ? '@' . $this->name : '';
0 ignored issues
show
Bug introduced by
The property name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46 2
        throw new InvalidValueException('not.' . $errorType . $suffix);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 1
    public function validate($item)
53
    {
54 1
        if (!isset($item)) return true;
55 1
        return !$this->next->validate($item);
56
    }
57
}
58