Link   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 13.64 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 6
dl 9
loc 66
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 9 51 9

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 Faulancer\View\Helper;
4
5
use Faulancer\Exception\RouteInvalidException;
6
use Faulancer\Http\Request;
7
use Faulancer\Service\Config;
8
use Faulancer\Translate\Translator;
9
use Faulancer\View\AbstractViewHelper;
10
11
/**
12
 * Class Link
13
 *
14
 * @package Faulancer\View\Helper
15
 * @author  Florian Knapp <[email protected]>
16
 */
17
class Link extends AbstractViewHelper
18
{
19
20
    /**
21
     * Render a ready-to-use link within an 'a' tag
22
     *
23
     * @param string $routeName
24
     * @param array  $elementAttributes
25
     * @param array  $parameter
26
     * @return string
27
     *
28
     * @throws RouteInvalidException
29
     */
30
    public function __invoke($routeName, $elementAttributes = [], $parameter = [])
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(Request::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->getPath() === $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
        if (!empty($route['i18n_key']) && !empty($route['title'])) {
69
            $translator = new Translator();
70
            $route['title'] = $translator->translate($route['i18n_key']);
71
        }
72
73
        $path = $route['path'];
74
75
        if (strpos($path, '(') !== false) {
76
            $path = $this->view->route($routeName, $parameter);
0 ignored issues
show
Documentation Bug introduced by
The method route does not exist on object<Faulancer\View\ViewController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
77
        }
78
79
        return sprintf($linkPattern, $path, $route['title']);
80
    }
81
82
}