Passed
Push — master ( b674ac...2bc77e )
by Kirill
04:21
created

Composite::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Spiral Framework package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Attributes\Composite;
13
14
use Spiral\Attributes\Reader;
15
use Spiral\Attributes\ReaderInterface;
16
17
abstract class Composite extends Reader
18
{
19
    /**
20
     * @var ReaderInterface[]
21
     */
22
    protected $readers;
23
24
    /**
25
     * @param ReaderInterface[] $readers
26
     */
27
    public function __construct(iterable $readers)
28
    {
29
        $this->readers = $this->iterableToArray($readers);
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35
    public function getClassMetadata(\ReflectionClass $class, string $name = null): iterable
36
    {
37
        return $this->each(static function (ReaderInterface $reader) use ($class, $name): iterable {
38
            return $reader->getClassMetadata($class, $name);
39
        });
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    public function getFunctionMetadata(\ReflectionFunctionAbstract $function, string $name = null): iterable
46
    {
47
        return $this->each(static function (ReaderInterface $reader) use ($function, $name): iterable {
48
            return $reader->getFunctionMetadata($function, $name);
49
        });
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function getPropertyMetadata(\ReflectionProperty $property, string $name = null): iterable
56
    {
57
        return $this->each(static function (ReaderInterface $reader) use ($property, $name): iterable {
58
            return $reader->getPropertyMetadata($property, $name);
59
        });
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    public function getConstantMetadata(\ReflectionClassConstant $constant, string $name = null): iterable
66
    {
67
        return $this->each(static function (ReaderInterface $reader) use ($constant, $name): iterable {
68
            return $reader->getConstantMetadata($constant, $name);
69
        });
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function getParameterMetadata(\ReflectionParameter $parameter, string $name = null): iterable
76
    {
77
        return $this->each(static function (ReaderInterface $reader) use ($parameter, $name): iterable {
78
            return $reader->getParameterMetadata($parameter, $name);
79
        });
80
    }
81
82
83
    /**
84
     * @param callable(ReaderInterface): list<array-key, object> $resolver
85
     * @return iterable
86
     */
87
    abstract protected function each(callable $resolver): iterable;
88
89
    /**
90
     * @param \Traversable|array $result
91
     * @return array
92
     */
93
    protected function iterableToArray(iterable $result): array
94
    {
95
        return $result instanceof \Traversable ? \iterator_to_array($result, false) : $result;
96
    }
97
}
98