These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\MenuBundle\Service; |
||
4 | |||
5 | use Kunstmaan\MenuBundle\Entity\MenuItem; |
||
6 | use Symfony\Component\Routing\RouterInterface; |
||
7 | use Twig\Environment; |
||
8 | |||
9 | class RenderService |
||
10 | { |
||
11 | /** |
||
12 | * @var RouterInterface |
||
13 | */ |
||
14 | private $router; |
||
15 | |||
16 | public function __construct(RouterInterface $router) |
||
17 | { |
||
18 | $this->router = $router; |
||
19 | } |
||
20 | |||
21 | /** |
||
22 | * @param $node |
||
23 | * @param array $options |
||
24 | * |
||
25 | * @return string |
||
26 | */ |
||
27 | public function renderMenuItemTemplate(Environment $environment, $node, $options = []) |
||
28 | { |
||
29 | $template = isset($options['template']) ? $options['template'] : false; |
||
30 | if ($template === false) { |
||
31 | $template = '@KunstmaanMenu/menu-item.html.twig'; |
||
32 | } |
||
33 | |||
34 | $hasActiveChild = false; |
||
35 | View Code Duplication | if ($node['__children']) { |
|
36 | foreach ($node['__children'] as $childNode) { |
||
37 | if ($childNode['type'] == MenuItem::TYPE_PAGE_LINK) { |
||
38 | $childUrl = $this->router->generate('_slug', ['url' => $childNode['nodeTranslation']['url']]); |
||
39 | if ($this->router->getContext()->getPathInfo() == $childUrl) { |
||
40 | $hasActiveChild = true; |
||
41 | |||
42 | break; |
||
43 | } |
||
44 | } |
||
45 | } |
||
46 | } |
||
47 | |||
48 | $active = false; |
||
49 | View Code Duplication | if ($node['type'] == MenuItem::TYPE_PAGE_LINK) { |
|
0 ignored issues
–
show
|
|||
50 | $url = $this->router->generate('_slug', ['url' => $node['nodeTranslation']['url']]); |
||
51 | |||
52 | if ($this->router->getContext()->getPathInfo() == $url) { |
||
53 | $active = true; |
||
54 | } |
||
55 | } else { |
||
56 | $url = $node['url']; |
||
57 | } |
||
58 | |||
59 | if ($node['type'] == MenuItem::TYPE_PAGE_LINK) { |
||
60 | if ($node['title']) { |
||
61 | $title = $node['title']; |
||
62 | } else { |
||
63 | $title = $node['nodeTranslation']['title']; |
||
64 | } |
||
65 | } else { |
||
66 | $title = $node['title']; |
||
67 | } |
||
68 | |||
69 | return $environment->render($template, [ |
||
70 | 'menuItem' => $node, |
||
71 | 'url' => $url, |
||
72 | 'options' => $options, |
||
73 | 'title' => $title, |
||
74 | 'active' => $active, |
||
75 | 'hasActiveChild' => $hasActiveChild, |
||
76 | ]); |
||
77 | } |
||
78 | } |
||
79 |
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.