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 Victoire\Bundle\BlogBundle\Listener; |
||
4 | |||
5 | use Symfony\Component\EventDispatcher\Event; |
||
6 | use Victoire\Bundle\CoreBundle\Listener\MenuListenerInterface; |
||
7 | use Victoire\Bundle\CoreBundle\Menu\MenuBuilder; |
||
8 | use Victoire\Bundle\PageBundle\Event\Menu\PageMenuContextualEvent; |
||
9 | |||
10 | /** |
||
11 | * When dispatched, this listener add items to a KnpMenu. |
||
12 | */ |
||
13 | class ArticleMenuListener implements MenuListenerInterface |
||
14 | { |
||
15 | protected $menuBuilder; |
||
16 | |||
17 | /** |
||
18 | * Blog menu listener constructor. |
||
19 | * |
||
20 | * @param MenuBuilder $menuBuilder |
||
21 | */ |
||
22 | public function __construct(MenuBuilder $menuBuilder) |
||
23 | { |
||
24 | $this->menuBuilder = $menuBuilder; |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * add a contextual menu item. |
||
29 | * |
||
30 | * @param PageMenuContextualEvent $event |
||
31 | * |
||
32 | * @return \Knp\Menu\ItemInterface |
||
33 | */ |
||
34 | public function addContextual($event) |
||
35 | { |
||
36 | $page = $event->getPage(); |
||
37 | $currentArticle = $event->getPage()->getBusinessEntity(); |
||
0 ignored issues
–
show
|
|||
38 | |||
39 | $bottomRightNavbar = $this->menuBuilder->getBottomRightNavbar(); |
||
40 | |||
41 | $bottomRightNavbar->addChild('menu.page.settings', |
||
42 | [ |
||
43 | 'route' => 'victoire_blog_article_settings', |
||
44 | 'routeParameters' => [ |
||
45 | 'id' => $currentArticle->getId(), |
||
46 | 'page_id' => $page->getId(), |
||
47 | ], |
||
48 | 'linkAttributes' => [ |
||
49 | 'class' => 'v-btn v-btn--sm v-btn--transparent', |
||
50 | 'id' => 'v-settings-link', |
||
51 | ], |
||
52 | ] |
||
53 | )->setLinkAttribute('data-toggle', 'vic-modal'); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * add global menu items. |
||
58 | * |
||
59 | * @param Event $event |
||
60 | * |
||
61 | * @return \Victoire\Bundle\BlogBundle\Listener\MenuItem |
||
62 | * |
||
63 | * @SuppressWarnings checkUnusedFunctionParameters |
||
64 | */ |
||
65 | public function addGlobal(Event $event) |
||
66 | { |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * This method returns you the main item and create it if not exists. |
||
71 | * |
||
72 | * @return \Knp\Menu\ItemInterface The main item to get |
||
73 | */ |
||
74 | View Code Duplication | private function getMainItem() |
|
75 | { |
||
76 | $menuPage = $this->menuBuilder->getTopNavbar()->getChild('menu.page'); |
||
77 | |||
78 | if ($menuPage) { |
||
79 | return $menuPage; |
||
80 | } else { |
||
81 | return $this->menuBuilder->createDropdownMenuItem( |
||
82 | $this->menuBuilder->getTopNavbar(), |
||
83 | 'menu.page' |
||
84 | ); |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: