GenericTypeHint   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 55
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B match() 0 24 10
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
    protected $key;
21
22
    /**
23
     * @var null|Matchable
24
     */
25
    protected $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