Passed
Pull Request — master (#288)
by Alexander
10:50 queued 07:32
created

BeforeValidationTrait::initSkipOnEmptyProperties()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5.4042

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 16
ccs 5
cts 9
cp 0.5556
rs 10
cc 4
nc 4
nop 2
crap 5.4042
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule\Trait;
6
7
use Closure;
8
use InvalidArgumentException;
9
use Yiisoft\Validator\SkipOnAll;
10
use Yiisoft\Validator\SkipOnNull;
11
use Yiisoft\Validator\ValidationContext;
12
13
use function is_callable;
14
15
trait BeforeValidationTrait
16
{
17
    public function getSkipOnEmpty(): bool
18
    {
19
        return $this->skipOnEmpty;
20
    }
21
22 37
    public function shouldSkipOnEmpty($value): bool
23
    {
24 37
        return ($this->skipOnEmptyCallback)($value);
25
    }
26
27 88
    protected function initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback): void
28
    {
29 88
        $this->skipOnEmpty = $skipOnEmpty;
0 ignored issues
show
Bug Best Practice introduced by
The property skipOnEmpty does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
30 88
        $this->skipOnEmptyCallback = $skipOnEmptyCallback;
0 ignored issues
show
Bug Best Practice introduced by
The property skipOnEmptyCallback does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
32 88
        if ($this->skipOnEmptyCallback) {
33
            if (!is_callable($this->skipOnEmptyCallback)) {
34
                throw new InvalidArgumentException('$skipOnEmptyCallback must be a callable.');
35
            }
36
37
            $this->skipOnEmpty = true;
38
39
            return;
40
        }
41
42 88
        $this->skipOnEmptyCallback = $this->skipOnEmpty === false ? new SkipOnAll() : new SkipOnNull();
43
    }
44
45
    public function skipOnEmpty(bool $value): self
46
    {
47
        $new = clone $this;
48
        $new->skipOnEmpty = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property skipOnEmpty does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
49
50
        return $new;
51
    }
52
53
    public function skipOnEmptyCallback(callable $value): self
54
    {
55
        $new = clone $this;
56
        $new->skipOnEmptyCallback = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property skipOnEmptyCallback does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
57
58
        return $new;
59
    }
60
61 34
    public function shouldSkipOnError(): bool
62
    {
63 34
        return $this->skipOnError;
0 ignored issues
show
Bug introduced by
The property skipOnError does not exist on Yiisoft\Validator\Rule\Trait\BeforeValidationTrait. Did you mean skipOnEmpty?
Loading history...
64
    }
65
66
    /**
67
     * @psalm-return Closure(mixed, ValidationContext):bool|null
68
     *
69
     * @return Closure|null
70
     */
71 33
    public function getWhen(): ?Closure
72
    {
73 33
        return $this->when;
74
    }
75
}
76