| Total Complexity | 43 |
| Total Lines | 214 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Complex classes like Folder 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 Folder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Folder { |
||
| 29 | |||
| 30 | protected $folder = ""; |
||
| 31 | protected $row = array(); |
||
| 32 | |||
| 33 | protected static $load_count = 0; |
||
| 34 | |||
| 35 | public function __construct($folder) { |
||
| 36 | $this->folder = $folder; |
||
| 37 | } |
||
| 38 | |||
| 39 | public function load ($folder) { |
||
| 40 | self::$load_count++; |
||
| 41 | |||
| 42 | if (self::$load_count > 50) { |
||
| 43 | throw new IllegalStateException("reached max allowed number of Folder::load() calls (to prevent recursion)."); |
||
| 44 | } |
||
| 45 | |||
| 46 | //escape string |
||
| 47 | //$folder = Database::getInstance()->escape($folder); |
||
| 48 | |||
| 49 | if (empty($folder)) { |
||
| 50 | $folder = "/"; |
||
| 51 | } |
||
| 52 | |||
| 53 | if (Cache::contains("folder", "folder-" . $folder)) { |
||
| 54 | $this->row = Cache::get("folder", "folder-" . $folder); |
||
| 55 | } else { |
||
| 56 | $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}folder` WHERE `folder` = :folder; ", array('folder' => $folder)); |
||
| 57 | |||
| 58 | if (!$row) { |
||
| 59 | //load upper folder |
||
| 60 | if (strcmp($folder, "/") !== 0) { |
||
| 61 | //load upper dir |
||
| 62 | $folder = self::getUpperDir($folder); |
||
| 63 | |||
| 64 | $this->load($folder); |
||
| 65 | } else { |
||
| 66 | throw new IllegalStateException("no main folder / is defined. Please insert folder '/'."); |
||
| 67 | } |
||
| 68 | } else { |
||
| 69 | $this->row = $row; |
||
| 70 | } |
||
| 71 | |||
| 72 | Cache::put("folder", "folder-" . $folder, $this->row); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | public function getFolder () : string { |
||
| 77 | return $this->row['folder']; |
||
| 78 | } |
||
| 79 | |||
| 80 | public function listRequiredPermissions () : array { |
||
| 81 | return explode("|", $this->row['permissions']); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function checkPermissions (PermissionChecker $permission_checker) : bool { |
||
| 85 | foreach ($this->listRequiredPermissions() as $permission) { |
||
| 86 | if ($permission_checker->hasRight($permission)) { |
||
| 87 | return true; |
||
| 88 | } else if ($permission === "none") { |
||
| 89 | return true; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | return false; |
||
| 94 | } |
||
| 95 | |||
| 96 | public function isHidden () : bool { |
||
| 98 | } |
||
| 99 | |||
| 100 | public function hasCustomMainMenu () : bool { |
||
| 101 | return $this->row['main_menu'] != -1; |
||
| 102 | } |
||
| 103 | |||
| 104 | public function getMainMenu () : int { |
||
| 105 | return $this->row['main_menu']; |
||
| 106 | } |
||
| 107 | |||
| 108 | public function hasCustomLocalMenu () : bool { |
||
| 109 | return $this->row['local_menu'] != -1; |
||
| 110 | } |
||
| 111 | |||
| 112 | public function getLocalMenu () : int { |
||
| 113 | return $this->row['local_menu']; |
||
| 114 | } |
||
| 115 | |||
| 116 | public function isTitleTranslationEnabled () { |
||
| 117 | return $this->row['title_translation_support'] == 1; |
||
| 118 | } |
||
| 119 | |||
| 120 | public function isActivated () : bool { |
||
| 122 | } |
||
| 123 | |||
| 124 | public static function getFolderByPage (string $page) : string { |
||
| 125 | $array = explode("/", $page); |
||
| 126 | |||
| 127 | if (sizeof($array) <= 2 && empty($array[0])) { |
||
| 128 | return "/"; |
||
| 129 | } |
||
| 130 | |||
| 131 | //remove last element |
||
| 132 | array_pop($array); |
||
| 133 | |||
| 134 | return implode("/", $array) . "/"; |
||
| 135 | } |
||
| 136 | |||
| 137 | public static function getUpperDir ($dir) { |
||
| 138 | if (PHPUtils::endsWith($dir, "/")) { |
||
| 139 | //remove last slash |
||
| 140 | $dir = substr($dir, 0, -1); |
||
| 141 | } |
||
| 142 | |||
| 143 | $array = explode("/", $dir); |
||
| 144 | |||
| 145 | if (sizeof($array) <= 2 && empty($array[0])) { |
||
| 146 | return "/"; |
||
| 147 | } |
||
| 148 | |||
| 149 | //remove last element |
||
| 150 | array_pop($array); |
||
| 151 | |||
| 152 | return implode("/", $array) . "/"; |
||
| 153 | } |
||
| 154 | |||
| 155 | public static function createFolder (string $folder, bool $hidden = false, array $permissions = array(), int $main_menu = -1, int $local_menu = -1, string $force_template = "none", bool $title_translation = true) { |
||
| 156 | //escape string |
||
| 157 | //$folder = Database::getInstance()->escape($folder); |
||
| 158 | |||
| 159 | $permissions_str = implode("|", $permissions); |
||
| 160 | |||
| 161 | if (sizeof($permissions) == 0) { |
||
| 162 | $permissions_str = "none"; |
||
| 163 | } |
||
| 164 | |||
| 165 | Database::getInstance()->execute("INSERT INTO `{praefix}folder` ( |
||
| 166 | `folder`, `force_template`, `main_menu`, `local_menu`, `permissions`, `title_translation_support`, `hidden`, `activated` |
||
| 167 | ) VALUES ( |
||
| 168 | :folder, :templatename, :main_menu, :local_menu, :permissions, :title_translation_support, :hidden, '1' |
||
| 169 | ); ", array( |
||
| 170 | 'folder' => $folder, |
||
| 171 | 'templatename' => $force_template, |
||
| 172 | 'main_menu' => $main_menu, |
||
| 173 | 'local_menu' => $local_menu, |
||
| 174 | 'permissions' => $permissions_str, |
||
| 175 | 'title_translation_support' => ($title_translation ? 1 : 0), |
||
| 176 | 'hidden' => $hidden ? 1 : 0 |
||
| 177 | )); |
||
| 178 | |||
| 179 | //clear cache |
||
| 180 | Cache::clear("folder"); |
||
| 181 | } |
||
| 182 | |||
| 183 | public static function createFolderIfAbsent (string $folder, bool $hidden = false, array $permissions = array(), int $main_menu = -1, int $local_menu = -1, string $force_template = "none", bool $title_translation = true) { |
||
| 184 | //escape string |
||
| 185 | //$folder = Database::getInstance()->escape($folder); |
||
| 186 | |||
| 187 | $permissions_str = implode("|", $permissions); |
||
| 188 | |||
| 189 | if (sizeof($permissions) == 0) { |
||
| 190 | $permissions_str = "none"; |
||
| 191 | } |
||
| 192 | |||
| 193 | Database::getInstance()->execute("INSERT INTO `{praefix}folder` ( |
||
| 194 | `folder`, `force_template`, `main_menu`, `local_menu`, `permissions`, `title_translation_support`, `hidden`, `activated` |
||
| 195 | ) VALUES ( |
||
| 196 | :folder, :templatename, :main_menu, :local_menu, :permissions, :title_translation_support, :hidden, '1' |
||
| 197 | ) ON DUPLICATE KEY UPDATE `hidden` = :hidden, `permissions` = :permissions, `force_template` = :templatename; ", array( |
||
| 198 | 'folder' => $folder, |
||
| 199 | 'templatename' => $force_template, |
||
| 200 | 'main_menu' => $main_menu, |
||
| 201 | 'local_menu' => $local_menu, |
||
| 202 | 'permissions' => $permissions_str, |
||
| 203 | 'title_translation_support' => ($title_translation ? 1 : 0), |
||
| 204 | 'hidden' => $hidden ? 1 : 0 |
||
| 205 | )); |
||
| 206 | |||
| 207 | //clear cache |
||
| 208 | Cache::clear("folder"); |
||
| 209 | } |
||
| 210 | |||
| 211 | public static function deleteFolder ($folder) { |
||
| 216 | } |
||
| 217 | |||
| 218 | public static function listFolders (bool $include_hidden_folders = false) { |
||
| 219 | if (Cache::contains("folder", "list_" . ($include_hidden_folders ? "hidden" : "not_hidden"))) { |
||
| 220 | return Cache::get("folder", "list_" . ($include_hidden_folders ? "hidden" : "not_hidden")); |
||
| 221 | } else { |
||
| 222 | $rows = array(); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | public static function exists (string $folder) : bool { |
||
| 242 | } |
||
| 243 | |||
| 247 |
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.
A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.