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 Container 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 Container, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | abstract class Container implements Datastore_Holder_Interface { |
||
19 | /** |
||
20 | * Where to put a particular tab -- at the head or the tail. Tail by default |
||
21 | */ |
||
22 | const TABS_TAIL = 1; |
||
23 | const TABS_HEAD = 2; |
||
24 | |||
25 | /** |
||
26 | * Separator signifying field hierarchy relation |
||
27 | * Used when searching for fields in a specific complex field |
||
28 | */ |
||
29 | const HIERARCHY_FIELD_SEPARATOR = '/'; |
||
30 | |||
31 | /** |
||
32 | * Separator signifying complex_field->group relation |
||
33 | * Used when searching for fields in a specific complex field group |
||
34 | */ |
||
35 | const HIERARCHY_GROUP_SEPARATOR = ':'; |
||
36 | |||
37 | /** |
||
38 | * Stores if the container is active on the current page |
||
39 | * |
||
40 | * @see activate() |
||
41 | * @var bool |
||
42 | */ |
||
43 | protected $active = false; |
||
44 | |||
45 | /** |
||
46 | * List of registered unique field names for this container instance |
||
47 | * |
||
48 | * @see register_field_name() |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $registered_field_names = array(); |
||
52 | |||
53 | /** |
||
54 | * Tabs available |
||
55 | */ |
||
56 | protected $tabs = array(); |
||
57 | |||
58 | /** |
||
59 | * List of default container settings |
||
60 | * |
||
61 | * @see init() |
||
62 | * @var array |
||
63 | */ |
||
64 | public $settings = array(); |
||
65 | |||
66 | /** |
||
67 | * Title of the container |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | public $title = ''; |
||
72 | |||
73 | /** |
||
74 | * List of notification messages to be displayed on the front-end |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $notifications = array(); |
||
79 | |||
80 | /** |
||
81 | * List of error messages to be displayed on the front-end |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | protected $errors = array(); |
||
86 | |||
87 | /** |
||
88 | * List of container fields |
||
89 | * |
||
90 | * @see add_fields() |
||
91 | * @var array |
||
92 | */ |
||
93 | protected $fields = array(); |
||
94 | |||
95 | /** |
||
96 | * Array of custom CSS classes. |
||
97 | * |
||
98 | * @see set_classes() |
||
99 | * @see get_classes() |
||
100 | * @var array<string> |
||
101 | */ |
||
102 | protected $classes = array(); |
||
103 | |||
104 | /** |
||
105 | * Container datastores. Propagated to all container fields |
||
106 | * |
||
107 | * @see set_datastore() |
||
108 | * @see get_datastore() |
||
109 | * @var object |
||
110 | */ |
||
111 | protected $datastore; |
||
112 | |||
113 | /** |
||
114 | * Flag whether the datastore is the default one or replaced with a custom one |
||
115 | * |
||
116 | * @see set_datastore() |
||
117 | * @see get_datastore() |
||
118 | * @var boolean |
||
119 | */ |
||
120 | protected $has_default_datastore = true; |
||
121 | |||
122 | /** |
||
123 | * Fulfillable_Collection to use when checking attachment/saving conditions |
||
124 | * |
||
125 | * @var Fulfillable_Collection |
||
126 | */ |
||
127 | protected $condition_collection; |
||
128 | |||
129 | /** |
||
130 | * Translator to use when translating conditions to json |
||
131 | * |
||
132 | * @var Carbon_Fields\Container\Fulfillable\Translator\Translator |
||
133 | */ |
||
134 | protected $condition_translator; |
||
135 | |||
136 | /** |
||
137 | * Create a new container of type $type and name $name. |
||
138 | * |
||
139 | * @param string $raw_type |
||
140 | * @param string $id Unique id for the container. Optional |
||
141 | * @param string $name Human-readable name of the container |
||
142 | * @return object $container |
||
143 | */ |
||
144 | 8 | public static function factory( $raw_type, $id, $name = '' ) { |
|
193 | |||
194 | /** |
||
195 | * An alias of factory(). |
||
196 | * |
||
197 | * @see Container::factory() |
||
198 | */ |
||
199 | public static function make( $type, $name ) { |
||
202 | |||
203 | /** |
||
204 | * Create a new container |
||
205 | * |
||
206 | * @param string $id Unique id of the container |
||
207 | * @param string $title Title of the container |
||
208 | * @param string $type Type of the container |
||
209 | * @param Fulfillable_Collection $condition_collection |
||
210 | * @param Carbon_Fields\Container\Fulfillable\Translator\Translator $condition_translator |
||
211 | */ |
||
212 | 2 | public function __construct( $id, $title, $type, $condition_collection, $condition_translator ) { |
|
229 | |||
230 | /** |
||
231 | * Return the container id |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | public function get_id() { |
||
238 | |||
239 | /** |
||
240 | * Get array of all static condition types |
||
241 | * |
||
242 | * @param boolean $static |
||
243 | * @return array<string> |
||
244 | */ |
||
245 | protected function get_condition_types( $static ) { |
||
255 | |||
256 | /** |
||
257 | * Return whether the container is active |
||
258 | */ |
||
259 | public function is_active() { |
||
262 | |||
263 | /** |
||
264 | * Activate the container and trigger an action |
||
265 | */ |
||
266 | protected function activate() { |
||
276 | |||
277 | /** |
||
278 | * Perform instance initialization |
||
279 | */ |
||
280 | abstract public function init(); |
||
281 | |||
282 | /** |
||
283 | * Boot the container once it's attached. |
||
284 | */ |
||
285 | protected function boot() { |
||
288 | |||
289 | /** |
||
290 | * Load the value for each field in the container. |
||
291 | * Could be used internally during container rendering |
||
292 | */ |
||
293 | public function load() { |
||
298 | |||
299 | /** |
||
300 | * Called first as part of the container save procedure. |
||
301 | * Responsible for checking the request validity and |
||
302 | * calling the container-specific save() method |
||
303 | * |
||
304 | * @see save() |
||
305 | * @see is_valid_save() |
||
306 | */ |
||
307 | public function _save() { |
||
313 | |||
314 | /** |
||
315 | * Load submitted data and save each field in the container |
||
316 | * |
||
317 | * @see is_valid_save() |
||
318 | */ |
||
319 | public function save( $data = null ) { |
||
325 | |||
326 | /** |
||
327 | * Checks whether the current save request is valid |
||
328 | * |
||
329 | * @return bool |
||
330 | */ |
||
331 | final protected function _is_valid_save() { |
||
336 | |||
337 | /** |
||
338 | * Checks whether the current save request is valid |
||
339 | * |
||
340 | * @return bool |
||
341 | */ |
||
342 | abstract protected function is_valid_save(); |
||
343 | |||
344 | /** |
||
345 | * Called first as part of the container attachment procedure. |
||
346 | * Responsible for checking it's OK to attach the container |
||
347 | * and if it is, calling the container-specific attach() method |
||
348 | * |
||
349 | * @see attach() |
||
350 | * @see is_valid_attach() |
||
351 | */ |
||
352 | public function _attach() { |
||
365 | |||
366 | /** |
||
367 | * Attach the container rendering and helping methods |
||
368 | * to concrete WordPress Action hooks |
||
369 | */ |
||
370 | public function attach() {} |
||
371 | |||
372 | /** |
||
373 | * Perform checks whether the container should be attached during the current request |
||
374 | * |
||
375 | * @return bool True if the container is allowed to be attached |
||
376 | */ |
||
377 | final public function is_valid_attach() { |
||
381 | |||
382 | /** |
||
383 | * Get environment array for page request (in admin) |
||
384 | * |
||
385 | * @return array |
||
386 | */ |
||
387 | abstract protected function get_environment_for_request(); |
||
388 | |||
389 | /** |
||
390 | * Check container attachment rules against current page request (in admin) |
||
391 | * |
||
392 | * @return bool |
||
393 | */ |
||
394 | abstract protected function is_valid_attach_for_request(); |
||
395 | |||
396 | /** |
||
397 | * Check if conditions pass for request |
||
398 | * |
||
399 | * @return bool |
||
400 | */ |
||
401 | protected function static_conditions_pass() { |
||
409 | |||
410 | /** |
||
411 | * Get environment array for object id |
||
412 | * |
||
413 | * @param integer $object_id |
||
414 | * @return array |
||
415 | */ |
||
416 | abstract protected function get_environment_for_object( $object_id ); |
||
417 | |||
418 | /** |
||
419 | * Check container attachment rules against object id |
||
420 | * |
||
421 | * @param int $object_id |
||
422 | * @return bool |
||
423 | */ |
||
424 | abstract public function is_valid_attach_for_object( $object_id ); |
||
425 | |||
426 | /** |
||
427 | * Check if all conditions pass for object |
||
428 | * |
||
429 | * @return bool |
||
430 | */ |
||
431 | protected function all_conditions_pass( $object_id ) { |
||
435 | |||
436 | /** |
||
437 | * Whether this container is currently viewed. |
||
438 | */ |
||
439 | public function should_activate() { |
||
442 | |||
443 | /** |
||
444 | * Perform a check whether the current container has fields |
||
445 | * |
||
446 | * @return bool |
||
447 | */ |
||
448 | public function has_fields() { |
||
451 | |||
452 | /** |
||
453 | * Returns the private container array of fields. |
||
454 | * Use only if you are completely aware of what you are doing. |
||
455 | * |
||
456 | * @return array |
||
457 | */ |
||
458 | public function get_fields() { |
||
461 | |||
462 | /** |
||
463 | * Return root field from container with specified name |
||
464 | * |
||
465 | * @example crb_complex |
||
466 | * |
||
467 | * @param string $field_name |
||
468 | * @return Field |
||
469 | */ |
||
470 | public function get_root_field_by_name( $field_name ) { |
||
479 | |||
480 | /** |
||
481 | * Get a regex to match field name patterns used to fetch specific fields |
||
482 | * |
||
483 | * @return string |
||
484 | */ |
||
485 | protected function get_field_pattern_regex() { |
||
500 | |||
501 | /** |
||
502 | * Return field from container with specified name |
||
503 | * |
||
504 | * @example $field_name = 'crb_complex/text_field' |
||
505 | * @example $field_name = 'crb_complex/complex_2' |
||
506 | * @example $field_name = 'crb_complex/complex_2:text_group/text_field' |
||
507 | * @example $field_name = 'crb_complex[3]/complex_2[1]:text_group/text_field' |
||
508 | * |
||
509 | * @param string $field_name |
||
510 | * @return Field |
||
511 | */ |
||
512 | public function get_field_by_name( $field_name ) { |
||
558 | |||
559 | /** |
||
560 | * Perform checks whether there is a field registered with the name $name. |
||
561 | * If not, the field name is recorded. |
||
562 | * |
||
563 | * @param string $name |
||
564 | * @return boolean |
||
565 | */ |
||
566 | View Code Duplication | protected function register_field_name( $name ) { |
|
575 | |||
576 | /** |
||
577 | * Return whether the datastore instance is the default one or has been overriden |
||
578 | * |
||
579 | * @return boolean |
||
580 | */ |
||
581 | 6 | public function has_default_datastore() { |
|
584 | |||
585 | /** |
||
586 | * Set datastore instance |
||
587 | * |
||
588 | * @param Datastore_Interface $datastore |
||
589 | * @return object $this |
||
590 | */ |
||
591 | 6 | View Code Duplication | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
603 | |||
604 | /** |
||
605 | * Get the DataStore instance |
||
606 | * |
||
607 | * @return Datastore_Interface $datastore |
||
608 | */ |
||
609 | 6 | public function get_datastore() { |
|
612 | |||
613 | /** |
||
614 | * Return WordPress nonce name used to identify the current container instance |
||
615 | * |
||
616 | * @return string |
||
617 | */ |
||
618 | protected function get_nonce_name() { |
||
621 | |||
622 | /** |
||
623 | * Return WordPress nonce name used to identify the current container instance |
||
624 | * |
||
625 | * @return string |
||
626 | */ |
||
627 | protected function get_nonce_value() { |
||
630 | |||
631 | /** |
||
632 | * Check if the nonce is present in the request and that it is verified |
||
633 | * |
||
634 | * @return bool |
||
635 | */ |
||
636 | protected function verified_nonce_in_request() { |
||
642 | |||
643 | /** |
||
644 | * Internal function that creates the tab and associates it with particular field set |
||
645 | * |
||
646 | * @param string $tab_name |
||
647 | * @param array $fields |
||
648 | * @param int $queue_end |
||
649 | * @return object $this |
||
650 | */ |
||
651 | private function create_tab( $tab_name, $fields, $queue_end = self::TABS_TAIL ) { |
||
672 | |||
673 | /** |
||
674 | * Whether the container is tabbed or not |
||
675 | * |
||
676 | * @return bool |
||
677 | */ |
||
678 | public function is_tabbed() { |
||
681 | |||
682 | /** |
||
683 | * Retrieve all fields that are not defined under a specific tab |
||
684 | * |
||
685 | * @return array |
||
686 | */ |
||
687 | protected function get_untabbed_fields() { |
||
699 | |||
700 | /** |
||
701 | * Retrieve all tabs. |
||
702 | * Create a default tab if there are any untabbed fields. |
||
703 | * |
||
704 | * @return array |
||
705 | */ |
||
706 | protected function get_tabs() { |
||
715 | |||
716 | /** |
||
717 | * Build the tabs JSON |
||
718 | * |
||
719 | * @return array |
||
720 | */ |
||
721 | protected function get_tabs_json() { |
||
733 | |||
734 | /** |
||
735 | * Get custom CSS classes. |
||
736 | * |
||
737 | * @return array<string> |
||
738 | */ |
||
739 | public function get_classes() { |
||
742 | |||
743 | /** |
||
744 | * Set CSS classes that the container should use. |
||
745 | * |
||
746 | * @param string|array<string> $classes |
||
747 | * @return object $this |
||
748 | */ |
||
749 | public function set_classes( $classes ) { |
||
753 | |||
754 | /** |
||
755 | * Returns an array that holds the container data, suitable for JSON representation. |
||
756 | * |
||
757 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
758 | * @return array |
||
759 | */ |
||
760 | public function to_json( $load ) { |
||
786 | |||
787 | /** |
||
788 | * COMMON USAGE METHODS |
||
789 | */ |
||
790 | |||
791 | /** |
||
792 | * Append array of fields to the current fields set. All items of the array |
||
793 | * must be instances of Field and their names should be unique for all |
||
794 | * Carbon containers. |
||
795 | * If a field does not have DataStore already, the container datastore is |
||
796 | * assigned to them instead. |
||
797 | * |
||
798 | * @param array $fields |
||
799 | * @return object $this |
||
800 | */ |
||
801 | public function add_fields( $fields ) { |
||
823 | |||
824 | /** |
||
825 | * Configuration function for adding tab with fields |
||
826 | * |
||
827 | * @param string $tab_name |
||
828 | * @param array $fields |
||
829 | * @return object $this |
||
830 | */ |
||
831 | public function add_tab( $tab_name, $fields ) { |
||
836 | |||
837 | /** |
||
838 | * Proxy function to set attachment conditions |
||
839 | * |
||
840 | * @see Fulfillable_Collection::where() |
||
841 | * @return Container $this |
||
842 | */ |
||
843 | public function where() { |
||
847 | |||
848 | /** |
||
849 | * Proxy function to set attachment conditions |
||
850 | * |
||
851 | * @see Fulfillable_Collection::or_where() |
||
852 | * @return Container $this |
||
853 | */ |
||
854 | public function or_where() { |
||
858 | } |
||
859 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.