Completed
Pull Request — master (#19)
by Flo
04:12
created

Link   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 14.75 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 0
cbo 6
dl 9
loc 61
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C __invoke() 9 48 8

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
 * Class Link | Link.php
4
 * @package Faulancer\View\Helper
5
 * @author  Florian Knapp <[email protected]>
6
 */
7
namespace Faulancer\View\Helper;
8
9
use Faulancer\Exception\RouteInvalidException;
10
use Faulancer\Http\Request;
11
use Faulancer\Service\Config;
12
use Faulancer\Service\RequestService;
13
use Faulancer\Translate\Translator;
14
use Faulancer\View\AbstractViewHelper;
15
use Faulancer\View\ViewController;
16
17
/**
18
 * Class Link
19
 */
20
class Link extends AbstractViewHelper
21
{
22
23
    /**
24
     * @param ViewController $view
25
     * @param string         $routeName
26
     * @param array          $elementAttributes
27
     * @return string
28
     * @throws RouteInvalidException
29
     * @codeCoverageIgnore
30
     */
31
    public function __invoke(ViewController $view, $routeName, $elementAttributes = [])
32
    {
33
        $id    = '';
34
        $class = '';
35
        $style = '';
36
37
        $serviceLocator = $this->getServiceLocator();
38
39
        /** @var Config $config */
40
        $config = $serviceLocator->get(Config::class);
41
42
        /** @var Request $request */
43
        $request = $serviceLocator->get(RequestService::class);
44
45
        $route = $config->get('routes:' . $routeName);
46
47
        if (empty($route)) {
48
            throw new RouteInvalidException('No valid route for "' . $routeName . '" found.');
49
        }
50
51
        if ($request->getUri() === $route['path']) {
52
            $elementAttributes['class'][] = 'selected';
53
        }
54
55 View Code Duplication
        if (!empty($elementAttributes['id'])) {
0 ignored issues
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...
56
            $id = ' id="' . implode(' ', $elementAttributes['id']) . '" ';
57
        }
58
59 View Code Duplication
        if (!empty($elementAttributes['class'])){
0 ignored issues
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...
60
            $class = 'class="' . implode(' ', $elementAttributes['class']) . '" ';
61
        }
62
63 View Code Duplication
        if (!empty($elementAttributes['style'])) {
0 ignored issues
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...
64
            $style = 'style="' . implode(' ', $elementAttributes['style']) . '" ';
65
        }
66
67
        $linkPattern = '<a ' . $id . $class . $style . 'href="%s" onfocus="blur()">%s</a>';
68
69
        if (!empty($route['i18n_key']) && !empty($route['title'])) {
70
            $translator = new Translator();
71
            $route['title'] = $translator->translate($route['i18n_key']);
72
        }
73
74
        $link = sprintf($linkPattern, $route['path'], $route['title']);
75
76
        return $link;
77
78
    }
79
80
}