Conditions | 19 |
Paths | 21 |
Total Lines | 179 |
Code Lines | 110 |
Lines | 0 |
Ratio | 0 % |
Changes | 25 | ||
Bugs | 2 | Features | 12 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
34 | public function getContent(): string { |
||
35 | $template = new DwooTemplate("pages/editpage"); |
||
36 | |||
37 | //check, if pageID is set |
||
38 | if (!isset($_REQUEST['edit']) || empty($_REQUEST['edit'])) { |
||
39 | //show error |
||
40 | return $this->showError("No pageID was set!"); |
||
41 | } |
||
42 | |||
43 | $pageID = (int) $_REQUEST['edit']; |
||
44 | |||
45 | $page = new Page(); |
||
46 | $page->loadByID($pageID); |
||
47 | |||
48 | //first check permissions |
||
49 | if (!PermissionChecker::current()->hasRight("can_edit_all_pages") && !(PermissionChecker::current()->hasRight("can_edit_own_pages") && $page->getAuthorID() == User::current()->getID())) { |
||
50 | //user doesn't have permissions to edit this page |
||
51 | return $this->showError("You don't have permissions to edit this page!"); |
||
52 | } |
||
53 | |||
54 | //first, lock page |
||
55 | Page::lockPage($page->getPageID(), User::current()->getID()); |
||
56 | |||
57 | $success_messages = array(); |
||
58 | $error_messages = array(); |
||
59 | |||
60 | //save page |
||
61 | if (isset($_REQUEST['submit'])) { |
||
62 | if ($_REQUEST['submit'] === "Save") { |
||
63 | //save page |
||
64 | $res = $this->save($page); |
||
65 | |||
66 | if ($res === true) { |
||
67 | $success_messages[] = "Saved page successfully!"; |
||
68 | } else { |
||
69 | $error_messages[] = $res; |
||
70 | } |
||
71 | } else if ($_REQUEST['submit'] === "SaveUnlock") { |
||
72 | //save page |
||
73 | $res = $this->save($page); |
||
74 | |||
75 | if ($res === true) { |
||
76 | //unlock page |
||
77 | Page::unlockPage($page->getPageID()); |
||
78 | |||
79 | //redirect to admin/pages |
||
80 | header("Location: " . DomainUtils::generateURL("admin/pages")); |
||
81 | |||
82 | ob_flush(); |
||
83 | ob_end_flush(); |
||
84 | |||
85 | exit; |
||
86 | } else { |
||
87 | $error_messages[] = $res; |
||
88 | } |
||
89 | } else if ($_REQUEST['submit'] === "Publish") { |
||
90 | //save page |
||
91 | $res = $this->save($page); |
||
92 | |||
93 | if ($res === true) { |
||
94 | $success_messages[] = "Saved page successfully!"; |
||
95 | } else { |
||
96 | $error_messages[] = $res; |
||
97 | } |
||
98 | |||
99 | //publish page |
||
100 | $res = $this->publish($page); |
||
101 | |||
102 | if ($res === true) { |
||
103 | $success_messages[] = "Page published successfully!"; |
||
104 | } else { |
||
105 | $error_messages[] = $res; |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | |||
110 | $template->assign("action_url", DomainUtils::generateURL($this->getPage()->getAlias(), array("edit" => $pageID))); |
||
111 | |||
112 | $template->assign("page", array( |
||
113 | 'id' => $page->getPageID(), |
||
114 | 'alias' => "/" . $page->getAlias(), |
||
115 | 'title' => $page->getTitle(), |
||
116 | 'content' => $page->getContent(), |
||
117 | 'is_published' => $page->isPublished(), |
||
118 | 'can_publish' => (!$page->isPublished() && (PermissionChecker::current()->hasRight("can_publish_all_pages") || (PermissionChecker::current()->hasRight("can_publish_own_pages") && $page->getAuthorID() == User::current()->getID()))), |
||
119 | 'can_change_owner' => (PermissionChecker::current()->hasRight("can_change_page_owner") || $page->getAuthorID() == User::current()->getID()), |
||
120 | 'folder' => $page->getFolder(), |
||
121 | 'preview_url' => DomainUtils::generateURL($page->getAlias(), array("preview" => "true")), |
||
122 | 'current_style' => $page->getStyle(), |
||
123 | 'template' => $page->getCustomTemplate(), |
||
124 | 'has_custom_template' => $page->hasCustomTemplate(), |
||
125 | 'parent' => $page->getParentID(), |
||
126 | 'meta_description' => $page->getMetaDescription(), |
||
127 | 'meta_keywords' => $page->getMetaKeywords(), |
||
128 | 'meta_robots' => $page->getMetaRobotsOptions(), |
||
129 | 'meta_canonicals' => $page->getMetaCanonicals(), |
||
130 | 'has_canoncials' => !empty($page->getMetaCanonicals()), |
||
131 | 'sitemap' => $page->isSitemapEnabled(), |
||
132 | 'sitemap_changefreq' => $page->getSitemapChangeFreq(), |
||
133 | 'sitemap_priority' => $page->getSitemapPriority(), |
||
134 | 'og_type' => $page->getOgType(), |
||
135 | 'og_title' => $page->getOgTitle(), |
||
136 | 'og_description' => $page->getOgDescription() |
||
137 | )); |
||
138 | |||
139 | //set available styles |
||
140 | $template->assign("styles", StyleController::listAllStyles()); |
||
141 | |||
142 | //get all pages from database |
||
143 | $pages = array(); |
||
144 | $rows = Database::getInstance()->listRows("SELECT `id`, `alias` FROM `{praefix}pages` WHERE `editable` = '1' AND `activated` = '1'; "); |
||
145 | |||
146 | foreach ($rows as $row) { |
||
147 | $pages[] = array( |
||
148 | 'id' => $row['id'], |
||
149 | 'alias' => $row['alias'] |
||
150 | ); |
||
151 | } |
||
152 | |||
153 | $template->assign("parent_pages", $pages); |
||
154 | |||
155 | //https://developers.google.com/search/reference/robots_meta_tag?hl=de |
||
156 | $robots_options = array( |
||
157 | "all", |
||
158 | "noindex", |
||
159 | "nofollow", |
||
160 | "none", |
||
161 | "noarchive", |
||
162 | "nosnippet", |
||
163 | "noodp", |
||
164 | "notranslate", |
||
165 | "noimageindex", |
||
166 | "unavailable_after: " |
||
167 | ); |
||
168 | |||
169 | $template->assign("robots_options", $robots_options); |
||
170 | |||
171 | $sitemap_change_frequencies = $this->sitemap_change_frequencies; |
||
172 | |||
173 | $template->assign("sitemap_change_frequencies", $sitemap_change_frequencies); |
||
174 | |||
175 | //OpenGraph types,https://developers.facebook.com/docs/reference/opengraph/ |
||
176 | $og_types = array("website", "article", "book", "profile"); |
||
177 | |||
178 | $template->assign("og_types", $og_types); |
||
179 | |||
180 | //add support to show additional code from plugins |
||
181 | $additional_code_header = ""; |
||
182 | $additional_code_footer = ""; |
||
183 | $additional_seo_code_header = ""; |
||
184 | $additional_seo_code_footer = ""; |
||
185 | |||
186 | Events::throwEvent("page_edit_additional_code_header", array( |
||
187 | 'page' => &$page, |
||
188 | 'code' => &$additional_code_header |
||
189 | )); |
||
190 | |||
191 | $template->assign("additional_code_header", $additional_code_header); |
||
192 | |||
193 | Events::throwEvent("page_edit_additional_code_footer", array( |
||
194 | 'page' => &$page, |
||
195 | 'code' => &$additional_code_footer |
||
196 | )); |
||
197 | |||
198 | $template->assign("additional_code_footer", $additional_code_footer); |
||
199 | |||
200 | Events::throwEvent("pageedit_additional_seo_code", array( |
||
201 | 'page' => &$page, |
||
202 | 'seo_header' => &$additional_seo_code_header, |
||
203 | 'seo_footer' => &$additional_seo_code_footer |
||
204 | )); |
||
205 | |||
206 | $template->assign("additional_seo_code_header", $additional_seo_code_header); |
||
207 | $template->assign("additional_seo_code_footer", $additional_seo_code_footer); |
||
208 | |||
209 | $template->assign("errors", $error_messages); |
||
210 | $template->assign("success_messages", $success_messages); |
||
211 | |||
212 | return $template->getCode(); |
||
213 | } |
||
443 |
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: