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 | /** |
||
10 | * Represents a classname selector, which respects obsolete clasess. |
||
11 | * |
||
12 | * @package framework |
||
13 | * @subpackage model |
||
14 | */ |
||
15 | class DBClassName extends DBEnum { |
||
16 | |||
17 | /** |
||
18 | * Base classname of class to enumerate. |
||
19 | * If 'DataObject' then all classes are included. |
||
20 | * If empty, then the baseClass of the parent object will be used |
||
21 | * |
||
22 | * @var string|null |
||
23 | */ |
||
24 | protected $baseClass = null; |
||
25 | |||
26 | /** |
||
27 | * Parent object |
||
28 | * |
||
29 | * @var DataObject|null |
||
30 | */ |
||
31 | protected $record = null; |
||
32 | |||
33 | /** |
||
34 | * Classname spec cache for obsolete classes. The top level keys are the table, each of which contains |
||
35 | * nested arrays with keys mapped to field names. The values of the lowest level array are the classnames |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected static $classname_cache = array(); |
||
40 | |||
41 | /** |
||
42 | * Clear all cached classname specs. It's necessary to clear all cached subclassed names |
||
43 | * for any classes if a new class manifest is generated. |
||
44 | */ |
||
45 | public static function clear_classname_cache() { |
||
48 | |||
49 | /** |
||
50 | * Create a new DBClassName field |
||
51 | * |
||
52 | * @param string $name Name of field |
||
53 | * @param string|null $baseClass Optional base class to limit selections |
||
54 | */ |
||
55 | public function __construct($name = null, $baseClass = null) { |
||
59 | |||
60 | /** |
||
61 | * @return void |
||
62 | */ |
||
63 | View Code Duplication | public function requireField() { |
|
81 | |||
82 | /** |
||
83 | * Get the base dataclass for the list of subclasses |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | public function getBaseClass() { |
||
104 | |||
105 | /** |
||
106 | * Assign the base class |
||
107 | * |
||
108 | * @param string $baseClass |
||
109 | * @return $this |
||
110 | */ |
||
111 | public function setBaseClass($baseClass) { |
||
115 | |||
116 | /** |
||
117 | * Given a table name, find the base data class |
||
118 | * |
||
119 | * @param string $table |
||
120 | * @return string|null |
||
121 | */ |
||
122 | protected function getClassNameFromTable($table) { |
||
137 | |||
138 | /** |
||
139 | * Get list of classnames that should be selectable |
||
140 | * |
||
141 | * @return array |
||
142 | */ |
||
143 | public function getEnum() { |
||
148 | |||
149 | /** |
||
150 | * Get the list of classnames, including obsolete classes. |
||
151 | * |
||
152 | * If table or name are not set, or if it is not a valid field on the given table, |
||
153 | * then only known classnames are returned. |
||
154 | * |
||
155 | * Values cached in this method can be cleared via `DBClassName::clear_classname_cache();` |
||
156 | * |
||
157 | * @return array |
||
158 | */ |
||
159 | public function getEnumObsolete() { |
||
188 | |||
189 | View Code Duplication | public function setValue($value, $record = null, $markChanged = true) { |
|
196 | |||
197 | public function getDefault() { |
||
198 | // Check for assigned default |
||
199 | $default = parent::getDefault(); |
||
200 | if($default) { |
||
201 | return $default; |
||
202 | } |
||
203 | |||
209 |
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.