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 |
||
13 | class EntityVersionLockService |
||
14 | { |
||
15 | /** |
||
16 | * @var ObjectManager |
||
17 | */ |
||
18 | private $objectManager; |
||
19 | |||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | private $threshold; |
||
24 | |||
25 | /** |
||
26 | * @var bool |
||
27 | */ |
||
28 | private $lockEnabled; |
||
29 | 6 | ||
30 | public function __construct(ObjectManager $em, $threshold, $lockEnabled) |
||
36 | |||
37 | /** |
||
38 | * @param LockableEntityInterface $entity |
||
39 | * |
||
40 | * @return bool |
||
41 | 3 | */ |
|
42 | public function isEntityBelowThreshold(LockableEntityInterface $entity) |
||
57 | |||
58 | /** |
||
59 | * @param User $user |
||
60 | * @param LockableEntityInterface $entity |
||
61 | * |
||
62 | * @return bool |
||
63 | 2 | */ |
|
64 | public function isEntityLocked(/*\Kunstmaan\AdminBundle\Entity\UserInterface*/ $user, LockableEntityInterface $entity) |
||
92 | |||
93 | 1 | /** |
|
94 | * When editing the entity, create a new entity translation lock. |
||
95 | * |
||
96 | 1 | * @param User $user |
|
97 | 1 | * @param LockableEntity $entity |
|
98 | 1 | */ |
|
99 | protected function createEntityVersionLock(/*\Kunstmaan\AdminBundle\Entity\UserInterface*/ $user, LockableEntity $entity) |
||
120 | |||
121 | 1 | /** |
|
122 | 1 | * @param LockableEntityInterface $entity |
|
123 | * @param User $userToExclude |
||
124 | 1 | * |
|
125 | * @return array |
||
126 | 1 | */ |
|
127 | 1 | public function getUsersWithEntityVersionLock(LockableEntityInterface $entity, /*\Kunstmaan\AdminBundle\Entity\UserInterface*/ $userToExclude = null) |
|
147 | |||
148 | /** |
||
149 | * @param LockableEntity $entity |
||
150 | */ |
||
151 | 3 | protected function removeExpiredLocks(LockableEntity $entity) |
|
158 | |||
159 | /** |
||
160 | * When editing an entity, check if there is a lock for this entity. |
||
161 | * |
||
162 | * @param LockableEntity $entity |
||
163 | * @param User $userToExclude |
||
164 | * |
||
165 | * @return EntityVersionLock[] |
||
166 | 6 | */ |
|
167 | protected function getEntityVersionLocksByLockableEntity(LockableEntity $entity, /*\Kunstmaan\AdminBundle\Entity\UserInterface*/ $userToExclude = null) |
||
179 | |||
180 | /** |
||
181 | * Get or create a LockableEntity for an entity with LockableEntityInterface |
||
182 | 6 | * |
|
183 | * @param LockableEntityInterface $entity |
||
184 | 6 | * |
|
185 | 6 | * @return LockableEntity |
|
186 | */ |
||
187 | protected function getLockableEntity(LockableEntityInterface $entity, $create = true) |
||
199 | |||
200 | 6 | /** |
|
201 | 6 | * @param ObjectManager $objectManager |
|
202 | */ |
||
203 | public function setObjectManager($objectManager) |
||
207 | |||
208 | /** |
||
209 | * @param int $threshold |
||
210 | */ |
||
211 | public function setThreshold($threshold) |
||
215 | |||
216 | /** |
||
217 | * @param bool lockEnabled |
||
218 | */ |
||
219 | public function setLockEnabled($lockEnabled) |
||
223 | } |
||
224 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.