Completed
Push — master ( 872ac5...951b37 )
by Andres
03:04 queued 13s
created

ExecutorTrait::validate()   D

Complexity

Conditions 9
Paths 33

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 20
c 1
b 0
f 0
nc 33
nop 2
dl 0
loc 33
rs 4.909
1
<?php
2
namespace Comfort\Validator\Helper;
3
4
use Comfort\Exception\ValidationException;
5
use Comfort\ValidationError;
6
7
trait ExecutorTrait
8
{
9
    /**
10
     * Execute the validation stack and fail on first
11
     *
12
     * @param $value
13
     * @param null $key
14
     * @return bool|ValidationError|null
15
     */
16
    protected function validate($value, $key = null)
17
    {
18
        if (is_null($value) && $this->optional) {
0 ignored issues
show
Bug introduced by
The property optional 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...
19
            if (is_null($this->defaultValue)) {
20
                return null;
21
            } else {
22
                $value = $this->defaultValue;
0 ignored issues
show
Bug introduced by
The property defaultValue 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...
23
            }
24
        }
25
26
        try {
27
            reset($this->validationStack);
0 ignored issues
show
Bug introduced by
The property validationStack 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...
28
29
            do {
30
                /** @var callable $validator */
31
                $validator = current($this->validationStack);
32
                $retVal = $validator($value, $key);
33
                $value = $retVal === null ? $value : $retVal;
34
            } while (next($this->validationStack));
35
36
            if ($this->toBool) {
0 ignored issues
show
Bug introduced by
The property toBool 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...
37
                return true;
38
            }
39
40
            return $value;
41
        } catch (ValidationException $validationException) {
42
            if ($this->toBool) {
43
                return false;
44
            }
45
46
            return ValidationError::fromException($validationException);
47
        }
48
    }
49
}