Failed Conditions
Pull Request — 4.0 (#4528)
by Kentaro
09:58
created

IgnoreRoutingNotFoundExtension   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 47.62 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 30
loc 63
ccs 0
cts 10
cp 0
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getFunctions() 0 7 1
A getPath() 15 15 3
A getUrl() 15 15 3
A isUrlGenerationSafe() 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
namespace Eccube\Twig\Extension;
5
6
7
use Symfony\Bridge\Twig\Extension\RoutingExtension;
8
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
9
use Symfony\Component\Routing\Exception\RouteNotFoundException;
10
use Twig\Extension\AbstractExtension;
11
use Twig\Node\Node;
12
use Twig\TwigFunction;
13
14
class IgnoreRoutingNotFoundExtension extends AbstractExtension
15
{
16
    /**
17
     * @var RoutingExtension
18
     */
19
    private $routingExtension;
20
21
    /**
22
     * @var ContainerBagInterface
23
     */
24
    private $containerBag;
25
26
    public function __construct(RoutingExtension $extension, ContainerBagInterface $containerBag)
27
    {
28
        $this->routingExtension = $extension;
29
        $this->containerBag = $containerBag;
30
    }
31
32
    public function getFunctions(): array
33
    {
34
        return [
35
            new TwigFunction('url', [$this, 'getUrl'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
36
            new TwigFunction('path', [$this, 'getPath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
37
        ];
38
    }
39
40 View Code Duplication
    public function getPath(string $name, array $parameters = [], bool $relative = false): string
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...
41
    {
42
        if ($this->containerBag->get('kernel.debug')) {
43
            return $this->routingExtension->getPath($name, $parameters, $relative);
44
        }
45
46
        try {
47
            return $this->routingExtension->getPath($name, $parameters, $relative);
48
        } catch (RouteNotFoundException $e) {
49
            // FIXME log関数でエラーになるので, 修正されたらコメントアウトをはずす
50
            // log_warning($e->getMessage(), ['exception' => $e]);
51
52
            return $this->routingExtension->getPath('homepage') . '404?bind=' . $name;
53
        }
54
    }
55
56 View Code Duplication
    public function getUrl(string $name, array $parameters = [], bool $schemeRelative = false): string
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...
57
    {
58
        if ($this->containerBag->get('kernel.debug')) {
59
            return $this->routingExtension->getUrl($name, $parameters, $schemeRelative);
60
        }
61
62
        try {
63
            return $this->routingExtension->getUrl($name, $parameters, $schemeRelative);
64
        } catch (RouteNotFoundException $e) {
65
            // FIXME log関数でエラーになるので, 修正されたらコメントアウトをはずす
66
            // log_warning($e->getMessage(), ['exception' => $e]);
67
68
            return $this->routingExtension->getUrl('homepage', $parameters, $schemeRelative) . '404?bind=' . $name;
69
        }
70
    }
71
72
    public function isUrlGenerationSafe(Node $argsNode): array
73
    {
74
        return $this->routingExtension->isUrlGenerationSafe($argsNode);
75
    }
76
}
77