Completed
Push — master ( c30481...9edbca )
by Marco
01:21
created

Psr11Container   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 7
loc 56
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 4 1
A has() 0 4 1
B get() 7 22 5
A add() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Fundic;
6
7
use Fundic\DataStructure\Dictionary;
8
use Fundic\DataStructure\Maybe\Just;
9
use Fundic\DataStructure\Maybe\Nothing;
10
use Fundic\Exception\ContainerException;
11
use Fundic\Exception\NotFoundException;
12
use Fundic\Factory\ValueFactory;
13
use Psr\Container\NotFoundExceptionInterface;
14
15
final class Psr11Container implements Container
16
{
17
    /**
18
     * @var Dictionary
19
     */
20
    private $values = [];
21
22
    private function __construct(Dictionary $values)
23
    {
24
        $this->values = $values;
25
    }
26
27
    public static function create() : self
28
    {
29
        return new self(Dictionary::empty());
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function has($id) : bool
36
    {
37
        return $this->values->has($id);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function get($id)
44
    {
45
        $maybeFactory = $this->values->get($id);
46
47
        $container = $this;
48
49
        try {
50 View Code Duplication
            switch (true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
                case ($maybeFactory instanceof Just):
52
                    /** @var Just $maybeFactory */
53
                    return ($maybeFactory->get())($container, $id);
54
                case ($maybeFactory instanceof Nothing):
55
                    throw NotFoundException::forKey($id);
56
            }
57
        } catch (\Throwable $e) {
58
            if ($e instanceof NotFoundExceptionInterface) {
59
                throw $e;
60
            }
61
62
            throw ContainerException::forKeyWithInner($id, $e);
63
        }
64
    }
65
66
    public function add(string $id, ValueFactory $factory) : Container
67
    {
68
        return new self($this->values->add($id, $factory));
69
    }
70
}
71