Passed
Push — master ( fabbdf...3d1055 )
by Aleksei
06:01 queued 12s
created

Binder::hasBinding()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core\Internal;
6
7
use Psr\Container\ContainerInterface;
8
use Spiral\Core\Internal\Common\DestructorTrait;
9
use Spiral\Core\Internal\Common\Registry;
10
use Spiral\Core\Internal\Config\StateBinder;
11
12
/**
13
 * @internal
14
 */
15
final class Binder extends StateBinder
16
{
17
    use DestructorTrait;
18
19
    private ContainerInterface $container;
20
    private Scope $scope;
21
22 1463
    public function __construct(Registry $constructor)
23
    {
24 1463
        $constructor->set('binder', $this);
25
26 1463
        $this->container = $constructor->get('container', ContainerInterface::class);
27 1463
        $this->scope = $constructor->get('scope', Scope::class);
28
29 1463
        parent::__construct($constructor->get('state', State::class));
30
    }
31
32 17
    public function hasInstance(string $alias): bool
33
    {
34 17
        $parent = $this->scope->getParent();
35 17
        if ($parent !== null && $parent->hasInstance($alias)) {
36 3
            return true;
37
        }
38
39 17
        if (!$this->container->has($alias)) {
40 3
            return false;
41
        }
42
43 16
        return parent::hasInstance($alias);
44
    }
45
46 101
    public function hasBinding(string $alias): bool
47
    {
48 101
        return parent::hasBinding($alias) || ($this->scope->getParent()?->hasBinding($alias) ?? false);
49
    }
50
51 610
    public function destruct(): void
52
    {
53 610
        unset($this->container);
54
    }
55
}
56