InfiniteRecursionDetector   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 30
ccs 12
cts 12
cp 1
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A has() 0 3 1
A get() 0 9 2
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