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 |
||
9 | class OnBeforeIBlockPropertyUpdate extends BaseHandler implements HandlerInterface |
||
10 | { |
||
11 | /** |
||
12 | * Old property fields from DB. |
||
13 | * |
||
14 | * @var array |
||
15 | */ |
||
16 | protected $dbFields; |
||
17 | |||
18 | /** |
||
19 | * Constructor. |
||
20 | * |
||
21 | * @param array $params |
||
22 | * |
||
23 | * @throws SkipHandlerException |
||
24 | */ |
||
25 | public function __construct($params) |
||
35 | |||
36 | /** |
||
37 | * Get migration name. |
||
38 | * |
||
39 | * @return string |
||
40 | */ |
||
41 | public function getName() |
||
45 | |||
46 | /** |
||
47 | * Get template name. |
||
48 | * |
||
49 | * @return string |
||
50 | */ |
||
51 | public function getTemplate() |
||
55 | |||
56 | /** |
||
57 | * Get array of placeholders to replace. |
||
58 | * |
||
59 | * @return array |
||
60 | */ |
||
61 | View Code Duplication | public function getReplace() |
|
69 | |||
70 | /** |
||
71 | * Collect property fields from DB and convert them to format that can be compared from user input. |
||
72 | * |
||
73 | * @return array |
||
74 | */ |
||
75 | protected function collectPropertyFieldsFromDB() |
||
102 | |||
103 | /** |
||
104 | * Determine if property has been changed. |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | protected function propertyHasChanged() |
||
118 | } |
||
119 |
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.