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 |
||
31 | class AuthorizationRepository |
||
32 | { |
||
33 | /** |
||
34 | * @var EntityManager |
||
35 | */ |
||
36 | private $entityManager; |
||
37 | |||
38 | public function __construct(EntityManager $entityManager) |
||
42 | |||
43 | /** |
||
44 | * Return all institutions were the actor has the specified role for |
||
45 | * The returned institutions are used to filter query results on |
||
46 | * |
||
47 | * @param InstitutionRole $role |
||
48 | * @param IdentityId $actorId |
||
49 | * @return InstitutionCollection |
||
50 | */ |
||
51 | public function getInstitutionsForRole(InstitutionRole $role, IdentityId $actorId) |
||
85 | |||
86 | /** |
||
87 | * This is the mapping to look up allowed institution roles |
||
88 | * - if the institution role is RA we should look if the configured institution has RA role |
||
89 | * - if the institution role is RAA we should look if the configured institution has RAA role |
||
90 | * |
||
91 | * @param InstitutionRole $role |
||
92 | * @return array |
||
93 | */ |
||
94 | View Code Duplication | private function getAllowedInstitutionRoles(InstitutionRole $role) |
|
105 | |||
106 | /** |
||
107 | * This is the mapping to look up allowed identity roles for a specific institution role |
||
108 | * - if the institution role is RA we should look if the identity has a RA or RAA role |
||
109 | * - if the institution role is RAA we should look if the identity has a RAA role |
||
110 | * |
||
111 | * @param InstitutionRole $role |
||
112 | * @return array |
||
113 | */ |
||
114 | View Code Duplication | private function getAllowedIdentityRoles(InstitutionRole $role) |
|
125 | } |
||
126 |
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.