Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

src/Kunstmaan/AdminBundle/Helper/CloneHelper.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\Helper;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\AdminBundle\Event\DeepCloneAndSaveEvent;
7
use Kunstmaan\AdminBundle\Event\Events;
8
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9
10
/**
11
 * This helper will help you to clone Entities
12
 */
13
class CloneHelper
14
{
15
    /**
16
     * @var EntityManager
17
     */
18
    private $em;
19
20
    /**
21
     * @var EventDispatcherInterface
22
     */
23
    private $eventDispatcher;
24
25
    /**
26
     * @param EntityManager            $em              The EntityManager
27
     * @param EventDispatcherInterface $eventDispatcher The EventDispatchInterface
28
     */
29
    public function __construct(EntityManager $em, EventDispatcherInterface $eventDispatcher)
0 ignored issues
show
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

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:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
30
    {
31
        $this->em = $em;
32
        $this->eventDispatcher = $eventDispatcher;
33
    }
34
35
    /**
36
     * @param mixed $entity
37
     *
38
     * @return mixed
39
     */
40
    public function deepCloneAndSave($entity)
41
    {
42
        $clonedEntity = clone $entity;
43
        $this->eventDispatcher->dispatch(Events::DEEP_CLONE_AND_SAVE, new DeepCloneAndSaveEvent($entity, $clonedEntity, $this->em));
0 ignored issues
show
The call to DeepCloneAndSaveEvent::__construct() has too many arguments starting with $this->em.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
44
45
        $this->em->persist($clonedEntity);
46
        $this->em->flush();
47
48
        $this->eventDispatcher->dispatch(Events::POST_DEEP_CLONE_AND_SAVE, new DeepCloneAndSaveEvent($entity, $clonedEntity, $this->em));
0 ignored issues
show
The call to DeepCloneAndSaveEvent::__construct() has too many arguments starting with $this->em.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
49
50
        return $clonedEntity;
51
    }
52
}
53