ChainUrlGenerator   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 5.17 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 3
loc 58
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 3 11 4
A setContext() 0 7 2
A getContext() 0 4 1
A generate() 0 14 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
namespace Skalpa\Silex\Symfony\Routing;
4
5
use Symfony\Component\Routing\Exception\RouteNotFoundException;
6
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
7
use Symfony\Component\Routing\RequestContext;
8
9
10
/**
11
 * Generates URLs using other generators.
12
 */
13
class ChainUrlGenerator implements UrlGeneratorInterface
14
{
15
    private $context;
16
    private $generators;
17
18
    /**
19
     * @param UrlGeneratorInterface[] $generators
20
     * @param RequestContext          $context
21
     */
22 9
    public function __construct(array $generators, RequestContext $context)
23
    {
24 9
        foreach ($generators as $generator) {
25 9 View Code Duplication
            if (!$generator instanceof UrlGeneratorInterface) {
1 ignored issue
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...
26 1
                throw new \InvalidArgumentException(sprintf('Invalid URL generator. Expected an instance of %s, got %s.', UrlGeneratorInterface::class, is_object($generator) ? get_class($generator) : gettype($generator)));
27
            }
28 8
            $generator->setContext($context);
29 8
        }
30 8
        $this->generators = $generators;
31 8
        $this->context = $context;
32 8
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 1
    public function setContext(RequestContext $context)
38
    {
39 1
        foreach ($this->generators as $generator) {
40 1
            $generator->setContext($context);
41 1
        }
42 1
        $this->context = $context;
43 1
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 2
    public function getContext()
49
    {
50 2
        return $this->context;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 6
    public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
57
    {
58
        /** @var RouteNotFoundException $notFound */
59 6
        $notFound = null;
60
61 6
        foreach ($this->generators as $generator) {
62
            try {
63 6
                return $generator->generate($name, $parameters, $referenceType);
64 3
            } catch (RouteNotFoundException $e) {
65 3
                $notFound = $e;
66
            }
67 3
        }
68 1
        throw $notFound;
69
    }
70
}
71