TypeInstanceOf   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
c 2
b 0
f 0
dl 0
loc 32
ccs 6
cts 6
cp 1
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 13 6
A type() 0 5 1
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