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 |
||
14 | View Code Duplication | class Param extends \Model { |
|
15 | |||
16 | public static $objectName = 'Параметр товара'; |
||
17 | public static $labels = [ |
||
18 | 'item_option_id' => 'Параметр', |
||
19 | 'item_id' => 'Товар', |
||
20 | 'value' => 'Значение', |
||
21 | ]; |
||
22 | public static $cols = [ |
||
23 | //Основные параметры |
||
24 | 'item_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'item'], |
||
25 | 'item_option_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'option', 'onChange' => 'reloadForm'], |
||
26 | 'value' => ['type' => 'dynamicType', 'typeSource' => 'selfMethod', 'selfMethod' => 'realType'], |
||
27 | //Системные |
||
28 | 'date_create' => ['type' => 'dateTime'] |
||
29 | ]; |
||
30 | |||
31 | public static function indexes() { |
||
54 | |||
55 | public function realType() { |
||
70 | |||
71 | public static $dataManagers = [ |
||
72 | |||
73 | 'manager' => [ |
||
74 | 'name' => 'Параметры товара', |
||
75 | 'cols' => [ |
||
76 | 'item_option_id', |
||
77 | 'item_id', |
||
78 | 'value', |
||
79 | ], |
||
80 | ], |
||
81 | ]; |
||
82 | public static $forms = [ |
||
83 | 'manager' => [ |
||
84 | 'map' => [ |
||
85 | ['item_id', 'item_option_id'], |
||
86 | ['value'] |
||
87 | ] |
||
88 | ]]; |
||
89 | |||
90 | function name() { |
||
93 | |||
94 | public function value($default = '') { |
||
102 | |||
103 | public static function relations() { |
||
123 | |||
124 | } |
||
125 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.