Completed
Push — master ( 0eab77...b2f729 )
by Paul
05:35
created

Bundle/BlogBundle/Listener/ArticleMenuListener.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

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
It seems like you code against a specific sub-type and not the parent class Victoire\Bundle\CoreBundle\Entity\View as the method getBusinessEntity() does only exist in the following sub-classes of Victoire\Bundle\CoreBundle\Entity\View: Victoire\Bundle\Business...dle\Entity\BusinessPage, Victoire\Bundle\Business...ity\VirtualBusinessPage. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
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