| Conditions | 8 |
| Paths | 8 |
| Total Lines | 66 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 2 | Features | 1 |
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/settings"); |
||
| 32 | |||
| 33 | $template->assign("form_action", DomainUtils::generateURL("admin/settings", array("option" => "save"))); |
||
| 34 | $template->assign("content", ""); |
||
| 35 | |||
| 36 | $categories = array(); |
||
| 37 | |||
| 38 | $all_settings_by_category = Settings::listAllSettingsByCategory(); |
||
| 39 | |||
| 40 | $save = false; |
||
|
|
|||
| 41 | $save_success = true; |
||
| 42 | |||
| 43 | if (isset($_REQUEST['option']) && $_REQUEST['option'] == "save") { |
||
| 44 | $save = true; |
||
| 45 | } |
||
| 46 | |||
| 47 | foreach (SettingsCategory::listAllCategories() as $category) { |
||
| 48 | $category = SettingsCategory::cast($category); |
||
| 49 | |||
| 50 | $settings = array(); |
||
| 51 | |||
| 52 | if (isset($all_settings_by_category[$category->getCategory()])) { |
||
| 53 | //list settings |
||
| 54 | $rows = $all_settings_by_category[$category->getCategory()]; |
||
| 55 | |||
| 56 | foreach ($rows as $key=>$row) { |
||
| 57 | $datatype = $row['datatype']; |
||
| 58 | $datatype_params = unserialize($row['datatype_params']); |
||
| 59 | |||
| 60 | $obj = new $datatype(); |
||
| 61 | |||
| 62 | if (!($obj instanceof DataType_Base)) { |
||
| 63 | throw new IllegalArgumentException("obj of class name '" . $datatype . "' has to be an instance of DataType_Base."); |
||
| 64 | } |
||
| 65 | |||
| 66 | //load instance |
||
| 67 | $obj->load($row, $datatype_params); |
||
| 68 | |||
| 69 | //try to validate |
||
| 70 | if (!$obj->val()) { |
||
| 71 | $save_success = false; |
||
| 72 | } else { |
||
| 73 | //save object |
||
| 74 | $obj->save(); |
||
| 75 | } |
||
| 76 | |||
| 77 | $settings[] = array( |
||
| 78 | 'title' => Translator::translateTitle($row['title']), |
||
| 79 | 'description' => Translator::translateTitle($row['description']), |
||
| 80 | 'code' => $obj->getFormCode() |
||
| 81 | ); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | $categories[] = array( |
||
| 86 | 'title' => $category->getTitle(), |
||
| 87 | 'settings' => $settings |
||
| 88 | ); |
||
| 89 | } |
||
| 90 | |||
| 91 | Settings::saveAsync(); |
||
| 92 | |||
| 93 | $template->assign("categories", $categories); |
||
| 94 | |||
| 95 | return $template->getCode(); |
||
| 96 | } |
||
| 105 |