Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 9 | View Code Duplication | class StringToActorsTransformer implements DataTransformerInterface |
|
| 10 | { |
||
| 11 | protected $manager; |
||
| 12 | protected $actorRepository; |
||
| 13 | |||
| 14 | public function __construct(EntityManager $manager, EntityRepository $actorRepository) |
||
| 19 | |||
| 20 | // En théorie, ne sera jamais utilisé |
||
| 21 | public function transform($actors) |
||
| 25 | |||
| 26 | public function reverseTransform($string) |
||
| 49 | } |
||
| 50 |
The
EntityManagermight 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
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring 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.