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 ViewReferenceManager |
||
15 | { |
||
16 | private $manager; |
||
17 | private $repository; |
||
18 | private $transformer; |
||
19 | |||
20 | /** |
||
21 | * ViewReferenceManager constructor. |
||
22 | * |
||
23 | * @param ViewReferenceConnectorManagerInterface $manager |
||
24 | * @param ViewReferenceConnectorRepositoryInterface $repository |
||
25 | * @param ViewReferenceTransformerChain $transformer |
||
26 | */ |
||
27 | public function __construct(ViewReferenceConnectorManagerInterface $manager, ViewReferenceConnectorRepositoryInterface $repository, ViewReferenceTransformerChain $transformer) |
||
33 | |||
34 | /** |
||
35 | * This method save a tree of viewReferences. |
||
36 | * |
||
37 | * @param array $viewReferences |
||
38 | * @param string $parentId |
||
39 | * @param string $parentLocale |
||
40 | * @param bool $reset |
||
41 | */ |
||
42 | public function saveReferences(array $viewReferences, $parentId = null, $parentLocale = null, $reset = true) |
||
65 | |||
66 | /** |
||
67 | * This method save a Reference. |
||
68 | * |
||
69 | * @param ViewReference $viewReference |
||
70 | * @param string $parentId |
||
71 | * @param string $parentLocale |
||
72 | * |
||
73 | * @return mixed |
||
74 | */ |
||
75 | public function saveReference(ViewReference $viewReference, $parentId = null, $parentLocale = null) |
||
95 | |||
96 | /** |
||
97 | * This method remove reference for a ViewReference. |
||
98 | * |
||
99 | * @param ViewReference $viewReference |
||
100 | */ |
||
101 | View Code Duplication | public function removeReference(ViewReference $viewReference) |
|
111 | |||
112 | /** |
||
113 | * Find the transformer for an element. |
||
114 | * |
||
115 | * @param $element |
||
116 | * |
||
117 | * @return ArrayToBusinessPageReferenceTransformer|ArrayToViewReferenceTransformer |
||
118 | */ |
||
119 | public static function findTransformerFromElement($element) |
||
129 | |||
130 | /** |
||
131 | * Remove an url for a viewReference with his reference in redis. |
||
132 | * |
||
133 | * @param ViewReference $viewReference |
||
134 | */ |
||
135 | View Code Duplication | public function removeUrlForViewReference(ViewReference $viewReference) |
|
142 | } |
||
143 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: