Passed
Branch phpunit-bridge (ebf7df)
by Pascal
08:31
created

ArrayLoader   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 84
Duplicated Lines 35.71 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 30
loc 84
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A load() 15 15 4
B supports() 15 15 5
A getResolver() 0 4 1
A setResolver() 0 4 1
A getLoader() 0 8 2

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
namespace Skalpa\Silex\Symfony\Routing\Loader;
4
5
use Pimple\Container;
6
use Symfony\Component\Config\Loader\LoaderInterface;
7
use Symfony\Component\Config\Loader\LoaderResolverInterface;
8
use Symfony\Component\Routing\RouteCollection;
9
10
/**
11
 * Lazy loader that can load multiple resources.
12
 *
13
 * Array of resources can be:
14
 * - Associative arrays with resource path as key and resource type as value
15
 * - Numerical arrays with resource path as value
16
 */
17
class ArrayLoader implements LoaderInterface
18
{
19
    private $container;
20
    private $serviceName;
21
    /**
22
     * @var LoaderInterface
23
     */
24
    private $loader;
25
26
    /**
27
     * ArrayLoader constructor.
28
     *
29
     * @param Container $container
30
     * @param string    $serviceName
31
     */
32 17
    public function __construct(Container $container, $serviceName)
33
    {
34 17
        $this->container = $container;
35 17
        $this->serviceName = $serviceName;
36 17
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 10 View Code Duplication
    public function load($resource, $type = null)
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...
42
    {
43 10
        if (!is_array($resource)) {
44 9
            return $this->getLoader()->load($resource, $type);
45
        }
46
47 1
        $loader = $this->getLoader();
48 1
        $collection = new RouteCollection();
49
50 1
        foreach ($resource as $key => $value) {
51 1
            $collection->addCollection(is_int($key) ? $loader->load($value) : $loader->load($key, $value));
52 1
        }
53
54 1
        return $collection;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 3 View Code Duplication
    public function supports($resource, $type = null)
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...
61
    {
62 3
        if (!is_array($resource)) {
63 1
            return $this->getLoader()->supports($resource, $type);
64
        }
65
66 2
        $loader = $this->getLoader();
67 2
        $supported = true;
68
69 2
        foreach ($resource as $key => $value) {
70 3
            $supported = $supported && (is_int($key) ? $loader->supports($value) : $loader->supports($key, $value));
71 2
        }
72
73 2
        return $supported;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 1
    public function getResolver()
80
    {
81 1
        return $this->getLoader()->getResolver();
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 1
    public function setResolver(LoaderResolverInterface $resolver)
88
    {
89 1
        return $this->getLoader()->setResolver($resolver);
90
    }
91
92 15
    protected function getLoader()
93
    {
94 15
        if (null === $this->loader) {
95 15
            $this->loader = $this->container[$this->serviceName];
96 15
        }
97
98 15
        return $this->loader;
99
    }
100
}
101