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:
Complex classes like Ajde_Crud_Field often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Ajde_Crud_Field, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | abstract class Ajde_Crud_Field extends Ajde_Object_Standard |
||
4 | { |
||
5 | /** |
||
6 | * @var Ajde_Crud |
||
7 | */ |
||
8 | protected $_crud; |
||
9 | |||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | protected $_type; |
||
14 | |||
15 | protected $_useSpan = 12; |
||
16 | protected $_attributes = []; |
||
17 | |||
18 | public function __construct(Ajde_Crud $crud, $fieldOptions) |
||
45 | |||
46 | protected function prepare() |
||
49 | |||
50 | /** |
||
51 | * Getters and setters. |
||
52 | */ |
||
53 | public function getName() |
||
57 | |||
58 | public function getDbType() |
||
62 | |||
63 | public function getLabel() |
||
67 | |||
68 | public function getLength() |
||
72 | |||
73 | public function getIsRequired() |
||
77 | |||
78 | public function getDefault() |
||
82 | |||
83 | public function getIsAutoIncrement() |
||
87 | |||
88 | public function getIsAutoUpdate() |
||
92 | |||
93 | public function getIsUnique() |
||
97 | |||
98 | public function getValue() |
||
106 | |||
107 | public function getType() |
||
111 | |||
112 | /** |
||
113 | * Template functions. |
||
114 | */ |
||
115 | public function getHtml() |
||
122 | |||
123 | public function getInput($id = null) |
||
131 | |||
132 | protected function _getFieldTemplate() |
||
136 | |||
137 | protected function _getInputTemplate() |
||
141 | |||
142 | protected function _getTemplate($action) |
||
161 | |||
162 | View Code Duplication | protected function _hasCustomTemplate($action) |
|
169 | |||
170 | protected function _getCustomTemplateBase() |
||
174 | |||
175 | protected function _getCustomTemplateAction($action) |
||
179 | |||
180 | /** |
||
181 | * HTML functions. |
||
182 | */ |
||
183 | public function getHtmlRequired() |
||
188 | |||
189 | public function getHtmlPK() |
||
193 | |||
194 | public function getHtmlAttributesAsArray() |
||
224 | |||
225 | public function getHtmlAttribute($name) |
||
234 | |||
235 | public function getHtmlAttributes() |
||
248 | |||
249 | /** |
||
250 | * @return Ajde_Crud |
||
251 | */ |
||
252 | public function getCrud() |
||
256 | } |
||
257 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: