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\Entity\AbstractEntity; |
||
7 | use Kunstmaan\AdminBundle\Event\DeepCloneAndSaveEvent; |
||
8 | use Kunstmaan\AdminBundle\Helper\CloneHelper; |
||
9 | use Kunstmaan\SeoBundle\Entity\Seo; |
||
10 | |||
11 | /** |
||
12 | * This event will make sure the seo metadata is copied when a page is cloned |
||
13 | */ |
||
14 | class CloneListener |
||
15 | { |
||
16 | /** |
||
17 | * @var EntityManager |
||
18 | */ |
||
19 | private $em; |
||
20 | |||
21 | /** |
||
22 | * @var CloneHelper |
||
23 | */ |
||
24 | private $cloneHelper; |
||
25 | |||
26 | /** |
||
27 | * @param EntityManager $em The entity manager |
||
28 | * @param CloneHelper $cloneHelper The clone helper |
||
29 | */ |
||
30 | public function __construct(EntityManager $em, CloneHelper $cloneHelper) |
||
0 ignored issues
–
show
|
|||
31 | { |
||
32 | $this->em = $em; |
||
33 | $this->cloneHelper = $cloneHelper; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @param DeepCloneAndSaveEvent $event |
||
38 | */ |
||
39 | public function postDeepCloneAndSave(DeepCloneAndSaveEvent $event) |
||
40 | { |
||
41 | $originalEntity = $event->getEntity(); |
||
42 | |||
43 | if ($originalEntity instanceof AbstractEntity) { |
||
44 | /* @var Seo $seo */ |
||
45 | $seo = $this->em->getRepository('KunstmaanSeoBundle:Seo')->findFor($originalEntity); |
||
0 ignored issues
–
show
It seems like you code against a concrete implementation and not the interface
Doctrine\Persistence\ObjectRepository as the method findFor() does only exist in the following implementations of said interface: Kunstmaan\PagePartBundle...ConfigurationRepository , 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
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
Loading history...
|
|||
46 | |||
47 | if (!\is_null($seo)) { |
||
48 | /* @var Seo $clonedSeo */ |
||
49 | $clonedSeo = $this->cloneHelper->deepCloneAndSave($seo); |
||
50 | $clonedSeo->setRef($event->getClonedEntity()); |
||
51 | |||
52 | $this->em->persist($clonedSeo); |
||
53 | $this->em->flush($clonedSeo); |
||
54 | } |
||
55 | } |
||
56 | } |
||
57 | } |
||
58 |
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.