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\AdminBundle\Helper\Menu; |
||
4 | |||
5 | use Symfony\Component\HttpFoundation\Request; |
||
6 | use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
||
7 | |||
8 | class SimpleMenuAdaptor implements MenuAdaptorInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var AuthorizationCheckerInterface |
||
12 | */ |
||
13 | private $authorizationChecker; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private $menuItems; |
||
19 | |||
20 | /** |
||
21 | * @param AuthorizationCheckerInterface $authorizationChecker |
||
22 | * @param array $menuItems |
||
23 | */ |
||
24 | public function __construct(AuthorizationCheckerInterface $authorizationChecker, array $menuItems) |
||
25 | { |
||
26 | $this->authorizationChecker = $authorizationChecker; |
||
27 | $this->menuItems = $menuItems; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * In this method you can add children for a specific parent, but also remove and change the already created |
||
32 | * children |
||
33 | * |
||
34 | * @param MenuBuilder $menu The MenuBuilder |
||
35 | * @param MenuItem[] &$children The current children |
||
36 | * @param MenuItem|null $parent The parent Menu item |
||
37 | * @param Request $request The Request |
||
38 | */ |
||
39 | public function adaptChildren(MenuBuilder $menu, array &$children, MenuItem $parent = null, Request $request = null) |
||
40 | { |
||
41 | foreach ($this->menuItems as $item) { |
||
42 | if (false === $this->parentMatches($parent, $item)) { |
||
43 | continue; |
||
44 | } |
||
45 | |||
46 | if ($item['role'] && false === $this->authorizationChecker->isGranted($item['role'])) { |
||
47 | continue; |
||
48 | } |
||
49 | |||
50 | $menuItem = new TopMenuItem($menu); |
||
51 | $menuItem |
||
52 | ->setRoute($item['route'], $item['params']) |
||
53 | ->setLabel($item['label']) |
||
54 | ->setUniqueId($item['route']) |
||
55 | ->setParent($parent); |
||
56 | |||
57 | View Code Duplication | if ($request && stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) { |
|
58 | $menuItem->setActive(true); |
||
59 | $parent->setActive(true); |
||
0 ignored issues
–
show
|
|||
60 | } |
||
61 | |||
62 | $children[] = $menuItem; |
||
63 | } |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param MenuItem $parent |
||
0 ignored issues
–
show
Should the type for parameter
$parent not be null|MenuItem ?
This check looks for It makes a suggestion as to what type it considers more descriptive. Most often this is a case of a parameter that can be null in addition to its declared types.
Loading history...
|
|||
68 | * @param array $item |
||
69 | * |
||
70 | * @return bool |
||
71 | */ |
||
72 | private function parentMatches(MenuItem $parent = null, $item) |
||
73 | { |
||
74 | if (null === $parent) { |
||
75 | return null === $item['parent']; |
||
76 | } |
||
77 | |||
78 | return $item['parent'] === $parent->getRoute(); |
||
79 | } |
||
80 | } |
||
81 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: