Completed
Pull Request — master (#126)
by Phil
19:36 queued 04:37
created

InflectorAggregate::getIterator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php declare(strict_types=1);
2
3
namespace League\Container\Inflector;
4
5
use Generator;
6
use League\Container\ContainerAwareTrait;
7
8
class InflectorAggregate implements InflectorAggregateInterface
9
{
10
    use ContainerAwareTrait;
11
12
    /**
13
     * @var \League\Container\Inflector[]
14
     */
15
    protected $inflectors = [];
16
17
    /**
18
     * {@inheritdoc}
19 9
     */
20
    public function add(string $type, callable $callback = null): Inflector
21 9
    {
22 6
        $inflector          = new Inflector($type, $callback);
23 6
        $this->inflectors[] = $inflector;
24
25 6
        return $inflector;
26
    }
27
28 3
    /**
29 3
     * {@inheritdoc}
30
     */
31
    public function getIterator(): Generator
32
    {
33
        $count = count($this->inflectors);
34 48
35
        for ($i = 0; $i < $count; $i++) {
36 48
            yield $this->inflectors[$i];
37 6
        }
38 3
    }
39
40
    /**
41 6
     * {@inheritdoc}
42 3
     */
43 3
    public function inflect($object)
44 3
    {
45
        foreach ($this->getIterator() as $inflector) {
46
            if (! is_subclass_of($object, $inflector->getType())) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $inflector->getType() can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
47
                continue;
48 3
            }
49 32
50
            $inflector->setContainer($this->getContainer());
51 48
            $inflector->inflect($object);
52
        }
53
54
        return $object;
55
    }
56
}
57