Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

AdminBundle/Controller/ExceptionController.php (3 issues)

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\AdminBundle\Controller;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\AdminBundle\AdminList\ExceptionAdminListConfigurator;
7
use Kunstmaan\AdminBundle\Entity\Exception;
8
use Kunstmaan\AdminListBundle\Controller\AdminListController;
9
use Symfony\Component\Routing\Annotation\Route;
10
use Symfony\Component\HttpFoundation\RedirectResponse;
11
use Symfony\Component\HttpFoundation\Request;
12
13
class ExceptionController extends AdminListController
14
{
15
    /** @var ExceptionAdminListConfigurator */
16
    private $configurator;
17
18
    private function getAdminListConfigurator()
19
    {
20
        if (!isset($this->configurator)) {
21
            $this->configurator = new ExceptionAdminListConfigurator($this->getEntityManager());
0 ignored issues
show
$this->getEntityManager() of type object<Doctrine\Persistence\ObjectManager> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\Persistence\ObjectManager to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
22
        }
23
24
        return $this->configurator;
25
    }
26
27
    /**
28
     * @Route("/", name="kunstmaanadminbundle_admin_exception")
29
     */
30
    public function indexAction(Request $request)
31
    {
32
        return parent::doIndexAction($this->getAdminListConfigurator(), $request);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (doIndexAction() instead of indexAction()). Are you sure this is correct? If so, you might want to change this to $this->doIndexAction().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
33
    }
34
35
    /**
36
     * @Route("/resolve_all", name="kunstmaanadminbundle_admin_exception_resolve_all")
37
     *
38
     * @return RedirectResponse
39
     *
40
     * @throws \Doctrine\ORM\NonUniqueResultException
41
     * @throws \Doctrine\ORM\NoResultException
42
     * @throws \InvalidArgumentException
43
     */
44
    public function resolveAllAction()
45
    {
46
        $this->getEntityManager()->getRepository(Exception::class)->markAllAsResolved();
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method markAllAsResolved() does only exist in the following implementations of said interface: Kunstmaan\AdminBundle\Re...ory\ExceptionRepository.

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...
47
48
        $indexUrl = $this->getAdminListConfigurator()->getIndexUrl();
49
50
        return new RedirectResponse(
51
            $this->generateUrl(
52
                $indexUrl['path'],
53
                isset($indexUrl['params']) ? $indexUrl['params'] : array()
54
            )
55
        );
56
    }
57
58
    /**
59
     * @Route("/toggle_resolve/{id}", name="kunstmaanadminbundle_admin_exception_toggle_resolve")
60
     *
61
     * @param Request   $request
62
     * @param Exception $model
63
     *
64
     * @return RedirectResponse
65
     *
66
     * @throws \Doctrine\ORM\OptimisticLockException
67
     * @throws \InvalidArgumentException
68
     * @throws \Doctrine\ORM\ORMInvalidArgumentException
69
     */
70
    public function toggleResolveAction(Request $request, Exception $model)
71
    {
72
        /* @var EntityManager $em */
73
        $em = $this->getEntityManager();
74
75
        $this->getAdminListConfigurator();
76
77
        $model->setResolved(!$model->isResolved());
78
79
        $em->persist($model);
80
        $em->flush();
81
82
        $indexUrl = $this->configurator->getIndexUrl();
83
84
        return new RedirectResponse(
85
            $this->generateUrl(
86
                $indexUrl['path'],
87
                isset($indexUrl['params']) ? $indexUrl['params'] : array()
88
            )
89
        );
90
    }
91
}
92