Complex classes like 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 Field, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Field |
||
6 | { |
||
7 | /** |
||
8 | * @var array |
||
9 | */ |
||
10 | protected $args; |
||
11 | |||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | protected $dependencies; |
||
16 | |||
17 | public function __construct() |
||
22 | |||
23 | /** |
||
24 | * @param string $property |
||
25 | * |
||
26 | * @return mixed |
||
27 | * @throws Exception |
||
28 | */ |
||
29 | public function __get( $property ) |
||
38 | |||
39 | |||
40 | /** |
||
41 | * Get a specific Field |
||
42 | * |
||
43 | * @return mixed GeminiLabs\SiteReviews\Html\Fields\* |
||
44 | */ |
||
45 | public function getField( array $args = [] ) |
||
59 | |||
60 | /** |
||
61 | * Normalize the field arguments |
||
62 | * |
||
63 | * @return $this |
||
64 | */ |
||
65 | public function normalize( array $args = [] ) |
||
109 | |||
110 | /** |
||
111 | * Render the field |
||
112 | * |
||
113 | * @param mixed $print |
||
114 | * |
||
115 | * @return string|void |
||
116 | */ |
||
117 | public function render( $print = true ) |
||
153 | |||
154 | /** |
||
155 | * Reset the Field |
||
156 | * |
||
157 | * @return self |
||
158 | */ |
||
159 | public function reset() |
||
165 | |||
166 | /** |
||
167 | * Check for form submission field errors |
||
168 | * |
||
169 | * @return void |
||
170 | */ |
||
171 | protected function checkForErrors( array $atts ) |
||
188 | |||
189 | /** |
||
190 | * Parse the field attributes and convert to an array if needed |
||
191 | * |
||
192 | * @return array |
||
193 | */ |
||
194 | protected function parseAttributes( array $args ) |
||
212 | |||
213 | /** |
||
214 | * Parse the field ID from the field path |
||
215 | * |
||
216 | * @return null|string |
||
217 | */ |
||
218 | protected function parseId( array $args ) |
||
226 | |||
227 | /** |
||
228 | * Parse the field inline |
||
229 | * |
||
230 | * @return bool |
||
231 | */ |
||
232 | protected function parseInline( array $args ) |
||
238 | |||
239 | /** |
||
240 | * Parse the field name |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | protected function parseName( array $args ) |
||
259 | |||
260 | /** |
||
261 | * Parse the field prefix |
||
262 | * |
||
263 | * @return string|false |
||
264 | */ |
||
265 | protected function parsePrefix( array $args ) |
||
269 | |||
270 | /** |
||
271 | * Parse the field type |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | protected function parseType( array $args ) |
||
283 | |||
284 | /** |
||
285 | * Parse the field value |
||
286 | * |
||
287 | * @return string |
||
288 | */ |
||
289 | protected function parseValue( array $args ) |
||
304 | |||
305 | /** |
||
306 | * Get the [data-depends] attribute |
||
307 | * |
||
308 | * @return array|null |
||
309 | */ |
||
310 | public function getDataDepends() |
||
314 | |||
315 | /** |
||
316 | * Set the field value |
||
317 | * |
||
318 | * @return self |
||
319 | */ |
||
320 | public function setValue() |
||
324 | |||
325 | /** |
||
326 | * Set the [data-depends] attribute |
||
327 | * |
||
328 | * @return null|array |
||
329 | */ |
||
330 | protected function setDataDepends() |
||
353 | } |
||
354 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.