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