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 Post_Meta_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 Post_Meta_Container, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Post_Meta_Container extends Container { |
||
| 14 | /** |
||
| 15 | * ID of the post the container is working with |
||
| 16 | * |
||
| 17 | * @see init() |
||
| 18 | * @var int |
||
| 19 | */ |
||
| 20 | protected $post_id; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * List of default container settings |
||
| 24 | * |
||
| 25 | * @see init() |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | public $settings = array( |
||
| 29 | 'panel_context' => 'normal', |
||
| 30 | 'panel_priority' => 'high', |
||
| 31 | ); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Create a new container |
||
| 35 | * |
||
| 36 | * @param string $unique_id Unique id of the container |
||
| 37 | * @param string $title title of the container |
||
| 38 | * @param string $type Type of the container |
||
| 39 | **/ |
||
| 40 | View Code Duplication | public function __construct( $unique_id, $title, $type ) { |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Create DataStore instance, set post ID to operate with (if such exists). |
||
| 50 | * Bind attach() and save() to the appropriate WordPress actions. |
||
| 51 | **/ |
||
| 52 | public function init() { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Checks whether the current save request is valid |
||
| 69 | * Possible errors are triggering save() for autosave requests |
||
| 70 | * or performing post save outside of the post edit page (like Quick Edit) |
||
| 71 | * |
||
| 72 | * @return bool |
||
| 73 | **/ |
||
| 74 | public function is_valid_save() { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Perform save operation after successful is_valid_save() check. |
||
| 95 | * The call is propagated to all fields in the container. |
||
| 96 | * |
||
| 97 | * @param int $post_id ID of the post against which save() is ran |
||
| 98 | **/ |
||
| 99 | View Code Duplication | public function save( $post_id = null ) { |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Get environment array for page request (in admin) |
||
| 116 | * |
||
| 117 | * @return array |
||
| 118 | **/ |
||
| 119 | protected function get_environment_for_request() { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Perform checks whether the container should be attached during the current request |
||
| 146 | * |
||
| 147 | * @return bool True if the container is allowed to be attached |
||
| 148 | **/ |
||
| 149 | public function is_valid_attach_for_request() { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get environment array for object id |
||
| 166 | * |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | protected function get_environment_for_object( $object_id ) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Check container attachment rules against object id |
||
| 182 | * |
||
| 183 | * @param int $object_id |
||
| 184 | * @return bool |
||
| 185 | **/ |
||
| 186 | View Code Duplication | public function is_valid_attach_for_object( $object_id = null ) { |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Add meta box for each of the container post types |
||
| 198 | **/ |
||
| 199 | public function attach() { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Classes to add to the post meta box |
||
| 218 | */ |
||
| 219 | public function add_postbox_classes( $classes ) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Output the container markup |
||
| 226 | **/ |
||
| 227 | public function render() { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Set the post ID the container will operate with. |
||
| 233 | * |
||
| 234 | * @param int $post_id |
||
| 235 | **/ |
||
| 236 | public function set_post_id( $post_id ) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get array of post types this container can appear on conditionally |
||
| 243 | * |
||
| 244 | * @return array<string> |
||
| 245 | */ |
||
| 246 | View Code Duplication | public function get_post_type_visibility() { |
|
| 261 | |||
| 262 | /** |
||
| 263 | * COMMON USAGE METHODS |
||
| 264 | */ |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Show the container only on particular page referenced by it's path. |
||
| 268 | * |
||
| 269 | * @deprecated |
||
| 270 | * @param int|string $page page ID or page path |
||
| 271 | * @return object $this |
||
| 272 | **/ |
||
| 273 | public function show_on_page( $page ) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Show the container only on pages whose parent is referenced by $parent_page_path. |
||
| 290 | * |
||
| 291 | * @deprecated |
||
| 292 | * @param string $parent_page_path |
||
| 293 | * @return object $this |
||
| 294 | **/ |
||
| 295 | public function show_on_page_children( $parent_page_path ) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Show the container only on pages whose template has filename $template_path. |
||
| 304 | * |
||
| 305 | * @deprecated |
||
| 306 | * @param string|array $template_path |
||
| 307 | * @return object $this |
||
| 308 | **/ |
||
| 309 | public function show_on_template( $template_path ) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Hide the container from pages whose template has filename $template_path. |
||
| 322 | * |
||
| 323 | * @deprecated |
||
| 324 | * @param string|array $template_path |
||
| 325 | * @return object $this |
||
| 326 | **/ |
||
| 327 | public function hide_on_template( $template_path ) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Show the container only on hierarchical posts of level $level. |
||
| 335 | * Levels start from 1 (top level post) |
||
| 336 | * |
||
| 337 | * @deprecated |
||
| 338 | * @param int $level |
||
| 339 | * @return object $this |
||
| 340 | **/ |
||
| 341 | public function show_on_level( $level ) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Show the container only on posts from the specified format. |
||
| 348 | * Learn more about {@link http://codex.wordpress.org/Post_Formats Post Formats (Codex)} |
||
| 349 | * |
||
| 350 | * @deprecated |
||
| 351 | * @param string|array $post_format Name of the format as listed on Codex |
||
| 352 | * @return object $this |
||
| 353 | **/ |
||
| 354 | public function show_on_post_format( $post_format ) { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Show the container only on posts from the specified type(s). |
||
| 362 | * |
||
| 363 | * @deprecated |
||
| 364 | * @param string|array $post_types |
||
| 365 | * @return object $this |
||
| 366 | **/ |
||
| 367 | public function show_on_post_type( $post_types ) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Show the container only on posts from the specified category. |
||
| 375 | * |
||
| 376 | * @see show_on_taxonomy_term() |
||
| 377 | * |
||
| 378 | * @deprecated |
||
| 379 | * @param string $category_slug |
||
| 380 | * @return object $this |
||
| 381 | **/ |
||
| 382 | View Code Duplication | public function show_on_category( $category_slug ) { |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Show the container only on posts which have term $term_slug from the $taxonomy_slug taxonomy. |
||
| 393 | * |
||
| 394 | * @deprecated |
||
| 395 | * @param string $taxonomy_slug |
||
| 396 | * @param string $term_slug |
||
| 397 | * @return object $this |
||
| 398 | **/ |
||
| 399 | View Code Duplication | public function show_on_taxonomy_term( $term_slug, $taxonomy_slug ) { |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Sets the meta box container context |
||
| 410 | * |
||
| 411 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
| 412 | * @param string $context ('normal', 'advanced' or 'side') |
||
| 413 | */ |
||
| 414 | public function set_context( $context ) { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Sets the meta box container priority |
||
| 422 | * |
||
| 423 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
| 424 | * @param string $priority ('high', 'core', 'default' or 'low') |
||
| 425 | */ |
||
| 426 | public function set_priority( $priority ) { |
||
| 431 | } |
||
| 432 |