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 PageManager 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 PageManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class PageManager |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Given an associative array of data, where the key is the column name |
||
| 20 | * in `tbl_pages` and the value is the data, this function will create a new |
||
| 21 | * Page and return a Page ID on success. |
||
| 22 | * |
||
| 23 | * @param array $fields |
||
| 24 | * Associative array of field names => values for the Page |
||
| 25 | * @throws DatabaseException |
||
| 26 | * @return integer|boolean |
||
| 27 | * Returns the Page ID of the created Page on success, false otherwise. |
||
| 28 | */ |
||
| 29 | View Code Duplication | public static function add(array $fields) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Work out the next available sort order for a new page |
||
| 44 | * |
||
| 45 | * @return integer |
||
| 46 | * Returns the next sort order |
||
| 47 | */ |
||
| 48 | public static function fetchNextSortOrder() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Return a Page title by the handle |
||
| 65 | * |
||
| 66 | * @param string $handle |
||
| 67 | * The handle of the page |
||
| 68 | * @return integer |
||
| 69 | * The Page title |
||
| 70 | */ |
||
| 71 | public static function fetchTitleFromHandle($handle) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Return a Page ID by the handle |
||
| 84 | * |
||
| 85 | * @param string $handle |
||
| 86 | * The handle of the page |
||
| 87 | * @return integer |
||
| 88 | * The Page ID |
||
| 89 | */ |
||
| 90 | public static function fetchIDFromHandle($handle) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Given a Page ID and an array of types, this function will add Page types |
||
| 103 | * to that Page. If a Page types are stored in `tbl_pages_types`. |
||
| 104 | * |
||
| 105 | * @param integer $page_id |
||
| 106 | * The Page ID to add the Types to |
||
| 107 | * @param array $types |
||
| 108 | * An array of page types |
||
| 109 | * @throws DatabaseException |
||
| 110 | * @return boolean |
||
| 111 | */ |
||
| 112 | public static function addPageTypesToPage($page_id = null, array $types) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Given a `$page_id`, this function will remove all associated |
||
| 135 | * Page Types from `tbl_pages_types`. |
||
| 136 | * |
||
| 137 | * @param integer $page_id |
||
| 138 | * The ID of the Page that should be deleted. |
||
| 139 | * @throws DatabaseException |
||
| 140 | * @return boolean |
||
| 141 | */ |
||
| 142 | public static function deletePageTypes($page_id = null) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * This function will update all children of a particular page (if any) |
||
| 153 | * by renaming/moving all related files to their new path and updating |
||
| 154 | * their database information. This is a recursive function and will work |
||
| 155 | * to any depth. |
||
| 156 | * |
||
| 157 | * @param integer $page_id |
||
| 158 | * The ID of the Page whose children need to be updated |
||
| 159 | * @param string $page_path |
||
| 160 | * The path of the Page, which is the handles of the Page parents. If the |
||
| 161 | * page has multiple parents, they will be separated by a forward slash. |
||
| 162 | * eg. article/read. If a page has no parents, this parameter should be null. |
||
| 163 | * @throws Exception |
||
| 164 | * @return boolean |
||
| 165 | */ |
||
| 166 | public static function editPageChildren($page_id = null, $page_path = null) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Returns the child Pages (if any) of the given `$page_id`. |
||
| 197 | * |
||
| 198 | * @param integer $page_id |
||
| 199 | * The ID of the Page. |
||
| 200 | * @param array $select (optional) |
||
| 201 | * Accepts an array of columns to return from `tbl_pages`. If omitted, |
||
| 202 | * all columns from the table will be returned. |
||
| 203 | * @return array|null |
||
| 204 | * An associative array of Page information with the key being the column |
||
| 205 | * name from `tbl_pages` and the value being the data. If multiple Pages |
||
| 206 | * are found, an array of Pages will be returned. If no Pages are found |
||
| 207 | * null is returned. |
||
| 208 | */ |
||
| 209 | public static function fetchChildPages($page_id = null, array $select = array()) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * This function will return an associative array of Page information. The |
||
| 227 | * information returned is defined by the `$include_types` and `$select` |
||
| 228 | * parameters, which will return the Page Types for the Page and allow |
||
| 229 | * a developer to restrict what information is returned about the Page. |
||
| 230 | * Optionally, `$where` and `$order_by` parameters allow a developer to |
||
| 231 | * further refine their query. |
||
| 232 | * |
||
| 233 | * @param boolean $include_types |
||
| 234 | * Whether to include the resulting Page's Page Types in the return array, |
||
| 235 | * under the key `type`. Defaults to true. |
||
| 236 | * @param array $select (optional) |
||
| 237 | * Accepts an array of columns to return from `tbl_pages`. If omitted, |
||
| 238 | * all columns from the table will be returned. |
||
| 239 | * @param array $where (optional) |
||
| 240 | * Accepts an array of WHERE statements that will be appended with AND. |
||
| 241 | * If omitted, all pages will be returned. |
||
| 242 | * @param string $order_by (optional) |
||
| 243 | * Allows a developer to return the Pages in a particular order. The string |
||
| 244 | * passed will be appended to `ORDER BY`. If omitted this will return |
||
| 245 | * Pages ordered by `sortorder`. |
||
| 246 | * @param boolean $hierarchical (optional) |
||
| 247 | * If true, builds a multidimensional array representing the pages hierarchy. |
||
| 248 | * Defaults to false. |
||
| 249 | * @return array|null |
||
| 250 | * An associative array of Page information with the key being the column |
||
| 251 | * name from `tbl_pages` and the value being the data. If requested, the array |
||
| 252 | * can be made multidimensional to reflect the pages hierarchy. If no Pages are |
||
| 253 | * found, null is returned. |
||
| 254 | */ |
||
| 255 | public static function fetch( |
||
| 304 | |||
| 305 | /** |
||
| 306 | * This function returns a Page's Page Types. If the `$page_id` |
||
| 307 | * parameter is given, the types returned will be for that Page. |
||
| 308 | * |
||
| 309 | * @param integer $page_id |
||
| 310 | * The ID of the Page. |
||
| 311 | * @return array |
||
| 312 | * An array of the Page Types |
||
| 313 | */ |
||
| 314 | public static function fetchPageTypes($page_id = null) |
||
| 337 | |||
| 338 | private function __buildTreeView($parent_id, $pages, &$results) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * This function creates the initial `.xsl` template for the page, whether |
||
| 355 | * that be from the `TEMPLATES/blueprints.page.xsl` file, or from an existing |
||
| 356 | * template with the same name. This function will handle the renaming of a page |
||
| 357 | * by creating the new files using the old files as the templates then removing |
||
| 358 | * the old template. If a template already exists for a Page, it will not |
||
| 359 | * be overridden and the function will return true. |
||
| 360 | * |
||
| 361 | * @see toolkit.PageManager#resolvePageFileLocation() |
||
| 362 | * @see toolkit.PageManager#createHandle() |
||
| 363 | * @param string $new_path |
||
| 364 | * The path of the Page, which is the handles of the Page parents. If the |
||
| 365 | * page has multiple parents, they will be separated by a forward slash. |
||
| 366 | * eg. article/read. If a page has no parents, this parameter should be null. |
||
| 367 | * @param string $new_handle |
||
| 368 | * The new Page handle, generated using `PageManager::createHandle`. |
||
| 369 | * @param string $old_path (optional) |
||
| 370 | * This parameter is only required when renaming a Page. It should be the 'old |
||
| 371 | * path' before the Page was renamed. |
||
| 372 | * @param string $old_handle (optional) |
||
| 373 | * This parameter is only required when renaming a Page. It should be the 'old |
||
| 374 | * handle' before the Page was renamed. |
||
| 375 | * @throws Exception |
||
| 376 | * @return boolean |
||
| 377 | * True when the page files have been created successfully, false otherwise. |
||
| 378 | */ |
||
| 379 | public static function createPageFiles($new_path, $new_handle, $old_path = null, $old_handle = null) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Resolves the path to this page's XSLT file. The Symphony convention |
||
| 438 | * is that they are stored in the `PAGES` folder. If this page has a parent |
||
| 439 | * it will be as if all the / in the URL have been replaced with _. ie. |
||
| 440 | * /articles/read/ will produce a file `articles_read.xsl` |
||
| 441 | * |
||
| 442 | * @see toolkit.PageManager#createFilePath() |
||
| 443 | * @param string $path |
||
| 444 | * The URL path to this page, excluding the current page. ie, /articles/read |
||
| 445 | * would make `$path` become articles/ |
||
| 446 | * @param string $handle |
||
| 447 | * The handle of the page. |
||
| 448 | * @return string |
||
| 449 | * The path to the XSLT of the page |
||
| 450 | */ |
||
| 451 | public static function resolvePageFileLocation($path, $handle) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * This function takes a `$path` and `$handle` and generates a flattened |
||
| 458 | * string for use as a filename for a Page's template. |
||
| 459 | * |
||
| 460 | * @param string $path |
||
| 461 | * The path of the Page, which is the handles of the Page parents. If the |
||
| 462 | * page has multiple parents, they will be separated by a forward slash. |
||
| 463 | * eg. article/read. If a page has no parents, this parameter should be null. |
||
| 464 | * @param string $handle |
||
| 465 | * A Page handle, generated using `PageManager::createHandle`. |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | public static function createFilePath($path, $handle) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Returns the path to the page-template by looking at the |
||
| 475 | * `WORKSPACE/template/` directory, then at the `TEMPLATES` |
||
| 476 | * directory for `$name.xsl`. If the template is not found, |
||
| 477 | * false is returned |
||
| 478 | * |
||
| 479 | * @param string $name |
||
| 480 | * Name of the template |
||
| 481 | * @return mixed |
||
| 482 | * String, which is the path to the template if the template is found, |
||
| 483 | * false otherwise |
||
| 484 | */ |
||
| 485 | View Code Duplication | public static function getTemplate($name) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * A wrapper for `General::writeFile`, this function takes a `$path` |
||
| 500 | * and a `$data` and writes the new template to disk. |
||
| 501 | * |
||
| 502 | * @param string $path |
||
| 503 | * The path to write the template to |
||
| 504 | * @param string $data |
||
| 505 | * The contents of the template |
||
| 506 | * @return boolean |
||
| 507 | * True when written successfully, false otherwise |
||
| 508 | */ |
||
| 509 | public static function writePageFiles($path, $data) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * This function will update a Page in `tbl_pages` given a `$page_id` |
||
| 516 | * and an associative array of `$fields`. A third parameter, `$delete_types` |
||
| 517 | * will also delete the Page's associated Page Types if passed true. |
||
| 518 | * |
||
| 519 | * @see toolkit.PageManager#addPageTypesToPage() |
||
| 520 | * @param integer $page_id |
||
| 521 | * The ID of the Page that should be updated |
||
| 522 | * @param array $fields |
||
| 523 | * Associative array of field names => values for the Page. |
||
| 524 | * This array does need to contain every value for the Page, it |
||
| 525 | * can just be the changed values. |
||
| 526 | * @param boolean $delete_types |
||
| 527 | * If true, this parameter will cause the Page Types of the Page to |
||
| 528 | * be deleted. By default this is false. |
||
| 529 | * @return boolean |
||
| 530 | */ |
||
| 531 | public static function edit($page_id, array $fields, $delete_types = false) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * This function takes a Page ID and removes the Page from the database |
||
| 555 | * in `tbl_pages` and it's associated Page Types in `tbl_pages_types`. |
||
| 556 | * This function does not delete any of the Page's children. |
||
| 557 | * |
||
| 558 | * @see toolkit.PageManager#deletePageTypes |
||
| 559 | * @see toolkit.PageManager#deletePageFiles |
||
| 560 | * @param integer $page_id |
||
| 561 | * The ID of the Page that should be deleted. |
||
| 562 | * @param boolean $delete_files |
||
| 563 | * If true, this parameter will remove the Page's templates from the |
||
| 564 | * the filesystem. By default this is true. |
||
| 565 | * @throws DatabaseException |
||
| 566 | * @throws Exception |
||
| 567 | * @return boolean |
||
| 568 | */ |
||
| 569 | public static function delete($page_id = null, $delete_files = true) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Returns Pages that match the given `$page_id`. Developers can optionally |
||
| 608 | * choose to specify what Page information is returned using the `$select` |
||
| 609 | * parameter. |
||
| 610 | * |
||
| 611 | * @param integer|array $page_id |
||
| 612 | * The ID of the Page, or an array of ID's |
||
| 613 | * @param array $select (optional) |
||
| 614 | * Accepts an array of columns to return from `tbl_pages`. If omitted, |
||
| 615 | * all columns from the table will be returned. |
||
| 616 | * @return array|null |
||
| 617 | * An associative array of Page information with the key being the column |
||
| 618 | * name from `tbl_pages` and the value being the data. If multiple Pages |
||
| 619 | * are found, an array of Pages will be returned. If no Pages are found |
||
| 620 | * null is returned. |
||
| 621 | */ |
||
| 622 | public static function fetchPageByID($page_id = null, array $select = array()) |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Given a Page's `$path` and `$handle`, this function will remove |
||
| 647 | * it's templates from the `PAGES` directory returning boolean on |
||
| 648 | * completion |
||
| 649 | * |
||
| 650 | * @param string $page_path |
||
| 651 | * The path of the Page, which is the handles of the Page parents. If the |
||
| 652 | * page has multiple parents, they will be separated by a forward slash. |
||
| 653 | * eg. article/read. If a page has no parents, this parameter should be null. |
||
| 654 | * @param string $handle |
||
| 655 | * A Page handle, generated using `PageManager::createHandle`. |
||
| 656 | * @throws Exception |
||
| 657 | * @return boolean |
||
| 658 | */ |
||
| 659 | public static function deletePageFiles($page_path, $handle) |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Returns Pages that match the given `$type`. If no `$type` is provided |
||
| 678 | * the function returns the result of `PageManager::fetch`. |
||
| 679 | * |
||
| 680 | * @param string $type |
||
| 681 | * Where the type is one of the available Page Types. |
||
| 682 | * @return array|null |
||
| 683 | * An associative array of Page information with the key being the column |
||
| 684 | * name from `tbl_pages` and the value being the data. If multiple Pages |
||
| 685 | * are found, an array of Pages will be returned. If no Pages are found |
||
| 686 | * null is returned. |
||
| 687 | */ |
||
| 688 | public static function fetchPageByType($type = null) |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Returns all the page types that exist in this Symphony install. |
||
| 711 | * There are 6 default system page types, and new types can be added |
||
| 712 | * by Developers via the Page Editor. |
||
| 713 | * |
||
| 714 | * @since Symphony 2.3 introduced the JSON type. |
||
| 715 | * @return array |
||
| 716 | * An array of strings of the page types used in this Symphony |
||
| 717 | * install. At the minimum, this will be an array with the values |
||
| 718 | * 'index', 'XML', 'JSON', 'admin', '404' and '403'. |
||
| 719 | */ |
||
| 720 | public static function fetchAvailablePageTypes() |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Fetch an associated array with Page ID's and the types they're using. |
||
| 732 | * |
||
| 733 | * @throws DatabaseException |
||
| 734 | * @return array |
||
| 735 | * A 2-dimensional associated array where the key is the page ID. |
||
| 736 | */ |
||
| 737 | public static function fetchAllPagesPageTypes() |
||
| 750 | |||
| 751 | /** |
||
| 752 | * Given a name, this function will return a page handle. These handles |
||
| 753 | * will only contain latin characters |
||
| 754 | * |
||
| 755 | * @param string $name |
||
| 756 | * The Page name to generate a handle for |
||
| 757 | * @return string |
||
| 758 | */ |
||
| 759 | public static function createHandle($name) |
||
| 766 | |||
| 767 | /** |
||
| 768 | * This function will return the number of child pages for a given |
||
| 769 | * `$page_id`. This is a recursive function and will return the absolute |
||
| 770 | * count. |
||
| 771 | * |
||
| 772 | * @param integer $page_id |
||
| 773 | * The ID of the Page. |
||
| 774 | * @return integer |
||
| 775 | * The number of child pages for the given `$page_id` |
||
| 776 | */ |
||
| 777 | public static function getChildPagesCount($page_id = null) |
||
| 796 | |||
| 797 | /** |
||
| 798 | * Returns boolean if a the given `$type` has been used by Symphony |
||
| 799 | * for a Page that is not `$page_id`. |
||
| 800 | * |
||
| 801 | * @param integer $page_id |
||
| 802 | * The ID of the Page to exclude from the query. |
||
| 803 | * @param string $type |
||
| 804 | * The Page Type to look for in `tbl_page_types`. |
||
| 805 | * @return boolean |
||
| 806 | * True if the type is used, false otherwise |
||
| 807 | */ |
||
| 808 | public static function hasPageTypeBeenUsed($page_id = null, $type) |
||
| 826 | |||
| 827 | /** |
||
| 828 | * Given a `$page_id`, this function returns boolean if the page |
||
| 829 | * has child pages. |
||
| 830 | * |
||
| 831 | * @param integer $page_id |
||
| 832 | * The ID of the Page to check |
||
| 833 | * @return boolean |
||
| 834 | * True if the page has children, false otherwise |
||
| 835 | */ |
||
| 836 | public static function hasChildPages($page_id = null) |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Given the `$page_id`, return the complete title of the |
||
| 853 | * current page. Each part of the Page's title will be |
||
| 854 | * separated by ': '. |
||
| 855 | * |
||
| 856 | * @param mixed $page_id |
||
| 857 | * The ID of the Page that currently being viewed, or the handle of the |
||
| 858 | * current Page |
||
| 859 | * @return string |
||
| 860 | * The title of the current Page. If the page is a child of another |
||
| 861 | * it will be prepended by the parent and a colon, ie. Articles: Read |
||
| 862 | */ |
||
| 863 | public static function resolvePageTitle($page_id) |
||
| 869 | |||
| 870 | /** |
||
| 871 | * Given the `$page_id` and a `$column`, this function will return an |
||
| 872 | * array of the given `$column` for the Page, including all parents. |
||
| 873 | * |
||
| 874 | * @param mixed $page_id |
||
| 875 | * The ID of the Page that currently being viewed, or the handle of the |
||
| 876 | * current Page |
||
| 877 | * @param string $column |
||
| 878 | * @return array |
||
| 879 | * An array of the current Page, containing the `$column` |
||
| 880 | * requested. The current page will be the last item the array, as all |
||
| 881 | * parent pages are prepended to the start of the array |
||
| 882 | */ |
||
| 883 | public static function resolvePage($page_id, $column) |
||
| 934 | |||
| 935 | /** |
||
| 936 | * Given the `$page_id`, return the complete path to the |
||
| 937 | * current page. Each part of the Page's path will be |
||
| 938 | * separated by '/'. |
||
| 939 | * |
||
| 940 | * @param mixed $page_id |
||
| 941 | * The ID of the Page that currently being viewed, or the handle of the |
||
| 942 | * current Page |
||
| 943 | * @return string |
||
| 944 | * The complete path to the current Page including any parent |
||
| 945 | * Pages, ie. /articles/read |
||
| 946 | */ |
||
| 947 | public static function resolvePagePath($page_id) |
||
| 953 | |||
| 954 | /** |
||
| 955 | * Resolve a page by it's handle and path |
||
| 956 | * |
||
| 957 | * @param string $handle |
||
| 958 | * The handle of the page |
||
| 959 | * @param boolean $path |
||
| 960 | * The path to the page |
||
| 961 | * @return array|boolean |
||
| 962 | * Array if found, false if not |
||
| 963 | */ |
||
| 964 | public static function resolvePageByPath($handle, $path = false) |
||
| 979 | |||
| 980 | /** |
||
| 981 | * Check whether a datasource is used or not |
||
| 982 | * |
||
| 983 | * @param string $handle |
||
| 984 | * The datasource handle |
||
| 985 | * @return boolean |
||
| 986 | * True if used, false if not |
||
| 987 | */ |
||
| 988 | public static function isDataSourceUsed($handle) |
||
| 993 | |||
| 994 | /** |
||
| 995 | * Check whether a event is used or not |
||
| 996 | * |
||
| 997 | * @param string $handle |
||
| 998 | * The event handle |
||
| 999 | * @return boolean |
||
| 1000 | * True if used, false if not |
||
| 1001 | */ |
||
| 1002 | public static function isEventUsed($handle) |
||
| 1007 | } |
||
| 1008 |
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.