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 |
||
22 | class BusinessPageHelper |
||
23 | { |
||
24 | protected $queryHelper = null; |
||
25 | protected $viewReferenceRepository = null; |
||
26 | protected $businessEntityHelper = null; |
||
27 | protected $parameterConverter = null; |
||
28 | protected $urlBuilder = null; |
||
29 | |||
30 | /** |
||
31 | * @param QueryHelper $queryHelper |
||
32 | * @param ViewReferenceRepository $viewReferenceRepository |
||
33 | * @param BusinessEntityHelper $businessEntityHelper |
||
34 | * @param ParameterConverter $parameterConverter |
||
35 | * @param UrlBuilder $urlBuilder |
||
36 | */ |
||
37 | View Code Duplication | public function __construct(QueryHelper $queryHelper, ViewReferenceRepository $viewReferenceRepository, BusinessEntityHelper $businessEntityHelper, ParameterConverter $parameterConverter, UrlBuilder $urlBuilder) |
|
45 | |||
46 | /** |
||
47 | * Is the entity allowed for the business entity page. |
||
48 | * |
||
49 | * @param BusinessTemplate $businessTemplate |
||
50 | * @param object|null $entity |
||
51 | * @param EntityManager $em |
||
52 | * |
||
53 | * @throws \Exception |
||
54 | * |
||
55 | * @return bool |
||
56 | */ |
||
57 | public function isEntityAllowed(BusinessTemplate $businessTemplate, $entity, EntityManager $em = null) |
||
91 | |||
92 | /** |
||
93 | * Get the list of entities allowed for the BusinessTemplate page. |
||
94 | * |
||
95 | * @param BusinessTemplate $businessTemplate |
||
96 | * @param EntityManager $em |
||
97 | * |
||
98 | * @return array |
||
99 | */ |
||
100 | public function getEntitiesAllowed(BusinessTemplate $businessTemplate, EntityManager $em) |
||
106 | |||
107 | /** |
||
108 | * Get the list of entities allowed for the BusinessTemplate page. |
||
109 | * |
||
110 | * @param BusinessTemplate $businessTemplate |
||
111 | * @param EntityManager $em |
||
112 | * |
||
113 | * @throws \Exception |
||
114 | * |
||
115 | * @return QueryBuilder |
||
116 | */ |
||
117 | public function getEntitiesAllowedQueryBuilder(BusinessTemplate $businessTemplate, EntityManager $em) |
||
129 | |||
130 | /** |
||
131 | * Get the list of business properties usable for the url. |
||
132 | * |
||
133 | * @param BusinessEntity $businessEntity |
||
134 | * |
||
135 | * @return BusinessProperty[] The list of business properties |
||
136 | */ |
||
137 | View Code Duplication | public function getBusinessProperties(BusinessEntity $businessEntity) |
|
150 | |||
151 | /** |
||
152 | * Get the position of the identifier in the url of a business entity page pattern. |
||
153 | * |
||
154 | * @param BusinessTemplate $businessTemplate |
||
155 | * |
||
156 | * @return int The position |
||
157 | */ |
||
158 | public function getIdentifierPositionInUrl(BusinessTemplate $businessTemplate) |
||
194 | |||
195 | /** |
||
196 | * Guess the best pattern to represent given reflectionClass. |
||
197 | * |
||
198 | * @param \ReflectionClass $refClass |
||
199 | * @param int $entityId |
||
200 | * @param EntityManager $em |
||
201 | * @param string $originalRefClassName When digging into parentClass, we do not have to forget originalClass to be able to get reference after all |
||
202 | * |
||
203 | * @throws \Exception |
||
204 | * |
||
205 | * @return View |
||
206 | */ |
||
207 | public function guessBestPatternIdForEntity($refClass, $entityId, $em, $originalRefClassName = null) |
||
240 | } |
||
241 |
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.