| Total Complexity | 47 |
| Total Lines | 326 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 4 | Features | 1 |
Complex classes like PageType 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.
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 PageType, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class PageType { |
||
| 20 | |||
| 21 | protected $page = null; |
||
| 22 | |||
| 23 | public function __construct() { |
||
| 24 | // |
||
| 25 | } |
||
| 26 | |||
| 27 | public function setPage (Page &$page) { |
||
| 29 | } |
||
| 30 | |||
| 31 | protected function getPage () : Page { |
||
| 33 | } |
||
| 34 | |||
| 35 | public function showDesign () { |
||
| 36 | return true; |
||
| 37 | } |
||
| 38 | |||
| 39 | public function getContentType () : string { |
||
| 40 | return "" . $this->page->getContentType() . "; charset=" . $this->getCharset(); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function getCharset () : string { |
||
| 45 | } |
||
| 46 | |||
| 47 | public function setCustomHeader () { |
||
| 48 | // |
||
| 49 | } |
||
| 50 | |||
| 51 | public function getAdditionalHeaderCode () : string { |
||
| 52 | return ""; |
||
| 53 | } |
||
| 54 | |||
| 55 | public function showFooter () : bool { |
||
| 57 | } |
||
| 58 | |||
| 59 | public function getFooterScripts () : string { |
||
| 61 | } |
||
| 62 | |||
| 63 | public function showHTMLComments () : bool { |
||
| 65 | } |
||
| 66 | |||
| 67 | public function getOgTitle () : string { |
||
| 69 | } |
||
| 70 | |||
| 71 | public function getOgDescription () : string { |
||
| 72 | return $this->page->getOgDescription(); |
||
| 73 | } |
||
| 74 | |||
| 75 | public function getOgType () : string { |
||
| 76 | return $this->page->getOgType(); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function getOgImage () : string { |
||
| 80 | return $this->page->getOgImage(); |
||
| 81 | } |
||
| 82 | |||
| 83 | public function getOgUrl () : string { |
||
| 84 | return DomainUtils::generateURL($this->page->getAlias()); |
||
| 85 | } |
||
| 86 | |||
| 87 | public function getOgTags () : string { |
||
| 88 | $tags = array(); |
||
| 89 | $tags['og:type'] = $this->getOgType(); |
||
| 90 | $tags['og:url'] = $this->getOgUrl(); |
||
| 91 | $tags['og:title'] = $this->getOgTitle(); |
||
| 92 | $tags['og:description'] = $this->getOgDescription(); |
||
| 93 | |||
| 94 | if (!empty($this->getOgImage())) { |
||
| 95 | $tags['og:image'] = $this->getOgImage(); |
||
| 96 | } |
||
| 97 | |||
| 98 | $this->getAdditionalTags($tags); |
||
| 99 | |||
| 100 | Events::throwEvent("og_tags", array( |
||
| 101 | 'page' => &$this->page, |
||
| 102 | 'page_type' => &$this, |
||
| 103 | 'tags' => &$tags |
||
| 104 | )); |
||
| 105 | |||
| 106 | $tags_lines = "<!-- OpenGraph tags -->\r\n"; |
||
| 107 | |||
| 108 | foreach ($tags as $property=>$content) { |
||
| 109 | $tags_lines .= "\t<meta property=\"" . $property . "\" content=\"" . $content . "\" />\r\n"; |
||
| 110 | } |
||
| 111 | |||
| 112 | return $tags_lines; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * add additional tags |
||
| 117 | * |
||
| 118 | * This method was added so that page types doesn't have to override getOgTags() completely |
||
| 119 | */ |
||
| 120 | protected function getAdditionalTags (array &$tags) { |
||
|
|
|||
| 121 | //add additional tags |
||
| 122 | } |
||
| 123 | |||
| 124 | public function getContent () : string { |
||
| 125 | $content = $this->getPage()->getContent(); |
||
| 126 | |||
| 127 | //check, if page has custom template |
||
| 128 | if ($this->getPage()->hasCustomTemplate()) { |
||
| 129 | //get custom template |
||
| 130 | $template = Validator_String::get($this->getPage()->getCustomTemplate()); |
||
| 131 | |||
| 132 | $current_style = Registry::singleton()->getSetting("current_style_name"); |
||
| 133 | |||
| 134 | //check, if custom template exists |
||
| 135 | if (file_exists(STYLE_PATH . $current_style . "/" . $template)) { |
||
| 136 | $template = new Template($template); |
||
| 137 | |||
| 138 | $template->assign("TITLE", $this->getPage()->getTitle()); |
||
| 139 | $template->assign("CONTENT", $content); |
||
| 140 | |||
| 141 | $template->parse("main"); |
||
| 142 | $content = $template->getCode(); |
||
| 143 | } else { |
||
| 144 | throw new IllegalStateException("Custom template '" . $template . "' doesnt exists."); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | Events::throwEvent("get_content", array( |
||
| 149 | 'content' => &$content, |
||
| 150 | 'page' => &$this->page, |
||
| 151 | 'page_type' => &$this |
||
| 152 | )); |
||
| 153 | |||
| 154 | return $content; |
||
| 155 | } |
||
| 156 | |||
| 157 | public function generateNormalPage (string $content, $vars = array()) : string { |
||
| 158 | $current_style = Registry::singleton()->getSetting("current_style_name"); |
||
| 159 | |||
| 160 | if (file_exists(STYLE_PATH . $current_style . "/pages/normal.tpl")) { |
||
| 161 | $template = new DwooTemplate(STYLE_PATH . $current_style . "/pages/normal.tpl"); |
||
| 162 | |||
| 163 | $title_preafix = Settings::get("title_praefix", ""); |
||
| 164 | $title_suffix = Settings::get("title_suffix", ""); |
||
| 165 | |||
| 166 | //translate title |
||
| 167 | $title = Translator::translateTitle($this->getPage()->getTitle()); |
||
| 168 | |||
| 169 | $template->assign("RAW_TITLE", $title); |
||
| 170 | $template->assign("TITLE", $title_preafix . $title . $title_suffix); |
||
| 171 | $template->assign("CONTENT", $content); |
||
| 172 | $template->assign("FOOTER", Registry::singleton()->getSetting("footer", "")); |
||
| 173 | |||
| 174 | Events::throwEvent("generate_normal_page", array( |
||
| 175 | 'template' => &$template, |
||
| 176 | 'current_style' => $current_style, |
||
| 177 | 'content' => &$content, |
||
| 178 | 'page_type' => &$this, |
||
| 179 | 'page' => $this->getPage() |
||
| 180 | )); |
||
| 181 | |||
| 182 | foreach ($vars as $key=>$value) { |
||
| 183 | $template->assign($key, $value); |
||
| 184 | } |
||
| 185 | |||
| 186 | return $template->getCode(); |
||
| 187 | } else { |
||
| 188 | throw new IllegalStateException("no normal template (pages/normal.tpl) found!"); |
||
| 189 | //return $content; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | public function checkPermissions (PermissionChecker $permission_checker) { |
||
| 194 | //first, check required permissions |
||
| 195 | if (count($this->listRequiredPermissions()) > 0) { |
||
| 196 | $bool = false; |
||
| 197 | |||
| 198 | foreach ($this->listRequiredPermissions() as $permission) { |
||
| 199 | if ($permission_checker->hasRight($permission)) { |
||
| 200 | $bool = true; |
||
| 201 | break; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | if (!$bool) { |
||
| 206 | return false; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | if (!$this->getPage()->hasCustomPermissions()) { |
||
| 211 | return true; |
||
| 212 | } else { |
||
| 213 | $permissions = $this->getPage()->listCustomPermissions(); |
||
| 214 | |||
| 215 | foreach ($permissions as $permission) { |
||
| 216 | if ($permission_checker->hasRight($permission)) { |
||
| 217 | return true; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | return false; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | protected function listRequiredPermissions () : array { |
||
| 226 | return array(); |
||
| 227 | } |
||
| 228 | |||
| 229 | public function exitAfterOutput () { |
||
| 230 | return false; |
||
| 231 | } |
||
| 232 | |||
| 233 | public static function reloadCache () { |
||
| 235 | } |
||
| 236 | |||
| 237 | public static function listPageTypes (bool $advanced = false) : array { |
||
| 238 | $rows = array(); |
||
| 258 | } |
||
| 259 | |||
| 260 | public static function createPageType (string $class_name, string $title, bool $advanced = false, int $order = 10, array $permissions = array("none"), string $owner = "system") { |
||
| 303 | } |
||
| 304 | |||
| 305 | public static function removePageType (string $class_name) { |
||
| 327 | } |
||
| 328 | } |
||
| 329 | |||
| 330 | public static function removePageTypesByOwner (string $owner) { |
||
| 331 | Database::getInstance()->execute("DELETE FROM `{praefix}page_types` WHERE `owner` = :owner; ", array('owner' => $owner)); |
||
| 332 | |||
| 333 | //TODO: change all pages which have this page type |
||
| 334 | |||
| 335 | //clear cache |
||
| 336 | Cache::clear("pagetypes"); |
||
| 337 | } |
||
| 338 | |||
| 339 | public static function exists (string $class_name) : bool { |
||
| 345 | } |
||
| 346 | |||
| 347 | } |
||
| 348 | |||
| 349 | ?> |
||
| 350 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.