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 |
||
10 | class EntityModel { |
||
11 | /** |
||
12 | * Link to DB which used by this EntityModel |
||
13 | * |
||
14 | * @var \db_mysql $dbStatic |
||
15 | * deprecated - replace with container ID like 'db' or 'dbAuth' |
||
16 | */ |
||
17 | protected $dbStatic; |
||
18 | /** |
||
19 | * Service to work with rows |
||
20 | * |
||
21 | * @var \DbRowDirectOperator $rowOperator |
||
22 | */ |
||
23 | protected $rowOperator; |
||
24 | /** |
||
25 | * Name of table for this entity |
||
26 | * |
||
27 | * @var string $tableName |
||
28 | */ |
||
29 | protected $tableName = '_table'; |
||
30 | /** |
||
31 | * Name of key field field in this table |
||
32 | * |
||
33 | * @var string $idField |
||
34 | */ |
||
35 | protected $idField = 'id'; |
||
36 | |||
37 | /** |
||
38 | * Name of exception class that would be thrown |
||
39 | * |
||
40 | * Uses for calling when you don't know which exact exception should be called |
||
41 | * On EntityModel's children should be used exception class name |
||
42 | * |
||
43 | * @var string $exceptionClass |
||
44 | */ |
||
45 | protected $exceptionClass = 'EntityException'; |
||
46 | protected $entityContainerClass = '\EntityContainer'; |
||
47 | |||
48 | /** |
||
49 | * Property list and description |
||
50 | * |
||
51 | * propertyName => array( |
||
52 | * P_DB_FIELD => 'dbFieldName', - directly converts property to field and vice versa |
||
53 | * ) |
||
54 | * |
||
55 | * @var array[] $properties |
||
56 | */ |
||
57 | protected $properties = array(); |
||
58 | |||
59 | /** |
||
60 | * Array of accessors - getters/setters/etc |
||
61 | * |
||
62 | * @var callable[][] |
||
63 | */ |
||
64 | protected $accessors = array(); |
||
65 | |||
66 | View Code Duplication | protected function assignAccessor($varName, $type, $callable) { |
|
77 | |||
78 | |||
79 | /** |
||
80 | * EntityModel constructor. |
||
81 | * |
||
82 | * @param \Common\GlobalContainer $gc |
||
83 | */ |
||
84 | public function __construct($gc) { |
||
88 | |||
89 | /** |
||
90 | * Returns link to DB used by entity |
||
91 | * |
||
92 | * DB can be differ for different entity. For ex. - UNIT EntityModel will use standard DB while AUTH entity would prefer dbAuth |
||
93 | * |
||
94 | * @return db_mysql |
||
95 | */ |
||
96 | public function getDbStatic() { |
||
99 | |||
100 | /** |
||
101 | * @return \DbRowDirectOperator |
||
102 | */ |
||
103 | public function getRowOperator() { |
||
106 | |||
107 | /** |
||
108 | * @param string $value |
||
109 | */ |
||
110 | public function setTableName($value) { |
||
113 | |||
114 | /** |
||
115 | * Gets entity's table name |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | public function getTableName() { |
||
122 | |||
123 | /** |
||
124 | * @param string $value |
||
125 | */ |
||
126 | public function setIdFieldName($value) { |
||
129 | |||
130 | /** |
||
131 | * Gets entity's DB ID field name (which is unique within entity set) |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | public function getIdFieldName() { |
||
138 | |||
139 | |||
140 | |||
141 | |||
142 | |||
143 | /** |
||
144 | * @param array $array |
||
145 | * |
||
146 | * @return \EntityContainer |
||
147 | */ |
||
148 | public function fromArray($array) { |
||
157 | |||
158 | |||
159 | /** |
||
160 | * Exports object properties to DB row state WITHOUT ID |
||
161 | * |
||
162 | * Useful for INSERT operations |
||
163 | * |
||
164 | * @param \EntityContainer $cEntity |
||
165 | * @return array |
||
166 | */ |
||
167 | public function exportRow($cEntity) { |
||
170 | |||
171 | /** |
||
172 | * Exports object properties to DB row state with ID |
||
173 | * |
||
174 | * @param \EntityContainer $cEntity |
||
175 | * @return array |
||
176 | */ |
||
177 | public function exportRowNoId($cEntity) { |
||
184 | |||
185 | |||
186 | /** |
||
187 | * @return \EntityContainer |
||
188 | */ |
||
189 | public function getContainer() { |
||
198 | |||
199 | |||
200 | /** |
||
201 | * @param mixed $dbId |
||
202 | * |
||
203 | * @return \EntityContainer|false |
||
204 | */ |
||
205 | public function loadById($dbId) { |
||
215 | |||
216 | } |
||
217 |
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.