Link::__invoke()   B
last analyzed

Complexity

Conditions 9
Paths 65

Size

Total Lines 51

Duplication

Lines 9
Ratio 17.65 %

Importance

Changes 0
Metric Value
dl 9
loc 51
rs 7.5135
c 0
b 0
f 0
cc 9
nc 65
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
}