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:
Complex classes like PerformanceEventAdmin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PerformanceEventAdmin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class PerformanceEventAdmin extends Admin |
||
26 | { |
||
27 | 1 | protected $baseRouteName = 'AppBundle\Entity\PerformanceEvent'; |
|
28 | 1 | protected $baseRoutePattern = 'PerformanceEvent'; |
|
29 | 1 | protected $datagridValues = [ |
|
30 | 1 | '_sort_order' => 'DESC', |
|
31 | '_sort_by' => 'dateTime', |
||
32 | 1 | ]; |
|
33 | protected $seatPrice = []; |
||
34 | |||
35 | /** @var GenerateSetHandler */ |
||
36 | protected $ticketGenerateSet; |
||
37 | |||
38 | 1 | /** |
|
39 | * PerformanceEventAdmin constructor. |
||
40 | 1 | * @param string $code |
|
41 | * @param string $class |
||
42 | * @param string $baseControllerName |
||
43 | * @param GenerateSetHandler $ticketGenerateSet |
||
44 | */ |
||
45 | public function __construct($code, $class, $baseControllerName, GenerateSetHandler $ticketGenerateSet) |
||
50 | 2 | ||
51 | 2 | /** |
|
52 | 2 | * @var EntityManager |
|
53 | 2 | */ |
|
54 | protected $em; |
||
55 | |||
56 | /** |
||
57 | 2 | * @param RouteCollection $collection |
|
58 | */ |
||
59 | protected function configureRoutes(RouteCollection $collection) |
||
66 | |||
67 | 2 | /** |
|
68 | * @param FormMapper $formMapper |
||
69 | * |
||
70 | 2 | * @return void |
|
71 | 2 | */ |
|
72 | protected function configureFormFields(FormMapper $formMapper) |
||
153 | |||
154 | /** |
||
155 | * @param ListMapper $listMapper |
||
156 | * |
||
157 | * @return void |
||
158 | */ |
||
159 | View Code Duplication | protected function configureListFields(ListMapper $listMapper) |
|
172 | |||
173 | /** |
||
174 | * @param DatagridMapper $datagridMapper |
||
175 | * |
||
176 | * @return void |
||
177 | */ |
||
178 | protected function configureDatagridFilters(DatagridMapper $datagridMapper) |
||
185 | |||
186 | public function preUpdate($object) |
||
205 | |||
206 | public function postUpdate($object) |
||
220 | |||
221 | public function getEm() |
||
228 | |||
229 | /** |
||
230 | * Change Status in Ticket |
||
231 | * form STATUS_OFFLINE to STATUS_FREE if Ticket is in RowsForSale |
||
232 | * and vice versa |
||
233 | * |
||
234 | * @param PerformanceEvent $performanceEvent |
||
235 | * @return int |
||
236 | */ |
||
237 | public function enableTicketsForSale(PerformanceEvent $performanceEvent) |
||
251 | |||
252 | /** |
||
253 | * Inspect PriceCategory. Search errors |
||
254 | * |
||
255 | * @param PerformanceEvent $performanceEvent |
||
256 | * @return bool |
||
257 | */ |
||
258 | public function inspectPriceCategories(PerformanceEvent $performanceEvent) |
||
272 | |||
273 | /** |
||
274 | * Parse string rows in PriceCategory |
||
275 | * |
||
276 | * @param Venue $venue |
||
277 | * @param $strRows |
||
278 | * @param VenueSector $venueSector |
||
279 | * @param $strPlaces |
||
280 | * @return bool|null |
||
281 | */ |
||
282 | public function getRows(Venue $venue, $strRows, VenueSector $venueSector, $strPlaces = null) |
||
298 | |||
299 | /** |
||
300 | * Parse string places in PriceCategory |
||
301 | * |
||
302 | * @param Venue $venue |
||
303 | * @param $row |
||
304 | * @param VenueSector $venueSector |
||
305 | * @param $strPlaces |
||
306 | */ |
||
307 | public function getPlaces(Venue $venue, $row, VenueSector $venueSector, $strPlaces = null) |
||
326 | |||
327 | /** |
||
328 | * Research existing Seat with row-place - $row-$place |
||
329 | * |
||
330 | * @param Venue $venue |
||
331 | * @param $row |
||
332 | * @param VenueSector $venueSector |
||
333 | * @param null $place |
||
334 | * @throws ModelManagerException |
||
335 | */ |
||
336 | public function getSeat(Venue $venue, $row, VenueSector $venueSector, $place = null) |
||
380 | |||
381 | /** |
||
382 | * Search Seat with more than one price |
||
383 | * |
||
384 | * @param $row |
||
385 | * @param $place |
||
386 | * @param VenueSector $venueSector |
||
387 | * @throws ModelManagerException |
||
388 | */ |
||
389 | public function inspectSeatMoreThanOnePrice($row, $place, VenueSector $venueSector) |
||
411 | |||
412 | /** |
||
413 | * Search Seat without price |
||
414 | * |
||
415 | * @param Venue $venue |
||
416 | * @return bool |
||
417 | * @throws ModelManagerException |
||
418 | */ |
||
419 | public function inspectSeatWithoutPrice(Venue $venue) |
||
441 | } |
||
442 |
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.