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

NotValidator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 51
ccs 16
cts 16
cp 1
rs 10
c 1
b 0
f 1
wmc 9
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 5 2
A test() 0 4 1
B assert() 0 15 5
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