TypeInstanceOf::check()   A
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 6
c 2
b 0
f 0
nc 10
nop 1
dl 0
loc 13
ccs 4
cts 4
cp 1
crap 6
rs 9.2222
1
<?php
2
3
/**
4
 * This file is part of Dimtrovich/Validation.
5
 *
6
 * (c) 2023 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Dimtrovich\Validation\Rules;
13
14
class TypeInstanceOf extends AbstractRule
15
{
16
    protected const NAME = 'instance_of';
17
18
    protected $fillableParams = ['type'];
19
20
    /**
21
     * Modifie le type de l'enumeration
22
     */
23
    public function type(string $type): self
24
    {
25 2
        $this->params['type'] = $type;
26
27 2
        return $this;
28
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    public function check($value): bool
34
    {
35 2
        $this->requireParameters($this->fillableParams);
36
37
        if (is_object($type = $this->parameter('type'))) {
38 2
            $type = get_class($type);
39
        }
40
41
        if (is_object($value)) {
42 2
            return $value instanceof $type || get_class($value) === $type;
43
        }
44
45 2
        return is_subclass_of($value, $type) || is_a($value, $type) || $value === $type;
46
    }
47
}
48