Passed
Pull Request — master (#300)
by Sergei
02:36
created

SkipOnEmptyNormalizer::normalize()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 15
ccs 7
cts 8
cp 0.875
rs 10
cc 4
nc 4
nop 1
crap 4.0312
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use InvalidArgumentException;
8
use Yiisoft\Validator\SkipOnEmptyCallback\SkipOnEmpty;
9
10
use function is_callable;
11
12
/**
13
 * @internal
14
 */
15
final class SkipOnEmptyNormalizer
16
{
17 3
    public static function normalize(mixed $skipOnEmpty): ?callable
18
    {
19 3
        if ($skipOnEmpty === false) {
20 1
            return null;
21
        }
22
23 2
        if ($skipOnEmpty === true) {
24 1
            return new SkipOnEmpty();
25
        }
26
27 1
        if (!is_callable($skipOnEmpty)) {
28
            throw new InvalidArgumentException('$skipOnEmpty must be a boolean or a callable.');
29
        }
30
31 1
        return $skipOnEmpty;
32
    }
33
}
34