Test Failed
Pull Request — master (#37)
by Divine Niiquaye
11:41
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\{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 (!isset($this->types[$cl = static::class])) {
32
            $this->type(self::SERVICE_CONTAINER, \array_keys(\class_implements($c = $this) + \class_parents($c) + [$cl => $cl]));
33
        }
34
        $this->resolver = new Resolver($this->services[self::SERVICE_CONTAINER] = $c ?? $this);
35
    }
36
37
    /**
38
     * Sets a new service to a unique identifier.
39
     *
40
     * @param string $offset The unique identifier for the parameter or object
41
     * @param mixed  $value  The value of the service assign to the $offset
42
     *
43
     * @throws FrozenServiceException Prevent override of a frozen service
44
     */
45
    public function offsetSet($offset, $value): void
46
    {
47
        $this->autowire($offset, $value);
48
    }
49
50
    /**
51
     * Gets a registered service definition.
52
     *
53
     * @param string $offset The unique identifier for the service
54
     *
55
     * @throws NotFoundServiceException If the identifier is not defined
56
     *
57 106
     * @return mixed The value of the service
58
     */
59 106
    #[\ReturnTypeWillChange]
60 106
    public function offsetGet($offset)
61 106
    {
62
        return $this->get($offset);
63
    }
64
65
    /**
66
     * Checks if a service is set.
67
     *
68
     * @param string $offset The unique identifier for the service
69
     */
70
    public function offsetExists($offset): bool
71 63
    {
72
        return $this->has($offset);
73 63
    }
74 63
75
    /**
76
     * Unset a service by given offset.
77
     *
78
     * @param string $offset The unique identifier for service definition
79
     */
80
    public function offsetUnset($offset): void
81
    {
82
        $this->removeDefinition($offset);
83
    }
84
85 55
    /**
86
     * {@inheritdoc}
87 55
     *
88
     * @throws \ReflectionException
89
     */
90
    protected function doCreate(string $id, $definition, int $invalidBehavior)
91
    {
92
        if ($definition instanceof Definitions\DefinitionInterface) {
93
            $service = $definition->build($id, $this->resolver);
94
95 7
            if ($definition instanceof Definitions\ShareableDefinitionInterface) {
96
                if (!$definition->isPublic()) {
97 7
                    $this->removeDefinition($id);
98
                }
99
100
                if (!$definition->isShared()) {
101
                    return $service;
102
                }
103
            }
104
        }
105 6
106
        return $this->services[$id] = $service ?? $definition;
107 6
    }
108
}
109