Completed
Push — master ( b12741...03b4a1 )
by Kirill
01:38
created

TypeHint::matchIterable()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A TypeHint::match() 0 4 1
1
<?php
2
/**
3
 * This file is part of Properties package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Serafim\Properties\Attribute;
11
12
/**
13
 * Class TypeHint
14
 */
15
abstract class TypeHint implements HintInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    private $name;
21
22
    /**
23
     * @var Matchable[]
24
     */
25
    private $matchers = [];
26
27
    /**
28
     * TypeHint constructor.
29
     * @param string $name
30
     */
31
    public function __construct(string $name)
32
    {
33
        $this->name = $name;
34
    }
35
36
    /**
37
     * @return string
38
     */
39
    public function getName(): string
40
    {
41
        return $this->name;
42
    }
43
44
    /**
45
     * @param Matchable $matcher
46
     * @return Disjunction
47
     */
48
    public function addMatcher(Matchable $matcher): Matchable
49
    {
50
        $this->matchers[] = $matcher;
51
52
        return $this;
53
    }
54
55
    /**
56
     * @param string $name
57
     * @param mixed $value
58
     * @return bool
59
     */
60
    protected function matchScalar(string $name, $value): bool
61
    {
62
        switch (true) {
63
            case $this->isBuiltin($name):
64
                return $this->matchBuiltin($name, $value);
65
66
            case $name === 'mixed':
67
                return true;
68
69
            case $name === 'null':
70
            case $name === 'void':
71
                return $value === null;
72
73
            case \class_exists($name):
74
                return $value instanceof $name;
75
76
            default:
77
                @\trigger_error('Unrecognized type-hint "' . $name . '"');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
78
                return true;
79
        }
80
    }
81
82
    /**
83
     * @param mixed $value
84
     * @return bool
85
     */
86
    public function match($value): bool
87
    {
88
        return $this->matchScalar($this->name, $value);
89
    }
90
91
    /**
92
     * @param string $name
93
     * @return bool
94
     */
95
    private function isBuiltin(string $name): bool
96
    {
97
        return \function_exists('\\is_' . $name);
98
    }
99
100
    /**
101
     * @param string $name
102
     * @param mixed $value
103
     * @return bool
104
     */
105
    private function matchBuiltin(string $name, $value): bool
106
    {
107
        $function = '\\is_' . $name;
108
109
        return $function($value);
110
    }
111
}
112