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 | * @param string $key |
||
35 | * @param array|null $params array with metadata of setting |
||
36 | */ |
||
37 | public function __construct($key, $params = null) { |
||
47 | |||
48 | /** |
||
49 | * Receives current values for the setting $key |
||
50 | * |
||
51 | * @param mixed $default default setting value |
||
52 | * @param mixed $local local setting value |
||
53 | * @param mixed $protected protected setting value |
||
54 | */ |
||
55 | public function initialize($default, $local, $protected) { |
||
60 | |||
61 | /** |
||
62 | * Should this type of config have a default? |
||
63 | * |
||
64 | * @return bool |
||
65 | */ |
||
66 | public function shouldHaveDefault() { |
||
69 | |||
70 | /** |
||
71 | * Get this setting's unique key |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | public function getKey() { |
||
78 | |||
79 | /** |
||
80 | * Get the key of this setting marked up human readable |
||
81 | * |
||
82 | * @param bool $url link to dokuwiki.org manual? |
||
83 | * @return string |
||
84 | */ |
||
85 | public function getPrettyKey($url = true) { |
||
97 | |||
98 | /** |
||
99 | * Returns setting key as an array key separator |
||
100 | * |
||
101 | * This is used to create form output |
||
102 | * |
||
103 | * @return string key |
||
104 | */ |
||
105 | public function getArrayKey() { |
||
108 | |||
109 | /** |
||
110 | * What type of configuration is this |
||
111 | * |
||
112 | * Returns one of |
||
113 | * |
||
114 | * 'plugin' for plugin configuration |
||
115 | * 'template' for template configuration |
||
116 | * 'conf' for core configuration |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | public function getType() { |
||
129 | |||
130 | /** |
||
131 | * update changed setting with user provided value $input |
||
132 | * - if changed value fails error check, save it to $this->_input (to allow echoing later) |
||
133 | * - if changed value passes error check, set $this->_local to the new value |
||
134 | * |
||
135 | * @param mixed $input the new value |
||
136 | * @return boolean true if changed, false otherwise (also on error) |
||
137 | */ |
||
138 | public function update($input) { |
||
154 | |||
155 | /** |
||
156 | * Build html for label and input of setting |
||
157 | * |
||
158 | * @param \admin_plugin_config $plugin object of config plugin |
||
159 | * @param bool $echo true: show inputted value, when error occurred, otherwise the stored setting |
||
160 | * @return string[] with content array(string $label_html, string $input_html) |
||
161 | */ |
||
162 | public function html(\admin_plugin_config $plugin, $echo = false) { |
||
184 | |||
185 | /** |
||
186 | * Generate string to save setting value to file according to $fmt |
||
187 | * |
||
188 | * @param string $var name of variable |
||
189 | * @param string $fmt save format |
||
190 | * @return string |
||
191 | */ |
||
192 | public function out($var, $fmt = 'php') { |
||
207 | |||
208 | /** |
||
209 | * Returns the localized prompt |
||
210 | * |
||
211 | * @param \admin_plugin_config $plugin object of config plugin |
||
212 | * @return string text |
||
213 | */ |
||
214 | public function prompt(\admin_plugin_config $plugin) { |
||
219 | |||
220 | /** |
||
221 | * Is setting protected |
||
222 | * |
||
223 | * @return bool |
||
224 | */ |
||
225 | public function isProtected() { |
||
228 | |||
229 | /** |
||
230 | * Is setting the default? |
||
231 | * |
||
232 | * @return bool |
||
233 | */ |
||
234 | public function isDefault() { |
||
237 | |||
238 | /** |
||
239 | * Has an error? |
||
240 | * |
||
241 | * @return bool |
||
242 | */ |
||
243 | public function hasError() { |
||
246 | |||
247 | /** |
||
248 | * Returns caution |
||
249 | * |
||
250 | * @return false|string caution string, otherwise false for invalid caution |
||
251 | */ |
||
252 | public function caution() { |
||
273 | |||
274 | } |
||
275 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.