InfiniteRecursionDetector::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cekta\DI;
6
7
use Cekta\DI\Exception\InfiniteRecursion;
8
use Psr\Container\ContainerInterface;
9
10
class InfiniteRecursionDetector implements ContainerInterface
11
{
12
    /**
13
     * @var ContainerInterface
14
     */
15
    private $container;
16
    /**
17
     * @var string[]
18
     */
19
    private $calls = [];
20
21 9
    public function __construct(ContainerInterface $container)
22
    {
23 9
        $this->container = $container;
24 9
    }
25
26 6
    public function get($id)
27
    {
28 6
        if (in_array($id, $this->calls)) {
29 3
            throw new InfiniteRecursion($id, $this->calls);
30
        }
31 6
        $this->calls[] = $id;
32 6
        $result = $this->container->get($id);
33 3
        array_pop($this->calls);
34 3
        return $result;
35
    }
36
37 3
    public function has($id): bool
38
    {
39 3
        return $this->container->has($id);
40
    }
41
}
42