Completed
Push — 5.3 ( 958546...1cc96e )
by Jeroen
14:02 queued 07:05
created

AdminBundle/Toolbar/ExceptionDataCollector.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\AdminBundle\Toolbar;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Kunstmaan\AdminBundle\Entity\Exception;
7
use Kunstmaan\AdminBundle\Helper\Toolbar\AbstractDataCollector;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
11
class ExceptionDataCollector extends AbstractDataCollector
12
{
13
    /**
14
     * @var EntityManagerInterface
15
     */
16
    private $em;
17
18
    public function __construct(EntityManagerInterface $em)
19
    {
20
        $this->em = $em;
21
    }
22
23
    /**
24
     * @return array
25
     */
26
    public function getAccessRoles()
27
    {
28
        return ['ROLE_ADMIN'];
29
    }
30
31
    /**
32
     * @return array
33
     */
34
    public function collectData()
35
    {
36
        $model = $this->em->getRepository(Exception::class)->findExceptionStatistics();
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method findExceptionStatistics() 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...
37
        if (isset($model['cp_all'], $model['cp_sum'])) {
38
            return [
39
                'data' => $model,
40
            ];
41
        } else {
42
            return [];
43
        }
44
    }
45
46
    /**
47
     * @param Request         $request
48
     * @param Response        $response
49
     * @param \Exception|null $exception
50
     */
51
    public function collect(Request $request, Response $response, \Exception $exception = null)
52
    {
53
        if (false === $this->isEnabled()) {
54
            $this->data = false;
55
        } else {
56
            $this->data = $this->collectData();
57
        }
58
    }
59
60
    /**
61
     * Gets the data for template
62
     *
63
     * @return array The request events
64
     */
65
    public function getTemplateData()
66
    {
67
        return $this->data;
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function getName()
74
    {
75
        return 'kuma_exception';
76
    }
77
78
    /**
79
     * @return bool
80
     */
81
    public function isEnabled()
82
    {
83
        return true;
84
    }
85
86
    public function reset()
87
    {
88
        $this->data = [];
89
    }
90
}
91