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 Pods_Templates 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 Pods_Templates, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class Pods_Templates extends PodsComponent { |
||
33 | |||
34 | /** |
||
35 | * Pods object |
||
36 | * |
||
37 | * @var object |
||
38 | * |
||
39 | * @since 2.0 |
||
40 | */ |
||
41 | static $obj = null; |
||
42 | |||
43 | /** |
||
44 | * Whether to enable deprecated functionality based on old function usage |
||
45 | * |
||
46 | * @var bool |
||
47 | * |
||
48 | * @since 2.0 |
||
49 | */ |
||
50 | static $deprecated = false; |
||
51 | |||
52 | /** |
||
53 | * Object type |
||
54 | * |
||
55 | * @var string |
||
56 | * |
||
57 | * @since 2.0 |
||
58 | */ |
||
59 | private $object_type = '_pods_template'; |
||
60 | |||
61 | /** |
||
62 | * The capability type. |
||
63 | * @link https://codex.wordpress.org/Function_Reference/register_post_type |
||
64 | * @var string |
||
65 | */ |
||
66 | private $capability_type = 'pods_template'; |
||
67 | |||
68 | /** |
||
69 | * Do things like register/enqueue scripts and stylesheets |
||
70 | * |
||
71 | * @since 2.0 |
||
72 | */ |
||
73 | public function __construct () { |
||
74 | $args = array( |
||
75 | 'label' => 'Pod Templates', |
||
76 | 'labels' => array( 'singular_name' => 'Pod Template' ), |
||
77 | 'public' => false, |
||
78 | 'can_export' => false, |
||
79 | 'show_ui' => true, |
||
80 | 'show_in_menu' => false, |
||
81 | 'query_var' => false, |
||
82 | 'rewrite' => false, |
||
83 | 'has_archive' => false, |
||
84 | 'hierarchical' => false, |
||
85 | 'supports' => array( 'title', 'author', 'revisions' ), |
||
86 | 'menu_icon' => 'dashicons-pods' |
||
87 | ); |
||
88 | |||
89 | if ( !pods_is_admin() ) |
||
90 | $args[ 'capability_type' ] = $this->capability_type; |
||
91 | |||
92 | $args = PodsInit::object_label_fix( $args, 'post_type' ); |
||
93 | |||
94 | register_post_type( $this->object_type, apply_filters( 'pods_internal_register_post_type_object_template', $args ) ); |
||
95 | |||
96 | View Code Duplication | if ( is_admin() ) { |
|
97 | add_filter( 'post_updated_messages', array( $this, 'setup_updated_messages' ), 10, 1 ); |
||
98 | |||
99 | add_action( 'dbx_post_advanced', array( $this, 'edit_page_form' ), 10 ); |
||
100 | |||
101 | add_action( 'pods_meta_groups', array( $this, 'add_meta_boxes' ) ); |
||
102 | |||
103 | add_filter( 'get_post_metadata', array( $this, 'get_meta' ), 10, 4 ); |
||
104 | add_filter( 'update_post_metadata', array( $this, 'save_meta' ), 10, 4 ); |
||
105 | |||
106 | add_action( 'pods_meta_save_pre_post__pods_template', array( $this, 'fix_filters' ), 10, 5 ); |
||
107 | add_action( 'post_updated', array( $this, 'clear_cache' ), 10, 3 ); |
||
108 | add_action( 'delete_post', array( $this, 'clear_cache' ), 10, 1 ); |
||
109 | add_filter( 'post_row_actions', array( $this, 'remove_row_actions' ), 10, 2 ); |
||
110 | add_filter( 'bulk_actions-edit-' . $this->object_type, array( $this, 'remove_bulk_actions' ) ); |
||
111 | |||
112 | add_filter( 'builder_layout_filter_non_layout_post_types', array( $this, 'disable_builder_layout' ) ); |
||
113 | |||
114 | } |
||
115 | |||
116 | add_filter( 'members_get_capabilities', array( $this, 'get_capabilities' ) ); |
||
117 | } |
||
118 | |||
119 | View Code Duplication | public function get_capabilities( $caps ) { |
|
120 | $caps = array_merge( $caps, array( |
||
121 | 'edit_' . $this->capability_type, |
||
122 | 'read_' . $this->capability_type, |
||
123 | 'delete_' . $this->capability_type, |
||
124 | 'edit_' . $this->capability_type . 's', |
||
125 | 'edit_others_' . $this->capability_type . 's', |
||
126 | 'publish_' . $this->capability_type . 's', |
||
127 | 'read_private_' . $this->capability_type . 's', |
||
128 | 'edit_' . $this->capability_type . 's', |
||
129 | ) ); |
||
130 | return $caps; |
||
131 | } |
||
132 | |||
133 | public function disable_builder_layout ( $post_types ) { |
||
138 | |||
139 | /** |
||
140 | * Update Post Type messages |
||
141 | * |
||
142 | * @param array $messages |
||
143 | * |
||
144 | * @return array |
||
145 | * @since 2.0.2 |
||
146 | */ |
||
147 | View Code Duplication | public function setup_updated_messages ( $messages ) { |
|
192 | |||
193 | /** |
||
194 | * Enqueue styles |
||
195 | * |
||
196 | * @since 2.0 |
||
197 | */ |
||
198 | public function admin_assets () { |
||
201 | |||
202 | /** |
||
203 | * Fix filters, specifically removing balanceTags |
||
204 | * |
||
205 | * @since 2.0.1 |
||
206 | */ |
||
207 | public function fix_filters ( $data, $pod = null, $id = null, $groups = null, $post = null ) { |
||
210 | |||
211 | /** |
||
212 | * Remove unused row actions |
||
213 | * |
||
214 | * @since 2.0.5 |
||
215 | */ |
||
216 | View Code Duplication | function remove_row_actions ( $actions, $post ) { |
|
234 | |||
235 | /** |
||
236 | * Remove unused bulk actions |
||
237 | * |
||
238 | * @since 2.0.5 |
||
239 | */ |
||
240 | public function remove_bulk_actions ( $actions ) { |
||
246 | |||
247 | /** |
||
248 | * Clear cache on save |
||
249 | * |
||
250 | * @since 2.0 |
||
251 | */ |
||
252 | View Code Duplication | public function clear_cache ( $data, $pod = null, $id = null, $groups = null, $post = null ) { |
|
269 | |||
270 | /** |
||
271 | * Change post title placeholder text |
||
272 | * |
||
273 | * @since 2.0 |
||
274 | */ |
||
275 | public function set_title_text ( $text, $post ) { |
||
278 | |||
279 | /** |
||
280 | * Edit page form |
||
281 | * |
||
282 | * @since 2.0 |
||
283 | */ |
||
284 | View Code Duplication | public function edit_page_form () { |
|
292 | |||
293 | /** |
||
294 | * Add meta boxes to the page |
||
295 | * |
||
296 | * @since 2.0 |
||
297 | */ |
||
298 | public function add_meta_boxes () { |
||
343 | |||
344 | /** |
||
345 | * Get the fields |
||
346 | * |
||
347 | * @param null $_null |
||
348 | * @param int $post_ID |
||
349 | * @param string $meta_key |
||
350 | * @param bool $single |
||
351 | * |
||
352 | * @return array|bool|int|mixed|null|string|void |
||
353 | */ |
||
354 | View Code Duplication | public function get_meta ( $_null, $post_ID = null, $meta_key = null, $single = false ) { |
|
364 | |||
365 | /** |
||
366 | * Save the fields |
||
367 | * |
||
368 | * @param $_null |
||
369 | * @param int $post_ID |
||
370 | * @param string $meta_key |
||
371 | * @param string $meta_value |
||
372 | * |
||
373 | * @return bool|int|null |
||
374 | */ |
||
375 | View Code Duplication | public function save_meta ( $_null, $post_ID = null, $meta_key = null, $meta_value = null ) { |
|
406 | |||
407 | /** |
||
408 | * Display the page template |
||
409 | * |
||
410 | * @param string $template_name The template name |
||
411 | * @param string $code Custom template code to use instead |
||
412 | * @param object $obj The Pods object |
||
413 | * @param bool $deprecated Whether to use deprecated functionality based on old function usage |
||
414 | * |
||
415 | * @return mixed|string|void |
||
416 | * @since 2.0 |
||
417 | */ |
||
418 | public static function template ( $template_name, $code = null, $obj = null, $deprecated = false ) { |
||
496 | |||
497 | /** |
||
498 | * Parse a template string |
||
499 | * |
||
500 | * @param string $code The template string to parse |
||
501 | * @param object $obj The Pods object |
||
502 | * |
||
503 | * @since 1.8.5 |
||
504 | */ |
||
505 | public static function do_template ( $code, $obj = null ) { |
||
534 | |||
535 | } |
||
536 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.