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 |
||
8 | class Setting { |
||
9 | |||
10 | protected $_key = ''; |
||
11 | protected $_default = null; |
||
12 | protected $_local = null; |
||
13 | protected $_protected = null; |
||
14 | |||
15 | protected $_pattern = ''; |
||
16 | protected $_error = false; // only used by those classes which error check |
||
17 | protected $_input = null; // only used by those classes which error check |
||
18 | protected $_caution = null; // used by any setting to provide an alert along with the setting |
||
19 | // valid alerts, 'warning', 'danger', 'security' |
||
20 | // images matching the alerts are in the plugin's images directory |
||
21 | |||
22 | static protected $_validCautions = array('warning', 'danger', 'security'); |
||
23 | |||
24 | /** |
||
25 | * @param string $key |
||
26 | * @param array|null $params array with metadata of setting |
||
27 | */ |
||
28 | public function __construct($key, $params = null) { |
||
37 | |||
38 | /** |
||
39 | * Receives current values for the setting $key |
||
40 | * |
||
41 | * @param mixed $default default setting value |
||
42 | * @param mixed $local local setting value |
||
43 | * @param mixed $protected protected setting value |
||
44 | */ |
||
45 | public function initialize($default, $local, $protected) { |
||
50 | |||
51 | /** |
||
52 | * Should this type of config have a default? |
||
53 | * |
||
54 | * @return bool |
||
55 | */ |
||
56 | public function shouldHaveDefault() { |
||
59 | |||
60 | /** |
||
61 | * Get this setting's unique key |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | public function getKey() { |
||
68 | |||
69 | /** |
||
70 | * update changed setting with user provided value $input |
||
71 | * - if changed value fails error check, save it to $this->_input (to allow echoing later) |
||
72 | * - if changed value passes error check, set $this->_local to the new value |
||
73 | * |
||
74 | * @param mixed $input the new value |
||
75 | * @return boolean true if changed, false otherwise (also on error) |
||
76 | */ |
||
77 | public function update($input) { |
||
93 | |||
94 | /** |
||
95 | * Build html for label and input of setting |
||
96 | * |
||
97 | * @param \admin_plugin_config $plugin object of config plugin |
||
98 | * @param bool $echo true: show inputted value, when error occurred, otherwise the stored setting |
||
99 | * @return string[] with content array(string $label_html, string $input_html) |
||
100 | */ |
||
101 | public function html(\admin_plugin_config $plugin, $echo = false) { |
||
123 | |||
124 | /** |
||
125 | * Generate string to save setting value to file according to $fmt |
||
126 | * |
||
127 | * @param string $var name of variable |
||
128 | * @param string $fmt save format |
||
129 | * @return string |
||
130 | */ |
||
131 | public function out($var, $fmt = 'php') { |
||
146 | |||
147 | /** |
||
148 | * Returns the localized prompt |
||
149 | * |
||
150 | * @param \admin_plugin_config $plugin object of config plugin |
||
151 | * @return string text |
||
152 | */ |
||
153 | public function prompt(\admin_plugin_config $plugin) { |
||
158 | |||
159 | /** |
||
160 | * Is setting protected |
||
161 | * |
||
162 | * @return bool |
||
163 | */ |
||
164 | public function is_protected() { |
||
167 | |||
168 | /** |
||
169 | * Is setting the default? |
||
170 | * |
||
171 | * @return bool |
||
172 | */ |
||
173 | public function is_default() { |
||
176 | |||
177 | /** |
||
178 | * Has an error? |
||
179 | * |
||
180 | * @return bool |
||
181 | */ |
||
182 | public function error() { |
||
185 | |||
186 | /** |
||
187 | * Returns caution |
||
188 | * |
||
189 | * @return false|string caution string, otherwise false for invalid caution |
||
190 | */ |
||
191 | public function caution() { |
||
212 | |||
213 | /** |
||
214 | * Returns setting key, eventually with referer to config: namespace at dokuwiki.org |
||
215 | * |
||
216 | * @param bool $pretty create nice key |
||
217 | * @param bool $url provide url to config: namespace |
||
218 | * @return string key |
||
219 | */ |
||
220 | public function _out_key($pretty = false, $url = false) { |
||
234 | } |
||
235 |
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.