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 Association_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 Association_Field, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Association_Field extends Field { |
||
16 | |||
17 | /** |
||
18 | * WP_Toolset instance for WP data loading |
||
19 | * |
||
20 | * @var Carbon_Fields\Toolset\WP_Toolset |
||
21 | */ |
||
22 | protected $wp_toolset; |
||
23 | |||
24 | /** |
||
25 | * Min number of selected items allowed. -1 for no limit |
||
26 | * |
||
27 | * @var integer |
||
28 | */ |
||
29 | protected $min = -1; |
||
30 | |||
31 | /** |
||
32 | * Max number of selected items allowed. -1 for no limit |
||
33 | * |
||
34 | * @var integer |
||
35 | */ |
||
36 | protected $max = -1; |
||
37 | |||
38 | /** |
||
39 | * Allow items to be added multiple times |
||
40 | * |
||
41 | * @var boolean |
||
42 | */ |
||
43 | protected $duplicates_allowed = false; |
||
44 | |||
45 | /** |
||
46 | * Default field value |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $default_value = array(); |
||
51 | |||
52 | /** |
||
53 | * Types of entries to associate with. |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $types = array( |
||
57 | array( |
||
58 | 'type' => 'post', |
||
59 | 'post_type' => 'post', |
||
60 | ), |
||
61 | ); |
||
62 | |||
63 | /** |
||
64 | * Create a field from a certain type with the specified label. |
||
65 | * |
||
66 | * @param string $type Field type |
||
67 | * @param string $name Field name |
||
68 | * @param string $label Field label |
||
69 | */ |
||
70 | public function __construct( $type, $name, $label ) { |
||
75 | |||
76 | /** |
||
77 | * Load the field value from an input array based on it's name |
||
78 | * |
||
79 | * @param array $input Array of field names and values. |
||
80 | */ |
||
81 | public function set_value_from_input( $input ) { |
||
91 | |||
92 | /** |
||
93 | * Alias for $this->get_value_set()->set( $value ); |
||
94 | */ |
||
95 | public function set_value( $value ) { |
||
99 | |||
100 | /** |
||
101 | * Get value string for legacy value |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | protected function get_value_string_for_legacy_value( $legacy_value ) { |
||
119 | |||
120 | /** |
||
121 | * Convert a colo:separated:string into it's expected components |
||
122 | * Used for backwards compatibility to CF 1.5 |
||
123 | * |
||
124 | * @param string $value_string |
||
125 | * @return array |
||
126 | */ |
||
127 | protected function value_string_to_property_array( $value_string ) { |
||
146 | |||
147 | /** |
||
148 | * Convert a colon:separated:string into it's expected components |
||
149 | * Used for backwards compatibility to CF 1.5 |
||
150 | * |
||
151 | * @param array $value_string_array |
||
152 | * @return array<array> |
||
153 | */ |
||
154 | protected function value_string_array_to_value_set( $value_string_array ) { |
||
177 | |||
178 | /** |
||
179 | * Used to get the title of an item. |
||
180 | * |
||
181 | * Can be overriden or extended by the `carbon_association_title` filter. |
||
182 | * |
||
183 | * @param int $id The database ID of the item. |
||
184 | * @param string $type Item type (post, term, user, comment, or a custom one). |
||
185 | * @param string $subtype The subtype - "page", "post", "category", etc. |
||
186 | * @return string $title The title of the item. |
||
187 | */ |
||
188 | protected function get_title_by_type( $id, $type, $subtype = '' ) { |
||
221 | |||
222 | /** |
||
223 | * Used to get the label of an item. |
||
224 | * |
||
225 | * Can be overriden or extended by the `carbon_association_item_label` filter. |
||
226 | * |
||
227 | * @param int $id The database ID of the item. |
||
228 | * @param string $type Item type (post, term, user, comment, or a custom one). |
||
229 | * @param string $subtype Subtype - "page", "post", "category", etc. |
||
230 | * @return string $label The label of the item. |
||
231 | */ |
||
232 | protected function get_item_label( $id, $type, $subtype = '' ) { |
||
254 | |||
255 | /** |
||
256 | * Get post options |
||
257 | * |
||
258 | * @return array $options |
||
259 | */ |
||
260 | protected function get_post_options( $type ) { |
||
289 | |||
290 | /** |
||
291 | * Get term options |
||
292 | * |
||
293 | * @return array $options |
||
294 | */ |
||
295 | protected function get_term_options( $type ) { |
||
322 | |||
323 | /** |
||
324 | * Get user options |
||
325 | * |
||
326 | * @return array $options |
||
327 | */ |
||
328 | View Code Duplication | protected function get_user_options( $type ) { |
|
354 | |||
355 | /** |
||
356 | * Get comment options |
||
357 | * |
||
358 | * @return array $options |
||
359 | */ |
||
360 | View Code Duplication | protected function get_comment_options( $type ) { |
|
386 | |||
387 | /** |
||
388 | * Generate the item options. |
||
389 | * |
||
390 | * @return array $options The selectable options of the association field. |
||
391 | */ |
||
392 | public function get_options() { |
||
413 | |||
414 | /** |
||
415 | * Retrieve the edit link of a particular object. |
||
416 | * |
||
417 | * @param string $type Object type. |
||
418 | * @param int $id ID of the object. |
||
419 | * @return string URL of the edit link. |
||
420 | */ |
||
421 | protected function get_object_edit_link( $type, $id ) { |
||
447 | |||
448 | /** |
||
449 | * Modify the types. |
||
450 | * @param array $types New types |
||
451 | */ |
||
452 | public function set_types( $types ) { |
||
456 | |||
457 | /** |
||
458 | * Get the minimum allowed number of selected entries. |
||
459 | * |
||
460 | * @return int |
||
461 | */ |
||
462 | public function get_min() { |
||
465 | |||
466 | /** |
||
467 | * Set the minimum allowed number of selected entries. |
||
468 | * |
||
469 | * @param int $min |
||
470 | */ |
||
471 | public function set_min( $min ) { |
||
475 | |||
476 | /** |
||
477 | * Get the maximum allowed number of selected entries. |
||
478 | * |
||
479 | * @return int |
||
480 | */ |
||
481 | public function get_max() { |
||
484 | |||
485 | /** |
||
486 | * Set the maximum allowed number of selected entries. |
||
487 | * |
||
488 | * @param int $max |
||
489 | */ |
||
490 | public function set_max( $max ) { |
||
494 | |||
495 | /** |
||
496 | * Get whether entry duplicates are allowed. |
||
497 | * |
||
498 | * @return boolean |
||
499 | */ |
||
500 | public function get_duplicates_allowed() { |
||
503 | |||
504 | /** |
||
505 | * Set whether entry duplicates are allowed. |
||
506 | * |
||
507 | * @param boolean $allowed |
||
508 | */ |
||
509 | public function set_duplicates_allowed( $allowed ) { |
||
513 | |||
514 | /** |
||
515 | * Specify whether to allow each entry to be selected multiple times. |
||
516 | * Backwards-compatibility alias. |
||
517 | * |
||
518 | * @param boolean $allow |
||
519 | */ |
||
520 | public function allow_duplicates( $allow = true ) { |
||
523 | |||
524 | /** |
||
525 | * Converts the field values into a usable associative array. |
||
526 | * |
||
527 | * The association data is saved in the database in the following format: |
||
528 | * array ( |
||
529 | * 0 => 'post:page:4', |
||
530 | * 1 => 'term:category:2', |
||
531 | * 2 => 'user:user:1', |
||
532 | * ) |
||
533 | * where the value of each array item contains: |
||
534 | * - Type of data (post, term, user or comment) |
||
535 | * - Subtype of data (the particular post type or taxonomy) |
||
536 | * - ID of the item (the database ID of the item) |
||
537 | */ |
||
538 | protected function value_to_json() { |
||
554 | |||
555 | /** |
||
556 | * Convert the field data into JSON representation. |
||
557 | * @param bool $load Whether to load data from the datastore. |
||
558 | * @return mixed The JSON field data. |
||
559 | */ |
||
560 | public function to_json( $load ) { |
||
573 | } |
||
574 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.