Test Failed
Pull Request — master (#19)
by Flo
02:19
created

Link   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 16.07 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 9 43 6

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\View\AbstractViewHelper;
14
use Faulancer\View\ViewController;
15
16
/**
17
 * Class Link
18
 */
19
class Link extends AbstractViewHelper
20
{
21
22
    /**
23
     * @param ViewController $view
24
     * @param string         $routeName
25
     * @param array          $elementAttributes
26
     * @return string
27
     * @throws RouteInvalidException
28
     * @codeCoverageIgnore
29
     */
30
    public function __invoke(ViewController $view, $routeName, $elementAttributes = [])
31
    {
32
        $id    = '';
33
        $class = '';
34
        $style = '';
35
36
        $serviceLocator = $this->getServiceLocator();
37
38
        /** @var Config $config */
39
        $config = $serviceLocator->get(Config::class);
40
41
        /** @var Request $request */
42
        $request = $serviceLocator->get(RequestService::class);
43
44
        $route = $config->get('routes:' . $routeName);
45
46
        if (empty($route)) {
47
            throw new RouteInvalidException('No valid route for "' . $routeName . '" found.');
48
        }
49
50
        if ($request->getUri() === $route['path']) {
51
            $elementAttributes['class'][] = 'selected';
52
        }
53
54 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...
55
            $id = ' id="' . implode(' ', $elementAttributes['id']) . '" ';
56
        }
57
58 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...
59
            $class = 'class="' . implode(' ', $elementAttributes['class']) . '" ';
60
        }
61
62 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...
63
            $style = 'style="' . implode(' ', $elementAttributes['style']) . '" ';
64
        }
65
66
        $linkPattern = '<a ' . $id . $class . $style . 'href="%s" onfocus="blur()">%s</a>';
67
68
        $link = sprintf($linkPattern, $route['path'], $route['title']);
69
70
        return $link;
71
72
    }
73
74
}