TypedContainer   A
last analyzed

Complexity

Total Complexity 8

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 8
lcom 1
cbo 3
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 4 1
A get() 7 18 4
A has() 0 4 1
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\DataStructure\Result\Result;
11
use Fundic\Factory\ValueFactory;
12
13
final class TypedContainer implements Container
14
{
15
    /**
16
     * @var Dictionary
17
     */
18
    private $values = [];
19
20
    private function __construct(Dictionary $values)
21
    {
22
        $this->values = $values;
23
    }
24
25
    public static function create() : self
26
    {
27
        return new self(Dictionary::empty());
28
    }
29
30
    /**
31
     * Finds an entry of the container by its identifier and returns it.
32
     *
33
     * @param string $id Identifier of the entry to look for.
34
     *
35
     * @return Result
36
     */
37
    public function get($id) : Result
38
    {
39
        $maybeFactory = $this->values->get($id);
40
41
        $container = $this;
42
43
        try {
44 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...
45
                case ($maybeFactory instanceof Just):
46
                    /** @var Just $maybeFactory */
47
                    return Result::just(($maybeFactory->get())($container, $id));
48
                case ($maybeFactory instanceof Nothing):
49
                    return Result::notFound();
50
            }
51
        } catch (\Throwable $e) {
52
            return Result::exception($e);
53
        }
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function has($id) : bool
60
    {
61
        return $this->values->has($id);
62
    }
63
64
    public function add(string $id, ValueFactory $factory) : Container
65
    {
66
        return new self($this->values->add($id, $factory));
67
    }
68
}
69