Complex classes like CustomPostTypeOptions 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 CustomPostTypeOptions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | abstract class CustomPostTypeOptions |
||
| 20 | { |
||
| 21 | private $label; |
||
| 22 | private $labels; |
||
| 23 | private $description; |
||
| 24 | private $public; |
||
| 25 | private $hierarchical; |
||
| 26 | private $excludeFromSearch; |
||
| 27 | private $publiclyQueryable; |
||
| 28 | private $showUi; |
||
| 29 | private $showInMenu; |
||
| 30 | private $showInNavMenus; |
||
| 31 | private $showInAdminBar; |
||
| 32 | private $showInRest; |
||
| 33 | private $restBase; |
||
| 34 | private $restControllerClass; |
||
| 35 | private $menuPosition; |
||
| 36 | private $menuIcon; |
||
| 37 | private $capabilityType; |
||
| 38 | private $capabilities; |
||
| 39 | private $mapMetaCap; |
||
| 40 | private $supports; |
||
| 41 | private $registerMetaboxCb; |
||
| 42 | private $taxonomies; |
||
| 43 | private $hasArchive; |
||
| 44 | private $rewrite; |
||
| 45 | private $slug; |
||
| 46 | private $withFront; |
||
| 47 | private $feeds; |
||
| 48 | private $pages; |
||
| 49 | private $epMask; |
||
| 50 | private $queryVar; |
||
| 51 | private $canExport; |
||
| 52 | private $deleteWithUser; |
||
| 53 | private $builtIn; |
||
| 54 | private $editLink; |
||
| 55 | |||
| 56 | public function getLabel() : string |
||
| 60 | |||
| 61 | public function setLabel(string $label) : self |
||
| 67 | |||
| 68 | public function getLabels() : array |
||
| 72 | |||
| 73 | public function setLabels($labels) : self |
||
| 79 | |||
| 80 | public function getDescription() : string |
||
| 84 | |||
| 85 | public function setDescription($description) |
||
| 91 | |||
| 92 | public function getPublic() |
||
| 96 | |||
| 97 | public function setPublic($public) : self |
||
| 103 | |||
| 104 | public function getHierarchical() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param mixed $hierarchical |
||
| 111 | * |
||
| 112 | * @return PostTypeOptions |
||
| 113 | */ |
||
| 114 | public function setHierarchical($hierarchical) |
||
| 120 | |||
| 121 | public function getExcludeFromSearch() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param mixed $excludeFromSearch |
||
| 128 | * |
||
| 129 | * @return PostTypeOptions |
||
| 130 | */ |
||
| 131 | public function setExcludeFromSearch($excludeFromSearch) |
||
| 137 | |||
| 138 | public function getPubliclyQueryable() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param mixed $publiclyQueryable |
||
| 145 | * |
||
| 146 | * @return PostTypeOptions |
||
| 147 | */ |
||
| 148 | public function setPubliclyQueryable($publiclyQueryable) |
||
| 154 | |||
| 155 | public function getShowUi() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param mixed $showUi |
||
| 162 | * |
||
| 163 | * @return PostTypeOptions |
||
| 164 | */ |
||
| 165 | public function setShowUi($showUi) |
||
| 171 | |||
| 172 | public function getShowInMenu() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param mixed $showInMenu |
||
| 179 | * |
||
| 180 | * @return PostTypeOptions |
||
| 181 | */ |
||
| 182 | public function setShowInMenu($showInMenu) |
||
| 188 | |||
| 189 | public function getShowInNavMenus() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param mixed $showInNavMenus |
||
| 196 | * |
||
| 197 | * @return PostTypeOptions |
||
| 198 | */ |
||
| 199 | public function setShowInNavMenus($showInNavMenus) |
||
| 205 | |||
| 206 | public function getShowInAdminBar() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param mixed $showInAdminBar |
||
| 213 | * |
||
| 214 | * @return PostTypeOptions |
||
| 215 | */ |
||
| 216 | public function setShowInAdminBar($showInAdminBar) |
||
| 222 | |||
| 223 | public function getShowInRest() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param mixed $showInRest |
||
| 230 | * |
||
| 231 | * @return PostTypeOptions |
||
| 232 | */ |
||
| 233 | public function setShowInRest($showInRest) |
||
| 239 | |||
| 240 | public function getRestBase() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param mixed $restBase |
||
| 247 | * |
||
| 248 | * @return PostTypeOptions |
||
| 249 | */ |
||
| 250 | public function setRestBase($restBase) |
||
| 256 | |||
| 257 | public function getRestControllerClass() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param mixed $restControllerClass |
||
| 264 | * |
||
| 265 | * @return PostTypeOptions |
||
| 266 | */ |
||
| 267 | public function setRestControllerClass($restControllerClass) |
||
| 273 | |||
| 274 | public function getMenuPosition() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param mixed $menuPosition |
||
| 281 | * |
||
| 282 | * @return PostTypeOptions |
||
| 283 | */ |
||
| 284 | public function setMenuPosition($menuPosition) |
||
| 290 | |||
| 291 | public function getMenuIcon() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @param mixed $menuIcon |
||
| 298 | * |
||
| 299 | * @return PostTypeOptions |
||
| 300 | */ |
||
| 301 | public function setMenuIcon($menuIcon) |
||
| 307 | |||
| 308 | public function getCapabilityType() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param mixed $capabilityType |
||
| 315 | * |
||
| 316 | * @return PostTypeOptions |
||
| 317 | */ |
||
| 318 | public function setCapabilityType($capabilityType) |
||
| 324 | |||
| 325 | public function getCapabilities() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param mixed $capabilities |
||
| 332 | * |
||
| 333 | * @return PostTypeOptions |
||
| 334 | */ |
||
| 335 | public function setCapabilities($capabilities) |
||
| 341 | |||
| 342 | public function getMapMetaCap() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param mixed $mapMetaCap |
||
| 349 | * |
||
| 350 | * @return PostTypeOptions |
||
| 351 | */ |
||
| 352 | public function setMapMetaCap($mapMetaCap) |
||
| 358 | |||
| 359 | public function getSupports() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param mixed $supports |
||
| 366 | * |
||
| 367 | * @return PostTypeOptions |
||
| 368 | */ |
||
| 369 | public function setSupports($supports) |
||
| 375 | |||
| 376 | public function getRegisterMetaboxCb() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param mixed $registerMetaboxCb |
||
| 383 | * |
||
| 384 | * @return PostTypeOptions |
||
| 385 | */ |
||
| 386 | public function setRegisterMetaboxCb($registerMetaboxCb) |
||
| 392 | |||
| 393 | public function getTaxonomies() |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param mixed $taxonomies |
||
| 400 | * |
||
| 401 | * @return PostTypeOptions |
||
| 402 | */ |
||
| 403 | public function setTaxonomies($taxonomies) |
||
| 409 | |||
| 410 | public function getHasArchive() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param mixed $hasArchive |
||
| 417 | * |
||
| 418 | * @return PostTypeOptions |
||
| 419 | */ |
||
| 420 | public function setHasArchive($hasArchive) |
||
| 426 | |||
| 427 | public function getRewrite() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param mixed $rewrite |
||
| 434 | * |
||
| 435 | * @return PostTypeOptions |
||
| 436 | */ |
||
| 437 | public function setRewrite($rewrite) |
||
| 443 | |||
| 444 | public function getSlug() |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @param mixed $slug |
||
| 451 | * |
||
| 452 | * @return PostTypeOptions |
||
| 453 | */ |
||
| 454 | public function setSlug($slug) |
||
| 460 | |||
| 461 | public function getWithFront() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param mixed $withFront |
||
| 468 | * |
||
| 469 | * @return PostTypeOptions |
||
| 470 | */ |
||
| 471 | public function setWithFront($withFront) |
||
| 477 | |||
| 478 | public function getFeeds() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param mixed $feeds |
||
| 485 | * |
||
| 486 | * @return PostTypeOptions |
||
| 487 | */ |
||
| 488 | public function setFeeds($feeds) |
||
| 494 | |||
| 495 | public function getPages() |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @param mixed $pages |
||
| 502 | * |
||
| 503 | * @return PostTypeOptions |
||
| 504 | */ |
||
| 505 | public function setPages($pages) |
||
| 511 | |||
| 512 | public function getEpMask() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @param mixed $epMask |
||
| 519 | * |
||
| 520 | * @return PostTypeOptions |
||
| 521 | */ |
||
| 522 | public function setEpMask($epMask) |
||
| 528 | |||
| 529 | public function getQueryVar() |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @param mixed $queryVar |
||
| 536 | * |
||
| 537 | * @return PostTypeOptions |
||
| 538 | */ |
||
| 539 | public function setQueryVar($queryVar) |
||
| 545 | |||
| 546 | public function getCanExport() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @param mixed $canExport |
||
| 553 | * |
||
| 554 | * @return PostTypeOptions |
||
| 555 | */ |
||
| 556 | public function setCanExport($canExport) |
||
| 562 | |||
| 563 | public function getDeleteWithUser() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @param mixed $deleteWithUser |
||
| 570 | * |
||
| 571 | * @return PostTypeOptions |
||
| 572 | */ |
||
| 573 | public function setDeleteWithUser($deleteWithUser) |
||
| 579 | |||
| 580 | public function getBuiltIn() |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @param mixed $builtIn |
||
| 587 | * |
||
| 588 | * @return PostTypeOptions |
||
| 589 | */ |
||
| 590 | public function setBuiltIn($builtIn) |
||
| 596 | |||
| 597 | public function getEditLink() |
||
| 601 | |||
| 602 | /** |
||
| 603 | * @param mixed $editLink |
||
| 604 | * |
||
| 605 | * @return PostTypeOptions |
||
| 606 | */ |
||
| 607 | public function setEditLink($editLink) |
||
| 613 | |||
| 614 | |||
| 615 | /* |
||
| 616 | * Array or string of arguments for registering a post type. |
||
| 617 | * |
||
| 618 | * @type string $label Name of the post type shown in the menu. Usually plural. |
||
| 619 | * Default is value of $labels['name']. |
||
| 620 | * @type array $labels An array of labels for this post type. If not set, post |
||
| 621 | * labels are inherited for non-hierarchical types and page |
||
| 622 | * labels for hierarchical ones. See getposttypelabels() for a full |
||
| 623 | * list of supported labels. |
||
| 624 | * @type string $description A short descriptive summary of what the post type is. |
||
| 625 | * Default empty. |
||
| 626 | * @type bool $public Whether a post type is intended for use publicly either via |
||
| 627 | * the admin interface or by front-end users. While the default |
||
| 628 | * settings of $excludefromsearch, $publiclyqueryable, $showui, |
||
| 629 | * and $showinnavmenus are inherited from public, each does not |
||
| 630 | * rely on this relationship and controls a very specific intention. |
||
| 631 | * Default false. |
||
| 632 | * @type bool $hierarchical Whether the post type is hierarchical (e.g. page). Default false. |
||
| 633 | * @type bool $excludefromsearch Whether to exclude posts with this post type from front end search |
||
| 634 | * results. Default is the opposite value of $public. |
||
| 635 | * @type bool $publiclyqueryable Whether queries can be performed on the front end for the post type |
||
| 636 | * as part of parserequest(). Endpoints would include: |
||
| 637 | * * ?posttype={posttypekey} |
||
| 638 | * * ?{posttypekey}={singlepostslug} |
||
| 639 | * * ?{posttypequeryvar}={singlepostslug} |
||
| 640 | * If not set, the default is inherited from $public. |
||
| 641 | * @type bool $showui Whether to generate and allow a UI for managing this post type in the |
||
| 642 | * admin. Default is value of $public. |
||
| 643 | * @type bool $showinmenu Where to show the post type in the admin menu. To work, $showui |
||
| 644 | * must be true. If true, the post type is shown in its own top level |
||
| 645 | * menu. If false, no menu is shown. If a string of an existing top |
||
| 646 | * level menu (eg. 'tools.php' or 'edit.php?posttype=page'), the post |
||
| 647 | * type will be placed as a sub-menu of that. |
||
| 648 | * Default is value of $showui. |
||
| 649 | * @type bool $showinnavmenus Makes this post type available for selection in navigation menus. |
||
| 650 | * Default is value $public. |
||
| 651 | * @type bool $showinadminbar Makes this post type available via the admin bar. Default is value |
||
| 652 | * of $showinmenu. |
||
| 653 | * @type bool $showinrest Whether to add the post type route in the REST API 'wp/v2' namespace. |
||
| 654 | * @type string $restbase To change the base url of REST API route. Default is $posttype. |
||
| 655 | * @type string $restcontrollerclass REST API Controller class name. Default is 'WPRESTPostsController'. |
||
| 656 | * @type int $menuposition The position in the menu order the post type should appear. To work, |
||
| 657 | * $showinmenu must be true. Default null (at the bottom). |
||
| 658 | * @type string $menuicon The url to the icon to be used for this menu. Pass a base64-encoded |
||
| 659 | * SVG using a data URI, which will be colored to match the color scheme |
||
| 660 | * -- this should begin with 'data:image/svg+xml;base64,'. Pass the name |
||
| 661 | * of a Dashicons helper class to use a font icon, e.g. |
||
| 662 | * 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty |
||
| 663 | * so an icon can be added via CSS. Defaults to use the posts icon. |
||
| 664 | * @type string $capabilitytype The string to use to build the read, edit, and delete capabilities. |
||
| 665 | * May be passed as an array to allow for alternative plurals when using |
||
| 666 | * this argument as a base to construct the capabilities, e.g. |
||
| 667 | * array('story', 'stories'). Default 'post'. |
||
| 668 | * @type array $capabilities Array of capabilities for this post type. $capabilitytype is used |
||
| 669 | * as a base to construct capabilities by default. |
||
| 670 | * See getposttypecapabilities(). |
||
| 671 | * @type bool $mapmetacap Whether to use the internal default meta capability handling. |
||
| 672 | * Default false. |
||
| 673 | * @type array $supports Core feature(s) the post type supports. Serves as an alias for calling |
||
| 674 | * addposttypesupport() directly. Core features include 'title', |
||
| 675 | * 'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', |
||
| 676 | * 'page-attributes', 'thumbnail', 'custom-fields', and 'post-formats'. |
||
| 677 | * Additionally, the 'revisions' feature dictates whether the post type |
||
| 678 | * will store revisions, and the 'comments' feature dictates whether the |
||
| 679 | * comments count will show on the edit screen. Defaults is an array |
||
| 680 | * containing 'title' and 'editor'. |
||
| 681 | * @type callable $registermetaboxcb Provide a callback function that sets up the meta boxes for the |
||
| 682 | * edit form. Do removemetabox() and addmetabox() calls in the |
||
| 683 | * callback. Default null. |
||
| 684 | * @type array $taxonomies An array of taxonomy identifiers that will be registered for the |
||
| 685 | * post type. Taxonomies can be registered later with registertaxonomy() |
||
| 686 | * or registertaxonomyforobjecttype(). |
||
| 687 | * Default empty array. |
||
| 688 | * @type bool|string $hasarchive Whether there should be post type archives, or if a string, the |
||
| 689 | * archive slug to use. Will generate the proper rewrite rules if |
||
| 690 | * $rewrite is enabled. Default false. |
||
| 691 | * @type bool|array $rewrite { |
||
| 692 | * Triggers the handling of rewrites for this post type. To prevent rewrite, set to false. |
||
| 693 | * Defaults to true, using $posttype as slug. To specify rewrite rules, an array can be |
||
| 694 | * passed with any of these keys: |
||
| 695 | * |
||
| 696 | * @type string $slug Customize the permastruct slug. Defaults to $posttype key. |
||
| 697 | * @type bool $withfront Whether the permastruct should be prepended with WPRewrite::$front. |
||
| 698 | * Default true. |
||
| 699 | * @type bool $feeds Whether the feed permastruct should be built for this post type. |
||
| 700 | * Default is value of $hasarchive. |
||
| 701 | * @type bool $pages Whether the permastruct should provide for pagination. Default true. |
||
| 702 | * @type const $epmask Endpoint mask to assign. If not specified and permalinkepmask is set, |
||
| 703 | * inherits from $permalinkepmask. If not specified and permalinkepmask |
||
| 704 | * is not set, defaults to EPPERMALINK. |
||
| 705 | * } |
||
| 706 | * @type string|bool $queryvar Sets the queryvar key for this post type. Defaults to $posttype |
||
| 707 | * key. If false, a post type cannot be loaded at |
||
| 708 | * ?{queryvar}={postslug}. If specified as a string, the query |
||
| 709 | * ?{queryvarstring}={postslug} will be valid. |
||
| 710 | * @type bool $canexport Whether to allow this post type to be exported. Default true. |
||
| 711 | * @type bool $deletewithuser Whether to delete posts of this type when deleting a user. If true, |
||
| 712 | * posts of this type belonging to the user will be moved to trash |
||
| 713 | * when then user is deleted. If false, posts of this type belonging |
||
| 714 | * to the user will *not* be trashed or deleted. If not set (the default), |
||
| 715 | * posts are trashed if posttypesupports('author'). Otherwise posts |
||
| 716 | * are not trashed or deleted. Default null. |
||
| 717 | * @type bool $builtin FOR INTERNAL USE ONLY! True if this post type is a native or |
||
| 718 | * "built-in" posttype. Default false. |
||
| 719 | * @type string $editlink FOR INTERNAL USE ONLY! URL segment to use for edit link of |
||
| 720 | * this post type. Default 'post.php?post=%d'. |
||
| 721 | */ |
||
| 722 | } |
||
| 723 |