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 AdministrationPage 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 AdministrationPage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class AdministrationPage extends HTMLPage |
||
16 | { |
||
17 | /** |
||
18 | * An array of `Alert` objects used to display page level |
||
19 | * messages to Symphony backend users one by one. Prior to Symphony 2.3 |
||
20 | * this variable only held a single `Alert` object. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | public $Alert = array(); |
||
25 | |||
26 | /** |
||
27 | * As the name suggests, a `<div>` that holds the following `$Header`, |
||
28 | * `$Contents` and `$Footer`. |
||
29 | * |
||
30 | * @var XMLElement |
||
31 | */ |
||
32 | public $Wrapper = null; |
||
33 | |||
34 | /** |
||
35 | * A `<div>` that contains the header of a Symphony backend page, which |
||
36 | * typically contains the Site title and the navigation. |
||
37 | * |
||
38 | * @var XMLElement |
||
39 | */ |
||
40 | public $Header = null; |
||
41 | |||
42 | /** |
||
43 | * A `<div>` that contains the breadcrumbs, the page title and some contextual |
||
44 | * actions (e.g. "Create new"). |
||
45 | * |
||
46 | * @since Symphony 2.3 |
||
47 | * @var XMLElement |
||
48 | */ |
||
49 | public $Context = null; |
||
50 | |||
51 | /** |
||
52 | * An object that stores the markup for the breadcrumbs and is only used |
||
53 | * internally. |
||
54 | * |
||
55 | * @since Symphony 2.3 |
||
56 | * @var XMLElement |
||
57 | */ |
||
58 | public $Breadcrumbs = null; |
||
59 | |||
60 | /** |
||
61 | * An array of Drawer widgets for the current page |
||
62 | * |
||
63 | * @since Symphony 2.3 |
||
64 | * @var array |
||
65 | */ |
||
66 | public $Drawer = array(); |
||
67 | |||
68 | /** |
||
69 | * A `<div>` that contains the content of a Symphony backend page. |
||
70 | * |
||
71 | * @var XMLElement |
||
72 | */ |
||
73 | public $Contents = null; |
||
74 | |||
75 | /** |
||
76 | * An associative array of the navigation where the key is the group |
||
77 | * index, and the value is an associative array of 'name', 'index' and |
||
78 | * 'children'. Name is the name of the this group, index is the same as |
||
79 | * the key and children is an associative array of navigation items containing |
||
80 | * the keys 'link', 'name' and 'visible'. In Symphony, all navigation items |
||
81 | * are contained within a group, and the group has no 'default' link, therefore |
||
82 | * it is up to the children to provide the link to pages. This link should be |
||
83 | * relative to the Symphony path, although it is possible to provide an |
||
84 | * absolute link by providing a key, 'relative' with the value false. |
||
85 | * |
||
86 | * @var array |
||
87 | */ |
||
88 | public $_navigation = array(); |
||
89 | |||
90 | /** |
||
91 | * An associative array describing this pages context. This |
||
92 | * can include the section handle, the current entry_id, the page |
||
93 | * name and any flags such as 'saved' or 'created'. This variable |
||
94 | * often provided in delegates so extensions can manipulate based |
||
95 | * off the current context or add new keys. |
||
96 | * |
||
97 | * @var array |
||
98 | */ |
||
99 | public $_context = null; |
||
100 | |||
101 | /** |
||
102 | * The class attribute of the `<body>` element for this page. Defaults |
||
103 | * to an empty string |
||
104 | * |
||
105 | * @var string |
||
106 | */ |
||
107 | private $_body_class = ''; |
||
108 | |||
109 | /** |
||
110 | * Constructor calls the parent constructor to set up |
||
111 | * the basic HTML, Head and Body `XMLElement`'s. This function |
||
112 | * also sets the `XMLElement` element style to be HTML, instead of XML |
||
113 | */ |
||
114 | public function __construct() |
||
120 | |||
121 | /** |
||
122 | * Specifies the type of page that being created. This is used to |
||
123 | * trigger various styling hooks. If your page is mainly a form, |
||
124 | * pass 'form' as the parameter, if it's displaying a single entry, |
||
125 | * pass 'single'. If any other parameter is passed, the 'index' |
||
126 | * styling will be applied. |
||
127 | * |
||
128 | * @param string $type |
||
129 | * Accepts 'form' or 'single', any other `$type` will trigger 'index' |
||
130 | * styling. |
||
131 | */ |
||
132 | public function setPageType($type = 'form') |
||
136 | |||
137 | /** |
||
138 | * Setter function to set the class attribute on the `<body>` element. |
||
139 | * This function will respect any previous classes that have been added |
||
140 | * to this `<body>` |
||
141 | * |
||
142 | * @param string $class |
||
143 | * The string of the classname, multiple classes can be specified by |
||
144 | * uses a space separator |
||
145 | */ |
||
146 | public function setBodyClass($class) |
||
155 | |||
156 | /** |
||
157 | * Given the current page `$context` and the URL path parts, parse the context for |
||
158 | * the current page. This happens prior to the AdminPagePostCallback delegate |
||
159 | * being fired. The `$context` is passed by reference |
||
160 | * |
||
161 | * @since Symphony 3.0.0 |
||
162 | * @param array $context |
||
163 | * @param array $parts |
||
164 | * @return void |
||
165 | */ |
||
166 | public function parseContext(array &$context, array $parts) |
||
169 | |||
170 | /** |
||
171 | * Accessor for `$this->_context` which includes contextual information |
||
172 | * about the current page such as the class, file location or page root. |
||
173 | * This information varies depending on if the page is provided by an |
||
174 | * extension, is for the publish area, is the login page or any other page |
||
175 | * |
||
176 | * @since Symphony 2.3 |
||
177 | * @return array |
||
178 | */ |
||
179 | public function getContext() |
||
183 | |||
184 | /** |
||
185 | * Given a `$message` and an optional `$type`, this function will |
||
186 | * add an Alert instance into this page's `$this->Alert` property. |
||
187 | * Since Symphony 2.3, there may be more than one `Alert` per page. |
||
188 | * Unless the Alert is an Error, it is required the `$message` be |
||
189 | * passed to this function. |
||
190 | * |
||
191 | * @param string $message |
||
192 | * The message to display to users |
||
193 | * @param string $type |
||
194 | * An Alert constant, being `Alert::NOTICE`, `Alert::ERROR` or |
||
195 | * `Alert::SUCCESS`. The differing types will show the error |
||
196 | * in a different style in the backend. If omitted, this defaults |
||
197 | * to `Alert::NOTICE`. |
||
198 | * @throws Exception |
||
199 | */ |
||
200 | public function pageAlert($message = null, $type = Alert::NOTICE) |
||
214 | |||
215 | /** |
||
216 | * Appends the heading of this Symphony page to the Context element. |
||
217 | * Action buttons can be provided (e.g. "Create new") as second parameter. |
||
218 | * |
||
219 | * @since Symphony 2.3 |
||
220 | * @param string $value |
||
221 | * The heading text |
||
222 | * @param array|XMLElement|string $actions |
||
223 | * Some contextual actions to append to the heading, they can be provided as |
||
224 | * an array of XMLElements or strings. Traditionally Symphony uses this to append |
||
225 | * a "Create new" link to the Context div. |
||
226 | */ |
||
227 | public function appendSubheading($value, $actions = null) |
||
242 | |||
243 | /** |
||
244 | * This function allows a user to insert an Action button to the page. |
||
245 | * It accepts an `XMLElement` (which should be of the `Anchor` type), |
||
246 | * an optional parameter `$prepend`, which when `true` will add this |
||
247 | * action before any existing actions. |
||
248 | * |
||
249 | * @since Symphony 2.3 |
||
250 | * @see core.Widget#Anchor |
||
251 | * @param XMLElement $action |
||
252 | * An Anchor element to add to the top of the page. |
||
253 | * @param boolean $append |
||
254 | * If true, this will add the `$action` after existing actions, otherwise |
||
255 | * it will be added before existing actions. By default this is `true`, |
||
256 | * which will add the `$action` after current actions. |
||
257 | */ |
||
258 | public function insertAction(XMLElement $action, $append = true) |
||
279 | |||
280 | /** |
||
281 | * Allows developers to specify a list of nav items that build the |
||
282 | * path to the current page or, in jargon, "breadcrumbs". |
||
283 | * |
||
284 | * @since Symphony 2.3 |
||
285 | * @param array $values |
||
286 | * An array of `XMLElement`'s or strings that compose the path. If breadcrumbs |
||
287 | * already exist, any new item will be appended to the rightmost part of the |
||
288 | * path. |
||
289 | */ |
||
290 | public function insertBreadcrumbs(array $values) |
||
314 | |||
315 | /** |
||
316 | * Allows a Drawer element to added to the backend page in one of three |
||
317 | * positions, `horizontal`, `vertical-left` or `vertical-right`. The button |
||
318 | * to trigger the visibility of the drawer will be added after existing |
||
319 | * actions by default. |
||
320 | * |
||
321 | * @since Symphony 2.3 |
||
322 | * @see core.Widget#Drawer |
||
323 | * @param XMLElement $drawer |
||
324 | * An XMLElement representing the drawer, use `Widget::Drawer` to construct |
||
325 | * @param string $position |
||
326 | * Where `$position` can be `horizontal`, `vertical-left` or |
||
327 | * `vertical-right`. Defaults to `horizontal`. |
||
328 | * @param string $button |
||
329 | * If not passed, a button to open/close the drawer will not be added |
||
330 | * to the interface. Accepts 'prepend' or 'append' values, which will |
||
331 | * add the button before or after existing buttons. Defaults to `prepend`. |
||
332 | * If any other value is passed, no button will be added. |
||
333 | * @throws InvalidArgumentException |
||
334 | */ |
||
335 | public function insertDrawer(XMLElement $drawer, $position = 'horizontal', $button = 'append') |
||
354 | |||
355 | /** |
||
356 | * This function initialises a lot of the basic elements that make up a Symphony |
||
357 | * backend page such as the default stylesheets and scripts, the navigation and |
||
358 | * the footer. Any alerts are also appended by this function. `view()` is called to |
||
359 | * build the actual content of the page. The `InitialiseAdminPageHead` delegate |
||
360 | * allows extensions to add elements to the `<head>`. |
||
361 | * |
||
362 | * @see view() |
||
363 | * @uses InitialiseAdminPageHead |
||
364 | * @param array $context |
||
365 | * An associative array describing this pages context. This |
||
366 | * can include the section handle, the current entry_id, the page |
||
367 | * name and any flags such as 'saved' or 'created'. This list is not exhaustive |
||
368 | * and extensions can add their own keys to the array. |
||
369 | * @throws InvalidArgumentException |
||
370 | * @throws SymphonyErrorPage |
||
371 | */ |
||
372 | public function build(array $context = array()) |
||
499 | |||
500 | /** |
||
501 | * Checks the current Symphony Author can access the current page. |
||
502 | * This check uses the `ASSETS . /xml/navigation.xml` file to determine |
||
503 | * if the current page (or the current page namespace) can be viewed |
||
504 | * by the currently logged in Author. |
||
505 | * |
||
506 | * @link http://github.com/symphonycms/symphony-2/blob/master/symphony/assets/xml/navigation.xml |
||
507 | * @return boolean |
||
508 | * True if the Author can access the current page, false otherwise |
||
509 | */ |
||
510 | public function canAccessPage() |
||
545 | |||
546 | /** |
||
547 | * Returns the `$_navigation` variable of this Page. If it is empty, |
||
548 | * it will be built by `__buildNavigation` |
||
549 | * |
||
550 | * @see __buildNavigation() |
||
551 | * @return array |
||
552 | */ |
||
553 | public function getNavigationArray() |
||
561 | |||
562 | /** |
||
563 | * This function populates the `$_navigation` array with an associative array |
||
564 | * of all the navigation groups and their links. Symphony only supports one |
||
565 | * level of navigation, so children links cannot have children links. The default |
||
566 | * Symphony navigation is found in the `ASSETS/xml/navigation.xml` folder. This is |
||
567 | * loaded first, and then the Section navigation is built, followed by the Extension |
||
568 | * navigation. Additionally, this function will set the active group of the navigation |
||
569 | * by checking the current page against the array of links. |
||
570 | * |
||
571 | * @link https://github.com/symphonycms/symphony-2/blob/master/symphony/assets/xml/navigation.xml |
||
572 | * @link https://github.com/symphonycms/symphony-2/blob/master/symphony/lib/toolkit/class.extension.php |
||
573 | */ |
||
574 | public function __buildNavigation() |
||
597 | |||
598 | /** |
||
599 | * This method fills the `$nav` array with value |
||
600 | * from the `ASSETS/xml/navigation.xml` file |
||
601 | * |
||
602 | * @link http://github.com/symphonycms/symphony-2/blob/master/symphony/assets/xml/navigation.xml |
||
603 | * |
||
604 | * @since Symphony 2.3.2 |
||
605 | * |
||
606 | * @param array $nav |
||
607 | * The navigation array that will receive nav nodes |
||
608 | */ |
||
609 | private function buildXmlNavigation(&$nav) |
||
658 | |||
659 | /** |
||
660 | * This method fills the `$nav` array with value |
||
661 | * from each Section |
||
662 | * |
||
663 | * @since Symphony 2.3.2 |
||
664 | * |
||
665 | * @param array $nav |
||
666 | * The navigation array that will receive nav nodes |
||
667 | */ |
||
668 | private function buildSectionNavigation(&$nav) |
||
698 | |||
699 | /** |
||
700 | * Given an associative array representing the navigation, and a group, |
||
701 | * this function will attempt to return the index of the group in the navigation |
||
702 | * array. If it is found, it will return the index, otherwise it will return false. |
||
703 | * |
||
704 | * @param array $nav |
||
705 | * An associative array of the navigation where the key is the group |
||
706 | * index, and the value is an associative array of 'name', 'index' and |
||
707 | * 'children'. Name is the name of the this group, index is the same as |
||
708 | * the key and children is an associative array of navigation items containing |
||
709 | * the keys 'link', 'name' and 'visible'. The 'haystack'. |
||
710 | * @param string $group |
||
711 | * The group name to find, the 'needle'. |
||
712 | * @return integer|boolean |
||
713 | * If the group is found, the index will be returned, otherwise false. |
||
714 | */ |
||
715 | private static function __navigationFindGroupIndex(array $nav, $group) |
||
725 | |||
726 | /** |
||
727 | * This method fills the `$nav` array with value |
||
728 | * from each Extension's `fetchNavigation` method |
||
729 | * |
||
730 | * @since Symphony 2.3.2 |
||
731 | * |
||
732 | * @param array $nav |
||
733 | * The navigation array that will receive nav nodes |
||
734 | * @throws Exception |
||
735 | * @throws SymphonyErrorPage |
||
736 | */ |
||
737 | private function buildExtensionsNavigation(&$nav) |
||
796 | |||
797 | /** |
||
798 | * This function builds out a navigation menu item for parents. Parents display |
||
799 | * in the top level navigation of the backend and may have children (dropdown menus) |
||
800 | * |
||
801 | * @since Symphony 2.5.1 |
||
802 | * @param integer $index |
||
803 | * @param array $item |
||
804 | * @return array |
||
805 | */ |
||
806 | private static function createParentNavItem($index, $item) |
||
818 | |||
819 | /** |
||
820 | * This function builds out a navigation menu item for children. Children |
||
821 | * live under a parent navigation item and are shown on hover. |
||
822 | * |
||
823 | * @since Symphony 2.5.1 |
||
824 | * @param array $item |
||
825 | * @param string $extension_handle |
||
826 | * @return array |
||
827 | */ |
||
828 | private static function createChildNavItem($item, $extension_handle) |
||
846 | |||
847 | // Errors first, success next, then notices. |
||
848 | |||
849 | /** |
||
850 | * Given the navigation array, this function will loop over all the items |
||
851 | * to determine which is the 'active' navigation group, or in other words, |
||
852 | * what group best represents the current page `$this->Author` is viewing. |
||
853 | * This is done by checking the current page's link against all the links |
||
854 | * provided in the `$nav`, and then flagging the group of the found link |
||
855 | * with an 'active' CSS class. The current page's link omits any flags or |
||
856 | * URL parameters and just uses the root page URL. |
||
857 | * |
||
858 | * @param array $nav |
||
859 | * An associative array of the navigation where the key is the group |
||
860 | * index, and the value is an associative array of 'name', 'index' and |
||
861 | * 'children'. Name is the name of the this group, index is the same as |
||
862 | * the key and children is an associative array of navigation items containing |
||
863 | * the keys 'link', 'name' and 'visible'. The 'haystack'. This parameter is passed |
||
864 | * by reference to this function. |
||
865 | * @param string $pageroot |
||
866 | * The current page the Author is the viewing, minus any flags or URL |
||
867 | * parameters such as a Symphony object ID. eg. Section ID, Entry ID. This |
||
868 | * parameter is also be a regular expression, but this is highly unlikely. |
||
869 | * @param boolean $pattern |
||
870 | * If set to true, the `$pageroot` represents a regular expression which will |
||
871 | * determine if the active navigation item |
||
872 | * @return boolean |
||
873 | * Returns true if an active link was found, false otherwise. If true, the |
||
874 | * navigation group of the active link will be given the CSS class 'active' |
||
875 | */ |
||
876 | private static function __findActiveNavigationGroup(array &$nav, $pageroot, $pattern = false) |
||
896 | |||
897 | /** |
||
898 | * Given the limit of the current navigation item or page, this function |
||
899 | * returns if the current Author can access that item or not. |
||
900 | * |
||
901 | * @since Symphony 2.5.1 |
||
902 | * @param string $item_limit |
||
903 | * @return boolean |
||
904 | */ |
||
905 | public function doesAuthorHaveAccess($item_limit = null) |
||
921 | |||
922 | /** |
||
923 | * This function is called when `$_REQUEST` contains a key of 'action'. |
||
924 | * Any logic that needs to occur immediately for the action to complete |
||
925 | * should be contained within this function. By default this calls the |
||
926 | * `__switchboard` with the type set to 'action'. |
||
927 | * |
||
928 | * @see __switchboard() |
||
929 | */ |
||
930 | public function action() |
||
934 | |||
935 | /** |
||
936 | * The `__switchboard` function acts as a controller to display content |
||
937 | * based off the $type. By default, the `$type` is 'view' but it can be set |
||
938 | * also set to 'action'. The `$type` is prepended by __ and the context is |
||
939 | * append to the $type to create the name of the function that will provide |
||
940 | * that logic. For example, if the $type was action and the context of the |
||
941 | * current page was new, the resulting function to be called would be named |
||
942 | * `__actionNew()`. If an action function is not provided by the Page, this function |
||
943 | * returns nothing, however if a view function is not provided, a 404 page |
||
944 | * will be returned. |
||
945 | * |
||
946 | * @param string $type |
||
947 | * Either 'view' or 'action', by default this will be 'view' |
||
948 | * @throws SymphonyErrorPage |
||
949 | */ |
||
950 | public function __switchboard($type = 'view') |
||
971 | |||
972 | /** |
||
973 | * Creates the Symphony footer for an Administration page. By default |
||
974 | * this includes the installed Symphony version and the currently logged |
||
975 | * in Author. A delegate is provided to allow extensions to manipulate the |
||
976 | * footer HTML, which is an XMLElement of a `<ul>` element. |
||
977 | * Since Symphony 2.3, it no longer uses the `AddElementToFooter` delegate. |
||
978 | */ |
||
979 | public function appendUserLinks() |
||
999 | |||
1000 | /** |
||
1001 | * This function will append the Navigation to the AdministrationPage. |
||
1002 | * It fires a delegate, NavigationPreRender, to allow extensions to manipulate |
||
1003 | * the navigation. Extensions should not use this to add their own navigation, |
||
1004 | * they should provide the navigation through their fetchNavigation function. |
||
1005 | * Note with the Section navigation groups, if there is only one section in a group |
||
1006 | * and that section is set to visible, the group will not appear in the navigation. |
||
1007 | * |
||
1008 | * @uses NavigationPreRender |
||
1009 | * @see getNavigationArray() |
||
1010 | * @see toolkit.Extension#fetchNavigation() |
||
1011 | */ |
||
1012 | public function appendNavigation() |
||
1087 | |||
1088 | /** |
||
1089 | * Called to build the content for the page. This function immediately calls |
||
1090 | * `__switchboard()` which acts a bit of a controller to show content based on |
||
1091 | * off a type, such as 'view' or 'action'. `AdministrationPages` can override this |
||
1092 | * function to just display content if they do not need the switchboard functionality |
||
1093 | * |
||
1094 | * @see __switchboard() |
||
1095 | */ |
||
1096 | public function view() |
||
1100 | |||
1101 | /** |
||
1102 | * If `$this->Alert` is set, it will be added to this page. The |
||
1103 | * `AppendPageAlert` delegate is fired to allow extensions to provide their |
||
1104 | * their own Alert messages for this page. Since Symphony 2.3, there may be |
||
1105 | * more than one `Alert` per page. Alerts are displayed in the order of |
||
1106 | * severity, with Errors first, then Success alerts followed by Notices. |
||
1107 | * |
||
1108 | * @uses AppendPageAlert |
||
1109 | */ |
||
1110 | public function appendAlert() |
||
1139 | |||
1140 | /** |
||
1141 | * Appends the `$this->Header`, `$this->Context` and `$this->Contents` |
||
1142 | * to `$this->Wrapper` before adding the ID and class attributes for |
||
1143 | * the `<body>` element. This function will also place any Drawer elements |
||
1144 | * in their relevant positions in the page. After this has completed the |
||
1145 | * parent `generate()` is called which will convert the `XMLElement`'s |
||
1146 | * into strings ready for output. |
||
1147 | * |
||
1148 | * @see core.HTMLPage#generate() |
||
1149 | * @param null $page |
||
1150 | * @return string |
||
1151 | */ |
||
1152 | public function generate($page = null) |
||
1182 | |||
1183 | /** |
||
1184 | * Uses this pages PHP classname as the `<body>` ID attribute. |
||
1185 | * This function removes 'content' from the start of the classname |
||
1186 | * and converts all uppercase letters to lowercase and prefixes them |
||
1187 | * with a hyphen. |
||
1188 | */ |
||
1189 | private function __appendBodyId() |
||
1210 | |||
1211 | /** |
||
1212 | * Given the context of the current page, which is an associative |
||
1213 | * array, this function will append the values to the page's body as |
||
1214 | * data attributes. If an context value is numeric it will be given |
||
1215 | * the key 'id' otherwise all attributes will be prefixed by the context key. |
||
1216 | * |
||
1217 | * If the context value is an array, it will be JSON encoded. |
||
1218 | * |
||
1219 | * @param array $context |
||
1220 | */ |
||
1221 | private function __appendBodyAttributes(array $context = array()) |
||
1241 | |||
1242 | public function sortAlerts($a, $b) |
||
1257 | } |
||
1258 |
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.