Complex classes like FS_Option_Manager 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 FS_Option_Manager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class FS_Option_Manager { |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $_id; |
||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $_options; |
||
34 | /** |
||
35 | * @var FS_Logger |
||
36 | */ |
||
37 | private $_logger; |
||
38 | |||
39 | /** |
||
40 | * @var FS_Option_Manager[] |
||
41 | */ |
||
42 | private static $_MANAGERS = array(); |
||
43 | |||
44 | /** |
||
45 | * @author Vova Feldman (@svovaf) |
||
46 | * @since 1.0.3 |
||
47 | * |
||
48 | * @param string $id |
||
49 | * @param bool $load |
||
50 | */ |
||
51 | private function __construct( $id, $load = false ) { |
||
63 | |||
64 | /** |
||
65 | * @author Vova Feldman (@svovaf) |
||
66 | * @since 1.0.3 |
||
67 | * |
||
68 | * @param $id |
||
69 | * @param $load |
||
70 | * |
||
71 | * @return FS_Option_Manager |
||
72 | */ |
||
73 | static function get_manager( $id, $load = false ) { |
||
85 | |||
86 | private function _get_option_manager_name() { |
||
90 | |||
91 | /** |
||
92 | * @author Vova Feldman (@svovaf) |
||
93 | * @since 1.0.3 |
||
94 | * |
||
95 | * @param bool $flush |
||
96 | */ |
||
97 | function load( $flush = false ) { |
||
142 | |||
143 | /** |
||
144 | * @author Vova Feldman (@svovaf) |
||
145 | * @since 1.0.3 |
||
146 | * |
||
147 | * @return bool |
||
148 | */ |
||
149 | function is_loaded() { |
||
152 | |||
153 | /** |
||
154 | * @author Vova Feldman (@svovaf) |
||
155 | * @since 1.0.3 |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | function is_empty() { |
||
162 | |||
163 | /** |
||
164 | * @author Vova Feldman (@svovaf) |
||
165 | * @since 1.0.6 |
||
166 | * |
||
167 | * @param bool $flush |
||
168 | */ |
||
169 | function clear( $flush = false ) { |
||
178 | |||
179 | /** |
||
180 | * Delete options manager from DB. |
||
181 | * |
||
182 | * @author Vova Feldman (@svovaf) |
||
183 | * @since 1.0.9 |
||
184 | */ |
||
185 | function delete() { |
||
188 | |||
189 | /** |
||
190 | * @author Vova Feldman (@svovaf) |
||
191 | * @since 1.0.6 |
||
192 | * |
||
193 | * @param string $option |
||
194 | * |
||
195 | * @return bool |
||
196 | */ |
||
197 | function has_option( $option ) { |
||
200 | |||
201 | /** |
||
202 | * @author Vova Feldman (@svovaf) |
||
203 | * @since 1.0.3 |
||
204 | * |
||
205 | * @param string $option |
||
206 | * @param mixed $default |
||
207 | * |
||
208 | * @return mixed |
||
209 | */ |
||
210 | function get_option( $option, $default = null ) { |
||
221 | |||
222 | /** |
||
223 | * @author Vova Feldman (@svovaf) |
||
224 | * @since 1.0.3 |
||
225 | * |
||
226 | * @param string $option |
||
227 | * @param mixed $value |
||
228 | * @param bool $flush |
||
229 | */ |
||
230 | function set_option( $option, $value, $flush = false ) { |
||
247 | |||
248 | /** |
||
249 | * Unset option. |
||
250 | * |
||
251 | * @author Vova Feldman (@svovaf) |
||
252 | * @since 1.0.3 |
||
253 | * |
||
254 | * @param string $option |
||
255 | * @param bool $flush |
||
256 | */ |
||
257 | function unset_option( $option, $flush = false ) { |
||
279 | |||
280 | /** |
||
281 | * Dump options to database. |
||
282 | * |
||
283 | * @author Vova Feldman (@svovaf) |
||
284 | * @since 1.0.3 |
||
285 | */ |
||
286 | function store() { |
||
302 | } |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.