InfiniteRecursionDetector::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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