Completed
Push — master ( a421dc...071248 )
by Eric
05:45
created

Registry   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
Duplicated Lines 17.02 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 3
dl 16
loc 94
ccs 33
cts 33
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A offsetExists() 0 4 1
A offsetGet() 8 8 2
A offsetSet() 0 17 4
A offsetUnset() 8 8 2
A count() 0 4 1
A getIterator() 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
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Component\Registry\Model;
13
14
use Lug\Component\Registry\Exception\InvalidServiceException;
15
use Lug\Component\Registry\Exception\ServiceAlreadyExistsException;
16
use Lug\Component\Registry\Exception\ServiceNotFoundException;
17
18
/**
19
 * @author GeLo <[email protected]>
20
 */
21
class Registry implements RegistryInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    private $interface;
27
28
    /**
29
     * @var object[]
30
     */
31
    private $services = [];
32
33
    /**
34
     * @param string   $interface
35
     * @param object[] $services
36
     */
37 12
    public function __construct($interface, array $services = [])
38
    {
39 12
        $this->interface = $interface;
40
41 12
        foreach ($services as $type => $service) {
42 12
            $this->offsetSet($type, $service);
43 12
        }
44 12
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 12
    public function offsetExists($type)
50
    {
51 12
        return array_key_exists($type, $this->services);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 3 View Code Duplication
    public function offsetGet($type)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
58
    {
59 3
        if (!$this->offsetExists($type)) {
60 1
            throw new ServiceNotFoundException(sprintf('The service "%s" could not be found.', $type));
61
        }
62
63 2
        return $this->services[$type];
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 12
    public function offsetSet($type, $service)
70
    {
71 12
        if ($this->offsetExists($type)) {
72 1
            throw new ServiceAlreadyExistsException(sprintf('The service "%s" already exists.', $type));
73
        }
74
75 12
        if (!$service instanceof $this->interface) {
76 2
            throw new InvalidServiceException(sprintf(
77 2
                'The service for the registry "%s" must be an instance of "%s", got "%s".',
78 2
                get_class($this),
79 2
                $this->interface,
80 2
                is_object($service) ? get_class($service) : gettype($service)
81 2
            ));
82
        }
83
84 12
        $this->services[$type] = $service;
85 12
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 2 View Code Duplication
    public function offsetUnset($type)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
91
    {
92 2
        if (!$this->offsetExists($type)) {
93 1
            throw new ServiceNotFoundException(sprintf('The service "%s" could not be found.', $type));
94
        }
95
96 1
        unset($this->services[$type]);
97 1
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 4
    public function count()
103
    {
104 4
        return count($this->services);
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 4
    public function getIterator()
111
    {
112 4
        return new \ArrayIterator($this->services);
113
    }
114
}
115