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 |
||
29 | View Code Duplication | class EventRoute extends TemplateRoute |
|
|
|||
30 | { |
||
31 | use TranslatorAwareTrait; |
||
32 | |||
33 | /** |
||
34 | * URI path. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | private $path; |
||
39 | |||
40 | /** |
||
41 | * The event object matching the URI path. |
||
42 | * |
||
43 | * @var EventInterface|RoutableInterface |
||
44 | */ |
||
45 | private $event; |
||
46 | |||
47 | /** |
||
48 | * The event model. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | private $objType = 'charcoal/cms/event'; |
||
53 | |||
54 | /** |
||
55 | * @param array $data Class depdendencies. |
||
56 | */ |
||
57 | public function __construct(array $data) |
||
62 | |||
63 | /** |
||
64 | * Determine if the URI path resolves to an object. |
||
65 | * |
||
66 | * @param Container $container A DI (Pimple) container. |
||
67 | * @return boolean |
||
68 | */ |
||
69 | public function pathResolvable(Container $container) |
||
74 | |||
75 | /** |
||
76 | * @param Container $container A DI (Pimple) container. |
||
77 | * @param RequestInterface $request A PSR-7 compatible Request instance. |
||
78 | * @param ResponseInterface $response A PSR-7 compatible Response instance. |
||
79 | * @return ResponseInterface |
||
80 | */ |
||
81 | public function __invoke( |
||
130 | |||
131 | /** |
||
132 | * @todo Add support for `@see setlocale()`; {@see GenericRoute::setLocale()} |
||
133 | * @param Container $container Pimple DI container. |
||
134 | * @return EventInterface|null |
||
135 | */ |
||
136 | protected function loadEventFromPath(Container $container) |
||
169 | } |
||
170 |
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.