Complex classes like WPSC_Purchase_Log_Notes 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 WPSC_Purchase_Log_Notes, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class WPSC_Purchase_Log_Notes extends WPSC_Query_Base implements Iterator { |
||
4 | const TYPE_DEFAULT = 0; |
||
5 | const TYPE_ERROR = 1; |
||
6 | const STATUS_PUBLIC = 0; |
||
7 | const STATUS_PRIVATE = 1; |
||
8 | const KEY_CONTENT = 0; |
||
9 | const KEY_STATUS = 1; |
||
10 | const KEY_TIME = 2; |
||
11 | const KEY_TYPE = 3; |
||
12 | |||
13 | protected static $map_types = array( |
||
14 | self::TYPE_DEFAULT => 'default', |
||
15 | self::TYPE_ERROR => 'error', |
||
16 | ); |
||
17 | |||
18 | protected static $map_statuses = array( |
||
19 | self::STATUS_PUBLIC => 'public', |
||
20 | self::STATUS_PRIVATE => 'private', |
||
21 | ); |
||
22 | |||
23 | protected static $map_keys = array( |
||
24 | self::KEY_TYPE => 'type', |
||
25 | self::KEY_STATUS => 'status', |
||
26 | self::KEY_TIME => 'time', |
||
27 | self::KEY_CONTENT => 'content', |
||
28 | ); |
||
29 | |||
30 | protected static $map_text = array(); |
||
31 | protected $log = null; |
||
32 | |||
33 | public function __construct( $log ) { |
||
53 | |||
54 | /** |
||
55 | * Fetches the actual record from the database |
||
56 | * |
||
57 | * @access protected |
||
58 | * @since 3.8.9 |
||
59 | * |
||
60 | * @return WPSC_Purchase_Log |
||
61 | */ |
||
62 | protected function fetch() { |
||
172 | |||
173 | /** |
||
174 | * Prepares the return value for get() (apply_filters, etc). |
||
175 | * |
||
176 | * @access protected |
||
177 | * @since 4.0 |
||
178 | * |
||
179 | * @param mixed $value Value fetched |
||
180 | * @param string $key Key for $data. |
||
181 | * |
||
182 | * @return mixed |
||
183 | */ |
||
184 | protected function prepare_get( $value, $key ) { |
||
187 | |||
188 | /** |
||
189 | * Prepares the return value for get_data() (apply_filters, etc). |
||
190 | * |
||
191 | * @access protected |
||
192 | * @since 4.0 |
||
193 | * |
||
194 | * @return mixed |
||
195 | */ |
||
196 | protected function prepare_get_data() { |
||
199 | |||
200 | public function add( $note_args ) { |
||
207 | |||
208 | public function remove( $note_id ) { |
||
214 | |||
215 | /** |
||
216 | * Sets a property to a certain value. This function accepts a key and a value |
||
217 | * as arguments, or an associative array containing key value pairs. |
||
218 | * |
||
219 | * @access public |
||
220 | * @since 4.0 |
||
221 | * |
||
222 | * @param mixed $key Name of the property (column), or an array containing |
||
223 | * key value pairs |
||
224 | * @param string|int|null $value Optional. Defaults to null. In case $key is a string, |
||
225 | * this should be specified. |
||
226 | * @return WPSC_Query_Base The current object (for method chaining) |
||
|
|||
227 | */ |
||
228 | public function set( $key, $value = null ) { |
||
249 | |||
250 | /** |
||
251 | * Saves the object back to the database. |
||
252 | * |
||
253 | * @access public |
||
254 | * @since 4.0 |
||
255 | * |
||
256 | * @return mixed |
||
257 | */ |
||
258 | public function save() { |
||
311 | |||
312 | /** |
||
313 | * Merge arguments into defaults array. |
||
314 | * |
||
315 | * @since 4.0 |
||
316 | * |
||
317 | * @param array $args Value to merge with defaults. |
||
318 | * @return array Merged arguments with defaults. |
||
319 | */ |
||
320 | public static function parse_args_for_db( array $args ) { |
||
328 | |||
329 | /** |
||
330 | * Merge arguments into defaults array. |
||
331 | * |
||
332 | * @since 4.0 |
||
333 | * |
||
334 | * @param array $args Value to merge with defaults. |
||
335 | * @return array Merged arguments with defaults. |
||
336 | */ |
||
337 | public static function parse_args( array $args ) { |
||
345 | |||
346 | /** |
||
347 | * Get current for Iterator. |
||
348 | * |
||
349 | * @since 4.0 |
||
350 | * |
||
351 | * @return mixed |
||
352 | */ |
||
353 | public function current() { |
||
357 | |||
358 | /** |
||
359 | * Get key for Iterator. |
||
360 | * |
||
361 | * @since 4.0 |
||
362 | * |
||
363 | * @return scalar |
||
364 | */ |
||
365 | public function key() { |
||
369 | |||
370 | /** |
||
371 | * Get next for Iterator. |
||
372 | * |
||
373 | * @since 4.0 |
||
374 | * |
||
375 | * @return void |
||
376 | */ |
||
377 | public function next() { |
||
381 | |||
382 | /** |
||
383 | * Get prev for Iterator. |
||
384 | * |
||
385 | * @since 4.0 |
||
386 | * |
||
387 | * @return void |
||
388 | */ |
||
389 | public function prev() { |
||
393 | |||
394 | /** |
||
395 | * Get rewind for Iterator. |
||
396 | * |
||
397 | * @since 4.0 |
||
398 | * |
||
399 | * @return void |
||
400 | */ |
||
401 | public function rewind() { |
||
405 | |||
406 | /** |
||
407 | * Get valid for Iterator. |
||
408 | * |
||
409 | * @since 4.0 |
||
410 | * |
||
411 | * @return boolean |
||
412 | */ |
||
413 | public function valid() { |
||
417 | |||
418 | |||
419 | public function get_status_text( $note = array() ) { |
||
430 | |||
431 | public function get_formatted_date( $note = array() ) { |
||
446 | |||
447 | } |
||
448 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.