These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of the Sonata Project package. |
||
7 | * |
||
8 | * (c) Thomas Rabaix <[email protected]> |
||
9 | * |
||
10 | * For the full copyright and license information, please view the LICENSE |
||
11 | * file that was distributed with this source code. |
||
12 | */ |
||
13 | |||
14 | namespace Sonata\MediaBundle\Listener\ORM; |
||
15 | |||
16 | use Doctrine\Common\EventArgs; |
||
17 | use Doctrine\ORM\Events; |
||
18 | use Sonata\ClassificationBundle\Model\CategoryInterface; |
||
19 | use Sonata\MediaBundle\Listener\BaseMediaEventSubscriber; |
||
20 | use Sonata\MediaBundle\Model\MediaInterface; |
||
21 | |||
22 | /** |
||
23 | * @final since sonata-project/media-bundle 3.21.0 |
||
24 | */ |
||
25 | class MediaEventSubscriber extends BaseMediaEventSubscriber |
||
26 | { |
||
27 | /** |
||
28 | * @var CategoryInterface[] |
||
29 | */ |
||
30 | protected $rootCategories; |
||
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | public function getSubscribedEvents() |
||
36 | { |
||
37 | return [ |
||
38 | Events::prePersist, |
||
39 | Events::preUpdate, |
||
40 | Events::preRemove, |
||
41 | Events::postUpdate, |
||
42 | Events::postRemove, |
||
43 | Events::postPersist, |
||
44 | Events::onClear, |
||
45 | ]; |
||
46 | } |
||
47 | |||
48 | public function onClear(): void |
||
49 | { |
||
50 | $this->rootCategories = null; |
||
0 ignored issues
–
show
|
|||
51 | } |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | protected function recomputeSingleEntityChangeSet(EventArgs $args): void |
||
57 | { |
||
58 | $em = $args->getEntityManager(); |
||
59 | |||
60 | $em->getUnitOfWork()->recomputeSingleEntityChangeSet( |
||
61 | $em->getClassMetadata(\get_class($args->getEntity())), |
||
62 | $args->getEntity() |
||
0 ignored issues
–
show
It seems like you code against a specific sub-type and not the parent class
Doctrine\Common\EventArgs as the method getEntity() does only exist in the following sub-classes of Doctrine\Common\EventArgs : Doctrine\Common\Persiste...vent\LifecycleEventArgs , Doctrine\Common\Persiste...vent\PreUpdateEventArgs , Doctrine\ORM\Event\LifecycleEventArgs , Doctrine\ORM\Event\PreUpdateEventArgs , Doctrine\Persistence\Event\LifecycleEventArgs , Doctrine\Persistence\Event\PreUpdateEventArgs . 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
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
Loading history...
|
|||
63 | ); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | protected function getMedia(EventArgs $args) |
||
70 | { |
||
71 | $media = $args->getEntity(); |
||
0 ignored issues
–
show
It seems like you code against a specific sub-type and not the parent class
Doctrine\Common\EventArgs as the method getEntity() does only exist in the following sub-classes of Doctrine\Common\EventArgs : Doctrine\Common\Persiste...vent\LifecycleEventArgs , Doctrine\Common\Persiste...vent\PreUpdateEventArgs , Doctrine\ORM\Event\LifecycleEventArgs , Doctrine\ORM\Event\PreUpdateEventArgs , Doctrine\Persistence\Event\LifecycleEventArgs , Doctrine\Persistence\Event\PreUpdateEventArgs . 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
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
Loading history...
|
|||
72 | |||
73 | if (!$media instanceof MediaInterface) { |
||
74 | return $media; |
||
75 | } |
||
76 | |||
77 | if ($this->container->has('sonata.media.manager.category') && !$media->getCategory()) { |
||
78 | $media->setCategory($this->getRootCategory($media)); |
||
79 | } |
||
80 | |||
81 | return $media; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @throws \RuntimeException |
||
86 | * |
||
87 | * @return CategoryInterface |
||
88 | */ |
||
89 | protected function getRootCategory(MediaInterface $media) |
||
90 | { |
||
91 | if (!$this->rootCategories) { |
||
92 | $this->rootCategories = $this->container->get('sonata.media.manager.category')->getRootCategories(false); |
||
93 | } |
||
94 | |||
95 | if (!\array_key_exists($media->getContext(), $this->rootCategories)) { |
||
96 | throw new \RuntimeException(sprintf('There is no main category related to context: %s', $media->getContext())); |
||
97 | } |
||
98 | |||
99 | return $this->rootCategories[$media->getContext()]; |
||
100 | } |
||
101 | } |
||
102 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..