TypeHintReducer::match()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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\Reducers;
11
12
use Railt\Parser\Ast\RuleInterface;
13
use Serafim\Properties\Attribute\Matchable;
14
use Serafim\Properties\Attribute\TypeHint;
15
16
/**
17
 * Class TypeHintReducer
18
 */
19
class TypeHintReducer implements ReducerInterface
20
{
21
    /**
22
     * @param RuleInterface $rule
23
     * @return bool
24
     */
25
    public function match(RuleInterface $rule): bool
26
    {
27
        return $rule->getName() === 'TypeHint';
28
    }
29
30
    /**
31
     * @param RuleInterface $rule
32
     * @return \Generator|TypeHint
33
     */
34
    public function reduce(RuleInterface $rule): \Generator
35
    {
36
        $hints = [];
37
38
        foreach ($rule->getChildren() as $child) {
39
            $hints[] = yield $child;
40
        }
41
42
        return $this->pack($hints);
43
    }
44
45
    /**
46
     * @param array|Matchable[] $hints
47
     * @return mixed
48
     */
49
    private function pack(array $hints)
50
    {
51
        if (\count($hints) > 1) {
52
            [$child, $parent] = $hints;
0 ignored issues
show
Bug introduced by
The variable $child does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $parent does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
53
54
            $parent->addMatcher($child);
55
56
            return $parent;
57
        }
58
59
        return $hints[0];
60
    }
61
}
62