Complex classes like Setting 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Setting, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Setting { |
||
| 11 | /** @var string unique identifier of this setting */ |
||
| 12 | protected $key = ''; |
||
| 13 | |||
| 14 | /** @var mixed the default value of this setting */ |
||
| 15 | protected $default = null; |
||
| 16 | /** @var mixed the local value of this setting */ |
||
| 17 | protected $local = null; |
||
| 18 | /** @var mixed the protected value of this setting */ |
||
| 19 | protected $protected = null; |
||
| 20 | |||
| 21 | /** @var array valid alerts, images matching the alerts are in the plugin's images directory */ |
||
| 22 | static protected $validCautions = array('warning', 'danger', 'security'); |
||
| 23 | |||
| 24 | protected $pattern = ''; |
||
| 25 | protected $error = false; // only used by those classes which error check |
||
| 26 | protected $input = null; // only used by those classes which error check |
||
| 27 | protected $caution = null; // used by any setting to provide an alert along with the setting |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Constructor. |
||
| 31 | * |
||
| 32 | * The given parameters will be set up as class properties |
||
| 33 | * |
||
| 34 | * @see initialize() to set the actual value of the setting |
||
| 35 | * |
||
| 36 | * @param string $key |
||
| 37 | * @param array|null $params array with metadata of setting |
||
| 38 | */ |
||
| 39 | public function __construct($key, $params = null) { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Set the current values for the setting $key |
||
| 52 | * |
||
| 53 | * This is used to initialize the setting with the data read form the config files. |
||
| 54 | * |
||
| 55 | * @see update() to set a new value |
||
| 56 | * @param mixed $default default setting value |
||
| 57 | * @param mixed $local local setting value |
||
| 58 | * @param mixed $protected protected setting value |
||
| 59 | */ |
||
| 60 | public function initialize($default = null, $local = null, $protected = null) { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * update changed setting with validated user provided value $input |
||
| 68 | * - if changed value fails validation check, save it to $this->input (to allow echoing later) |
||
| 69 | * - if changed value passes validation check, set $this->local to the new value |
||
| 70 | * |
||
| 71 | * @param mixed $input the new value |
||
| 72 | * @return boolean true if changed, false otherwise |
||
| 73 | */ |
||
| 74 | public function update($input) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Clean a value read from a config before using it internally |
||
| 98 | * |
||
| 99 | * Default implementation returns $value as is. Subclasses can override. |
||
| 100 | * Note: null should always be returned as null! |
||
| 101 | * |
||
| 102 | * This is applied in initialize() and update() |
||
| 103 | * |
||
| 104 | * @param mixed $value |
||
| 105 | * @return mixed |
||
| 106 | */ |
||
| 107 | protected function cleanValue($value) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Should this type of config have a default? |
||
| 113 | * |
||
| 114 | * @return bool |
||
| 115 | */ |
||
| 116 | public function shouldHaveDefault() { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get this setting's unique key |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function getKey() { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get the key of this setting marked up human readable |
||
| 131 | * |
||
| 132 | * @param bool $url link to dokuwiki.org manual? |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public function getPrettyKey($url = true) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Returns setting key as an array key separator |
||
| 150 | * |
||
| 151 | * This is used to create form output |
||
| 152 | * |
||
| 153 | * @return string key |
||
| 154 | */ |
||
| 155 | public function getArrayKey() { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * What type of configuration is this |
||
| 161 | * |
||
| 162 | * Returns one of |
||
| 163 | * |
||
| 164 | * 'plugin' for plugin configuration |
||
| 165 | * 'template' for template configuration |
||
| 166 | * 'dokuwiki' for core configuration |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | public function getType() { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Build html for label and input of setting |
||
| 182 | * |
||
| 183 | * @param \admin_plugin_config $plugin object of config plugin |
||
| 184 | * @param bool $echo true: show inputted value, when error occurred, otherwise the stored setting |
||
| 185 | * @return string[] with content array(string $label_html, string $input_html) |
||
| 186 | */ |
||
| 187 | public function html(\admin_plugin_config $plugin, $echo = false) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Should the current local value be saved? |
||
| 212 | * |
||
| 213 | * @see out() to run when this returns true |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | public function shouldBeSaved() { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Generate string to save local setting value to file according to $fmt |
||
| 225 | * |
||
| 226 | * @see shouldBeSaved() to check if this should be called |
||
| 227 | * @param string $var name of variable |
||
| 228 | * @param string $fmt save format |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | public function out($var, $fmt = 'php') { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Returns the localized prompt |
||
| 242 | * |
||
| 243 | * @param \admin_plugin_config $plugin object of config plugin |
||
| 244 | * @return string text |
||
| 245 | */ |
||
| 246 | public function prompt(\admin_plugin_config $plugin) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Is setting protected |
||
| 254 | * |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | public function isProtected() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Is setting the default? |
||
| 263 | * |
||
| 264 | * @return bool |
||
| 265 | */ |
||
| 266 | public function isDefault() { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Has an error? |
||
| 272 | * |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | public function hasError() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Returns caution |
||
| 281 | * |
||
| 282 | * @return false|string caution string, otherwise false for invalid caution |
||
| 283 | */ |
||
| 284 | public function caution() { |
||
| 293 | |||
| 294 | } |
||
| 295 |