InflectorAggregate::inflect()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\Container\Inflector;
6
7
use Generator;
8
use League\Container\ContainerAwareTrait;
9
10
class InflectorAggregate implements InflectorAggregateInterface
11
{
12
    use ContainerAwareTrait;
13
14
    /**
15
     * @var Inflector[]
16
     */
17
    protected $inflectors = [];
18
19 12
    public function add(string $type, ?callable $callback = null): Inflector
20
    {
21 12
        $inflector = new Inflector($type, $callback);
22 12
        $this->inflectors[] = $inflector;
23 12
        return $inflector;
24
    }
25
26 42
    public function inflect($object)
27
    {
28 42
        foreach ($this->getIterator() as $inflector) {
29 6
            $type = $inflector->getType();
30
31 6
            if ($object instanceof $type) {
32 6
                $inflector->setContainer($this->getContainer());
33 6
                $inflector->inflect($object);
34
            }
35
        }
36
37 42
        return $object;
38
    }
39
40 45
    public function getIterator(): Generator
41
    {
42 45
        yield from $this->inflectors;
43
    }
44
}
45