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 |
||
18 | class Material extends \samson\activerecord\material |
||
19 | { |
||
20 | /** Store entity name */ |
||
21 | const ENTITY = __CLASS__; |
||
22 | |||
23 | /** Entity field names constants for using in code */ |
||
24 | const F_PRIMARY = 'MaterialID'; |
||
25 | const F_IDENTIFIER = 'Url'; |
||
26 | const F_DELETION = 'Active'; |
||
27 | const F_PUBLISHED = 'Published'; |
||
28 | const F_PARENT = 'parent_id'; |
||
29 | const F_PRIORITY = 'priority'; |
||
30 | |||
31 | /** @var integer Primary field */ |
||
32 | public $MaterialID; |
||
33 | |||
34 | /** @var string Unique identifier */ |
||
35 | public $Url; |
||
36 | |||
37 | /** @var bool Internal existence flag */ |
||
38 | public $Active; |
||
39 | |||
40 | /** @var bool Published flag */ |
||
41 | public $Published; |
||
42 | |||
43 | /** @var integer Parent material identifier */ |
||
44 | public $parent_id; |
||
45 | |||
46 | /** @var integer Priority inside material relation */ |
||
47 | public $priority; |
||
48 | |||
49 | /** |
||
50 | * Get material entity by URL(s). |
||
51 | * |
||
52 | * @param QueryInterface $query Object for performing database queries |
||
53 | * @param array|string $url Material URL or collection of material URLs |
||
54 | * @param self|array|null $return Variable where request result would be returned |
||
55 | * @return bool|self True if material entities has been found |
||
56 | */ |
||
57 | View Code Duplication | public static function byUrl(QueryInterface $query, $url, & $return = array()) |
|
68 | |||
69 | /** |
||
70 | * Set additional material field value by field identifier |
||
71 | * @param string $fieldID Field identifier |
||
72 | * @param string $value Value to be stored |
||
73 | * @param string $locale Locale identifier |
||
74 | */ |
||
75 | public function setFieldByID($fieldID, $value, $locale = DEFAULT_LOCALE) |
||
102 | |||
103 | /** |
||
104 | * Get select additional field text value. |
||
105 | * |
||
106 | * @param string $fieldID Field identifier |
||
107 | * @return string Select field text |
||
108 | */ |
||
109 | public function selectText($fieldID) |
||
124 | |||
125 | /** |
||
126 | * Get collection of images for material by gallery additional field selector. If none is passed |
||
127 | * all images from gallery table would be returned for this material entity. |
||
128 | * |
||
129 | * @param string|null $fieldSelector Additional field selector value |
||
130 | * @param string $selector Additional field field name to search for |
||
131 | * @return \samsonframework\orm\RecordInterface[] Collection of images in this gallery additional field for material |
||
132 | */ |
||
133 | public function &gallery($fieldSelector = null, $selector = 'FieldID') |
||
165 | |||
166 | /** |
||
167 | * Copy this material related entities. |
||
168 | * |
||
169 | * @param QueryInterface $query Database query instance |
||
170 | * @param string $entity Entity identifier |
||
171 | * @param string $newIdentifier Copied material idetifier |
||
172 | * @param array $excludedIDs Collection of related entity identifier to exclude from copying |
||
173 | */ |
||
174 | protected function copyRelatedEntity(QueryInterface $query, $entity, $newIdentifier, $excludedIDs = array()) |
||
189 | |||
190 | /** |
||
191 | * Create copy of current object. |
||
192 | * |
||
193 | * @param mixed $clone Material for cloning |
||
194 | * @param array $excludedFields Additional fields identifiers not copied |
||
195 | * @returns self New copied instance |
||
196 | */ |
||
197 | public function ©(&$clone = null, $excludedFields = array()) |
||
211 | } |
||
212 |
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.