| Conditions | 20 |
| Paths | 1348 |
| Total Lines | 111 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 10 | ||
| Bugs | 0 | Features | 6 |
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 |
||
| 30 | public function getContent(): string { |
||
| 31 | $template = new DwooTemplate("pages/createpage"); |
||
| 32 | |||
| 33 | $errors = array(); |
||
| 34 | |||
| 35 | if (isset($_REQUEST['action']) && $_REQUEST['action'] === "create" && PermissionChecker::current()->hasRight("can_create_pages")) { |
||
| 36 | //try to create a new page |
||
| 37 | |||
| 38 | //first, get all values |
||
| 39 | if (!isset($_REQUEST['folder']) || empty($_REQUEST['folder'])) { |
||
| 40 | $errors[] = "Folder is not set!"; |
||
| 41 | } |
||
| 42 | |||
| 43 | if (!isset($_REQUEST['page_alias']) || empty($_REQUEST['page_alias'])) { |
||
| 44 | $errors[] = "Page alias isn't set!"; |
||
| 45 | } |
||
| 46 | |||
| 47 | if (!isset($_REQUEST['title']) || empty($_REQUEST['title'])) { |
||
| 48 | $errors[] = "Title is't set!"; |
||
| 49 | } |
||
| 50 | |||
| 51 | if (!isset($_REQUEST['pagetype']) || empty($_REQUEST['pagetype'])) { |
||
| 52 | $errors[] = "Pagetype is't set!"; |
||
| 53 | } |
||
| 54 | |||
| 55 | if (empty($errors)) { |
||
| 56 | $folder = $_REQUEST['folder']; |
||
| 57 | $alias = $_REQUEST['page_alias']; |
||
| 58 | $title = $_REQUEST['title']; |
||
| 59 | $pagetype = $_REQUEST['pagetype']; |
||
| 60 | |||
| 61 | //check, if folder exists |
||
| 62 | if (!Folder::exists($folder)) { |
||
| 63 | $errors[] = "Folder '" . htmlentities($folder) . "' doesn't exists!"; |
||
| 64 | } |
||
| 65 | |||
| 66 | //check, if page alias already exists |
||
| 67 | $page_full_alias = $folder . $alias; |
||
| 68 | |||
| 69 | if (PHPUtils::startsWith($page_full_alias, "/")) { |
||
| 70 | //remove / at beginning |
||
| 71 | $page_full_alias = substr($page_full_alias, 1); |
||
| 72 | } |
||
| 73 | |||
| 74 | if (Page::exists($page_full_alias)) { |
||
| 75 | $errors[] = "Page alias '" . htmlentities($page_full_alias) . "' already exists!"; |
||
| 76 | } |
||
| 77 | |||
| 78 | //remove html characters in title |
||
| 79 | $title = htmlentities($title); |
||
| 80 | |||
| 81 | //check, if pagetype exists |
||
| 82 | if (!PageType::exists($pagetype)) { |
||
| 83 | $errors[] = "Pagetype '" . htmlentities($pagetype) . "' doesn't exists!"; |
||
| 84 | } |
||
| 85 | |||
| 86 | Events::throwEvent("before_create_page", array( |
||
| 87 | 'folder' => &$folder, |
||
| 88 | 'alias' => &$alias, |
||
| 89 | 'full_alias' => &$page_full_alias, |
||
| 90 | 'title' => &$title, |
||
| 91 | 'pagetype' => &$pagetype |
||
| 92 | )); |
||
| 93 | |||
| 94 | if (empty($errors)) { |
||
| 95 | $pageID = Page::createIfAbsent($page_full_alias, htmlentities($title), $pagetype, "", $folder, -1, -1, -1, true, false, true, true, User::current()->getUsername()); |
||
| 96 | |||
| 97 | Events::throwEvent("after_create_page", array( |
||
| 98 | 'pageID' => $pageID |
||
| 99 | )); |
||
| 100 | |||
| 101 | //redirect header |
||
| 102 | header("Location: " . DomainUtils::generateURL("admin/edit_page", array("edit" => $pageID))); |
||
| 103 | |||
| 104 | exit; |
||
|
|
|||
| 105 | } |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | //set form action url |
||
| 110 | $template->assign("action_url", DomainUtils::generateURL($this->getPage()->getAlias(), array("action" => "create"))); |
||
| 111 | $template->assign("username", User::current()->getUsername()); |
||
| 112 | |||
| 113 | $template->assign("errors", $errors); |
||
| 114 | |||
| 115 | //list page types |
||
| 116 | $page_types_rows = PageType::listPageTypes(); |
||
| 117 | $page_types = array(); |
||
| 118 | |||
| 119 | foreach ($page_types_rows as $row) { |
||
| 120 | $page_types[] = array( |
||
| 121 | 'title' => htmlentities(Translator::translateTitle($row['title'])), |
||
| 122 | 'class_name' => $row['page_type'] |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | |||
| 126 | $template->assign("pagetypes", $page_types); |
||
| 127 | |||
| 128 | $folders = array(); |
||
| 129 | |||
| 130 | foreach (Folder::listFolders(false) as $row) { |
||
| 131 | $folders[] = array( |
||
| 132 | 'folder' => $row['folder'], |
||
| 133 | 'hidden' => $row['hidden'] == 1, |
||
| 134 | 'is_root_folder' => $row['folder'] === "/" |
||
| 135 | ); |
||
| 136 | } |
||
| 137 | |||
| 138 | $template->assign("folders", $folders); |
||
| 139 | |||
| 140 | return $template->getCode(); |
||
| 141 | } |
||
| 154 |
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: