Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
created

AdminBundle/Controller/ExceptionController.php (2 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
    private function getAdminListConfigurator()
16
    {
17
        if (!isset($this->configurator)) {
18
            $this->configurator = new ExceptionAdminListConfigurator($this->getEntityManager());
0 ignored issues
show
The property configurator does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
        }
20
21
        return $this->configurator;
22
    }
23
24
    /**
25
     * @Route("/", name="kunstmaanadminbundle_admin_exception")
26
     */
27
    public function indexAction(Request $request)
28
    {
29
        return parent::doIndexAction($this->getAdminListConfigurator(), $request);
30
    }
31
32
    /**
33
     * @Route("/resolve_all", name="kunstmaanadminbundle_admin_exception_resolve_all")
34
     *
35
     * @return RedirectResponse
36
     *
37
     * @throws \Doctrine\ORM\NonUniqueResultException
38
     * @throws \Doctrine\ORM\NoResultException
39
     * @throws \InvalidArgumentException
40
     */
41
    public function resolveAllAction()
42
    {
43
        $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...
44
45
        $indexUrl = $this->getAdminListConfigurator()->getIndexUrl();
46
47
        return new RedirectResponse(
48
            $this->generateUrl(
49
                $indexUrl['path'],
50
                isset($indexUrl['params']) ? $indexUrl['params'] : array()
51
            )
52
        );
53
    }
54
55
    /**
56
     * @Route("/toggle_resolve/{id}", name="kunstmaanadminbundle_admin_exception_toggle_resolve")
57
     *
58
     * @param Request   $request
59
     * @param Exception $model
60
     *
61
     * @return RedirectResponse
62
     *
63
     * @throws \Doctrine\ORM\OptimisticLockException
64
     * @throws \InvalidArgumentException
65
     * @throws \Doctrine\ORM\ORMInvalidArgumentException
66
     */
67
    public function toggleResolveAction(Request $request, Exception $model)
68
    {
69
        /* @var EntityManager $em */
70
        $em = $this->getEntityManager();
71
72
        $this->getAdminListConfigurator();
73
74
        $model->setResolved(!$model->isResolved());
75
76
        $em->persist($model);
77
        $em->flush();
78
79
        $indexUrl = $this->configurator->getIndexUrl();
80
81
        return new RedirectResponse(
82
            $this->generateUrl(
83
                $indexUrl['path'],
84
                isset($indexUrl['params']) ? $indexUrl['params'] : array()
85
            )
86
        );
87
    }
88
}
89