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 |
||
14 | class InMemoryRepository implements RepositoryInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $entityName; |
||
20 | |||
21 | /** |
||
22 | * @var CriteriaFactory |
||
23 | */ |
||
24 | protected $criteriaFactory; |
||
25 | |||
26 | /** |
||
27 | * @var EntityFactoryInterface |
||
28 | */ |
||
29 | protected $entityFactory; |
||
30 | |||
31 | /** |
||
32 | * @var ArrayObject |
||
33 | */ |
||
34 | protected $identityMap; |
||
35 | |||
36 | /** |
||
37 | * @var ArrayObject |
||
38 | */ |
||
39 | protected $identityMapOriginal; |
||
40 | |||
41 | /** |
||
42 | * @var EventManagerInterface |
||
43 | */ |
||
44 | protected $eventManager; |
||
45 | |||
46 | /** |
||
47 | * @var EntityChangedEvent |
||
48 | */ |
||
49 | protected $event; |
||
50 | |||
51 | protected $primaryKey = 1; |
||
52 | |||
53 | /** |
||
54 | * @param string $entityName |
||
55 | * @param CriteriaFactory $criteriaFactory |
||
56 | * @param EntityFactoryInterface $entityFactory |
||
57 | * @param EventManagerInterface $eventManager |
||
58 | */ |
||
59 | public function __construct( |
||
72 | |||
73 | /** |
||
74 | * @param EntityInterface $entity |
||
75 | * @return EntityInterface|int|null |
||
76 | */ |
||
77 | public function add(EntityInterface $entity) |
||
108 | |||
109 | /** |
||
110 | * @param EntityInterface $entity |
||
111 | * @return int|null |
||
112 | */ |
||
113 | public function remove(EntityInterface $entity) |
||
128 | |||
129 | /** |
||
130 | * @param CriteriaInterface|array $criteria |
||
131 | * @return EntityInterface|null |
||
132 | */ |
||
133 | public function find($criteria) |
||
159 | |||
160 | /** |
||
161 | * @param mixed $id |
||
162 | * @return EntityInterface|null |
||
163 | */ |
||
164 | public function findById($id) |
||
171 | |||
172 | /** |
||
173 | * @param CriteriaInterface|array $criteria |
||
174 | * @return EntityInterface[] |
||
175 | */ |
||
176 | public function findMany($criteria) |
||
198 | |||
199 | /** |
||
200 | * @param CriteriaInterface|array $criteria |
||
201 | * @return int |
||
202 | */ |
||
203 | public function count($criteria) |
||
214 | |||
215 | /** |
||
216 | * @param array $filter |
||
217 | * @return CriteriaInterface |
||
218 | */ |
||
219 | public function createCriteria(array $filter = []) |
||
223 | |||
224 | /** |
||
225 | * @param EntityInterface $entity |
||
226 | */ |
||
227 | protected function toIdentityMap(EntityInterface $entity) |
||
232 | |||
233 | /** |
||
234 | * @param EntityInterface $changedEntity |
||
235 | * @return bool |
||
236 | */ |
||
237 | protected function isEntityChanged(EntityInterface $changedEntity) |
||
242 | |||
243 | /** |
||
244 | * @return EntityChangedEvent |
||
245 | */ |
||
246 | View Code Duplication | protected function getEvent() |
|
255 | |||
256 | /** |
||
257 | * @param EntityInterface $createdEntity |
||
258 | */ |
||
259 | View Code Duplication | protected function triggerCreate(EntityInterface &$createdEntity) |
|
274 | |||
275 | /** |
||
276 | * @param EntityInterface $deletedEntity |
||
277 | */ |
||
278 | View Code Duplication | protected function triggerDelete(EntityInterface $deletedEntity) |
|
289 | |||
290 | /** |
||
291 | * @param EntityChangedEvent $e |
||
292 | */ |
||
293 | protected function triggerChanges(EntityChangedEvent $e) |
||
298 | |||
299 | /** |
||
300 | * @param EntityChangedEvent $e |
||
301 | */ |
||
302 | protected function triggerPreChanges(EntityChangedEvent $e) |
||
307 | |||
308 | /** |
||
309 | * @param EntityChangedEvent $e |
||
310 | */ |
||
311 | View Code Duplication | protected function triggerAttributesChange(EntityChangedEvent $e) |
|
322 | |||
323 | /** |
||
324 | * @param EntityInterface $changedEntity |
||
325 | * @return string |
||
326 | */ |
||
327 | protected function getEntityChangeEventName(EntityInterface $changedEntity) |
||
331 | |||
332 | /** |
||
333 | * @param EntityInterface $changedEntity |
||
334 | * @param $attributeName |
||
335 | * @return string |
||
336 | */ |
||
337 | protected function getAttributeChangeEventName(EntityInterface $changedEntity, $attributeName) |
||
341 | } |
||
342 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.