| Total Complexity | 71 |
| Total Lines | 282 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Options 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 Options, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Options |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @access private |
||
| 22 | * @since 1.0 |
||
| 23 | */ |
||
| 24 | private function __construct() |
||
| 25 | { |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Get an option. |
||
| 30 | * |
||
| 31 | * @return mixed |
||
| 32 | * @since 1.0 |
||
| 33 | * @static |
||
| 34 | */ |
||
| 35 | public static function getOption($name) |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Add a new option. |
||
| 52 | * |
||
| 53 | * @return bool |
||
| 54 | * @since 1.0 |
||
| 55 | * @static |
||
| 56 | */ |
||
| 57 | public static function addOption($name, $value, bool $sanitize = false, bool $validate = true) |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Update an existing option. |
||
| 91 | * |
||
| 92 | * @return bool |
||
| 93 | * @since 1.0 |
||
| 94 | * @static |
||
| 95 | */ |
||
| 96 | public static function updateOption($name, $value, bool $sanitize = false, bool $validate = true) |
||
| 97 | { |
||
| 98 | if ($sanitize) { |
||
| 99 | $value = self::sanitizeOption($name, $value); |
||
| 100 | } |
||
| 101 | |||
| 102 | if ($validate && ! self::validateOption($name, $value)) { |
||
| 103 | return false; |
||
| 104 | } |
||
| 105 | |||
| 106 | $em = Main::getInstance()->getEntityManager(); |
||
| 107 | |||
| 108 | if (getInstallationStatus($em->getConnection()) !== DATABASE_INSTALLED) { |
||
| 109 | return false; |
||
| 110 | } |
||
| 111 | |||
| 112 | $var = $em->find('Entities:Variable', $name); |
||
| 113 | |||
| 114 | if (empty($var)) { |
||
| 115 | return false; |
||
| 116 | } |
||
| 117 | |||
| 118 | $var->set('value', $value, $sanitize, $validate); |
||
| 119 | |||
| 120 | $em->flush($var); |
||
| 121 | |||
| 122 | return true; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Submit an option. |
||
| 127 | * |
||
| 128 | * @return bool |
||
| 129 | * @since 1.0 |
||
| 130 | * @static |
||
| 131 | */ |
||
| 132 | public static function submitOption($name, $value, bool $sanitize = false, bool $validate = true) |
||
| 133 | { |
||
| 134 | if ('' === strval($value)) { |
||
| 135 | return self::deleteOption($name); |
||
| 136 | } else { |
||
| 137 | $currentOption = self::getOption($name); |
||
| 138 | if (is_null($currentOption)) { |
||
| 139 | return self::addOption($name, $value, $sanitize, $validate); |
||
| 140 | } else { |
||
| 141 | return self::updateOption($name, $value, $sanitize, $validate); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Delete an option. |
||
| 148 | * |
||
| 149 | * @return bool |
||
| 150 | * @since 1.0 |
||
| 151 | * @static |
||
| 152 | */ |
||
| 153 | public static function deleteOption($name) |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Sanitize an option value. |
||
| 175 | * |
||
| 176 | * @return mixed |
||
| 177 | * @since 1.0 |
||
| 178 | * @static |
||
| 179 | */ |
||
| 180 | public static function sanitizeOption($name, $value) |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Validate an option value. |
||
| 210 | * |
||
| 211 | * @return bool |
||
| 212 | * @since 1.0 |
||
| 213 | * @static |
||
| 214 | */ |
||
| 215 | public static function validateOption($name, $value) |
||
| 300 | } |
||
| 301 | } |
||
| 302 |