Test Failed
Pull Request — master (#37)
by Divine Niiquaye
02:59
created

Container::doGet()   C

Complexity

Conditions 12
Paths 11

Size

Total Lines 44
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 44
ccs 23
cts 23
cp 1
rs 6.9666
cc 12
nc 11
nop 2
crap 12

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2021 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Rade\DI;
19
20
use Rade\DI\Exceptions\{ContainerResolutionException, FrozenServiceException, NotFoundServiceException};
21
22
/**
23
 * Dependency injection container.
24
 *
25
 * @author Divine Niiquaye Ibok <[email protected]>
26
 */
27
class Container extends AbstractContainer implements \ArrayAccess
28
{
29
    public function __construct()
30
    {
31
        if (empty($this->types)) {
32
            $this->services[self::SERVICE_CONTAINER] = $c = $this;
33
            $this->type(self::SERVICE_CONTAINER, \array_keys(\class_implements($c) + \class_parents($c) + [static::class => static::class]));
34
        }
35
        $this->resolver = new Resolver($s ?? $this);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $s seems to never exist and therefore isset should always be false.
Loading history...
36
    }
37
38
    /**
39
     * Sets a new service to a unique identifier.
40
     *
41
     * @param string $offset The unique identifier for the parameter or object
42
     * @param mixed  $value  The value of the service assign to the $offset
43
     *
44
     * @throws FrozenServiceException Prevent override of a frozen service
45
     */
46
    public function offsetSet($offset, $value): void
47
    {
48
        $this->autowire($offset, $value);
49
    }
50
51
    /**
52
     * Gets a registered service definition.
53
     *
54
     * @param string $offset The unique identifier for the service
55
     *
56
     * @throws NotFoundServiceException If the identifier is not defined
57 106
     *
58
     * @return mixed The value of the service
59 106
     */
60 106
    #[\ReturnTypeWillChange]
61 106
    public function offsetGet($offset)
62
    {
63
        return $this->get($offset);
64
    }
65
66
    /**
67
     * Checks if a service is set.
68
     *
69
     * @param string $offset The unique identifier for the service
70
     */
71 63
    public function offsetExists($offset): bool
72
    {
73 63
        return $this->has($offset);
74 63
    }
75
76
    /**
77
     * Unset a service by given offset.
78
     *
79
     * @param string $offset The unique identifier for service definition
80
     */
81
    public function offsetUnset($offset): void
82
    {
83
        $this->removeDefinition($offset);
84
    }
85 55
86
    /**
87 55
     * {@inheritdoc}
88
     *
89
     * @throws FrozenServiceException if definition has been initialized
90
     */
91
    public function definition(string $id)
92
    {
93
        if (\array_key_exists($id, $this->privates)) {
94
            throw new FrozenServiceException(\sprintf('The "%s" internal service is meant to be private and out of reach.', $id));
95 7
        }
96
97 7
        return parent::definition($id);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     *
103
     * @throws \ReflectionException
104
     */
105 6
    protected function doCreate(string $id, object $definition, int $invalidBehavior)
106
    {
107 6
        if ($definition instanceof Definitions\DefinitionInterface) {
108 6
            if ($definition instanceof Definitions\ShareableDefinitionInterface) {
109
                if ($definition->isAbstract()) {
110
                    throw new ContainerResolutionException(\sprintf('Resolving an abstract definition %s is not allowed.', $id));
111
                }
112
113
                if (!$definition->isPublic()) {
114
                    $this->removeDefinition($id);
115
                }
116 17
117
                if ($definition->isShared()) {
118 17
                    $s = &$this->services[$id] ?? null;
119
                }
120
            }
121
            $s = $definition->build($id, $this->resolver);
122
        }
123
124
        return $this->definitions[$id] = ($this->services[$id] ??= $s ?? $definition);
125
    }
126
}
127