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

GenericTypeHint::match()   B

Complexity

Conditions 10
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 7.6666
c 0
b 0
f 0
cc 10
nc 6
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 GenericTypeHint
14
 */
15
class GenericTypeHint extends TypeHint
16
{
17
    /**
18
     * @var null|Matchable
19
     */
20
    private $key;
21
22
    /**
23
     * @var null|Matchable
24
     */
25
    private $value;
26
27
    /**
28
     * GenericTypeHint constructor.
29
     * @param string $name
30
     * @param Matchable|null $key
31
     * @param Matchable|null $value
32
     */
33
    public function __construct(string $name, Matchable $value = null, Matchable $key = null)
34
    {
35
        $this->key = $key;
36
        $this->value = $value;
37
38
        parent::__construct($name);
39
    }
40
41
    /**
42
     * @param iterable|mixed $values
43
     * @return bool
44
     */
45
    public function match($values): bool
46
    {
47
        if (! \is_iterable($values)) {
48
            return false;
49
        }
50
51
        if (! parent::match($values)) {
52
            return false;
53
        }
54
55
        if ($this->value || $this->key) {
56
            foreach ($values as $key => $value) {
57
                if ($this->key && ! $this->key->match($key)) {
58
                    return false;
59
                }
60
61
                if ($this->value && ! $this->value->match($value)) {
62
                    return false;
63
                }
64
            }
65
        }
66
67
        return true;
68
    }
69
}
70