Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

Kunstmaan/SeoBundle/EventListener/NodeListener.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 Kunstmaan\SeoBundle\EventListener;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\AdminBundle\Helper\FormWidgets\FormWidget;
7
use Kunstmaan\AdminBundle\Helper\FormWidgets\Tabs\Tab;
8
use Kunstmaan\NodeBundle\Entity\HasNodeInterface;
9
use Kunstmaan\NodeBundle\Event\AdaptFormEvent;
10
use Kunstmaan\SeoBundle\Entity\Seo;
11
use Kunstmaan\SeoBundle\Form\SeoType;
12
use Kunstmaan\SeoBundle\Form\SocialType;
13
14
/**
15
 * This will add a seo tab on each page
16
 */
17
class NodeListener
18
{
19
    /**
20
     * @var EntityManager
21
     */
22
    private $em;
23
24
    /**
25
     * @param EntityManager $em
26
     */
27
    public function __construct(EntityManager $em)
28
    {
29
        $this->em = $em;
30
    }
31
32
    /**
33
     * @param AdaptFormEvent $event
34
     */
35
    public function adaptForm(AdaptFormEvent $event)
36
    {
37
        if ($event->getPage() instanceof HasNodeInterface && !$event->getPage()->isStructureNode()) {
38
            /* @var Seo $seo */
39
            $seo = $this->em->getRepository('KunstmaanSeoBundle:Seo')->findOrCreateFor($event->getPage());
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method findOrCreateFor() does only exist in the following implementations of said interface: Kunstmaan\SeoBundle\Repository\SeoRepository.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
40
41
            $seoWidget = new FormWidget();
42
            $seoWidget->addType('seo', SeoType::class, $seo);
43
            $event->getTabPane()->addTab(new Tab('seo.tab.seo.title', $seoWidget));
44
45
            $socialWidget = new FormWidget();
46
            $socialWidget->addType('social', SocialType::class, $seo);
47
            $socialWidget->setTemplate('@KunstmaanSeo/Admin/Social/social.html.twig');
48
            $event->getTabPane()->addTab(new Tab('seo.tab.social.title', $socialWidget));
49
        }
50
    }
51
}
52