Completed
Push — master ( 21f0c6...bca4fe )
by Bogdan
02:21
created

AliasContainerConfigurator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 46
Duplicated Lines 34.78 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 16
loc 46
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B configure() 16 16 5
A configureOne() 0 8 3

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
  +----------------------------------------------------------------------+
5
  | This file is part of the pinepain/container-extras PHP library.      |
6
  |                                                                      |
7
  | Copyright (c) 2015-2016 Bogdan Padalko <[email protected]>          |
8
  |                                                                      |
9
  | Licensed under the MIT license: http://opensource.org/licenses/MIT   |
10
  |                                                                      |
11
  | For the full copyright and license information, please view the      |
12
  | LICENSE file that was distributed with this source or visit          |
13
  | http://opensource.org/licenses/MIT                                   |
14
  +----------------------------------------------------------------------+
15
*/
16
17
18
namespace Pinepain\Container\Extras;
19
20
21
use League\Container\ContainerInterface;
22
use League\Container\Definition\ClassDefinition;
23
use League\Container\Definition\DefinitionInterface;
24
use Pinepain\Container\Extras\Exceptions\InvalidConfigException;
25
use Traversable;
26
27
28
class AliasContainerConfigurator implements ContainerConfiguratorInterface
29
{
30
    /**
31
     * @var AliasContainerInterface
32
     */
33
    private $container;
34
35
    /**
36
     * @param AliasContainerInterface $container
37
     */
38 8
    public function __construct(AliasContainerInterface $container)
39
    {
40 8
        $this->container = $container;
41 8
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 5 View Code Duplication
    public function configure($config)
47
    {
48 5
        if (!is_array($config) && !($config instanceof Traversable)) {
49 1
            throw new InvalidConfigException(
50
                'You can only load definitions from an array or an object that implements Traversable interface.'
51 1
            );
52
        }
53 4
        foreach ($config as $alias => $concrete) {
54
55 2
            if (!is_string($concrete)) {
56 1
                continue;
57
            }
58
59 2
            $this->configureOne($alias, $concrete);
60 4
        }
61 4
    }
62
    /**
63
     * {@inheritdoc}
64
     */
65 5
    public function configureOne($alias, $concrete)
66
    {
67 5
        if (!is_string($alias) || !is_string($concrete)) {
68 3
            throw new InvalidConfigException('Alias and concrete should be strings');
69
        }
70
71 2
        $this->container->add($alias, $concrete);
72 2
    }
73
}
74