Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
20 | class Config { |
||
21 | |||
22 | const FEATURE_ENSURED = 1; |
||
23 | const FEATURE_NOT_AVAILABLE = 0; |
||
24 | const FEATURE_ALREADY_ENSURED = -1; |
||
25 | |||
26 | /** |
||
27 | * The initial setting values. |
||
28 | * |
||
29 | * @var Array |
||
30 | */ |
||
31 | protected $config = array( |
||
32 | 'jitm' => false, |
||
33 | 'connection' => false, |
||
34 | 'sync' => false, |
||
35 | 'tracking' => false, |
||
36 | 'tos' => false, |
||
37 | ); |
||
38 | |||
39 | /** |
||
40 | * Initialization options stored here. |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $feature_options = array(); |
||
45 | |||
46 | /** |
||
47 | * Creates the configuration class instance. |
||
48 | */ |
||
49 | public function __construct() { |
||
57 | |||
58 | /** |
||
59 | * Require a feature to be initialized. It's up to the package consumer to actually add |
||
60 | * the package to their composer project. Declaring a requirement using this method |
||
61 | * instructs the class to initialize it. |
||
62 | * |
||
63 | * @param String $feature the feature slug. |
||
64 | * @param array $options Additional options, optional. |
||
65 | */ |
||
66 | public function ensure( $feature, array $options = array() ) { |
||
71 | |||
72 | /** |
||
73 | * Runs on plugins_loaded hook priority with priority 2. |
||
74 | * |
||
75 | * @action plugins_loaded |
||
76 | */ |
||
77 | public function on_plugins_loaded() { |
||
101 | |||
102 | /** |
||
103 | * Returns true if the required class is available and alerts the user if it's not available |
||
104 | * in case the site is in debug mode. |
||
105 | * |
||
106 | * @param String $classname a fully qualified class name. |
||
107 | * @param Boolean $log_notice whether the E_USER_NOTICE should be generated if the class is not found. |
||
108 | * |
||
109 | * @return Boolean whether the class is available. |
||
110 | */ |
||
111 | protected function ensure_class( $classname, $log_notice = true ) { |
||
130 | |||
131 | /** |
||
132 | * Ensures a feature is enabled, sets it up if it hasn't already been set up. |
||
133 | * Run the options method (if exists) every time the method is called. |
||
134 | * |
||
135 | * @param String $feature slug of the feature. |
||
136 | * @return Integer either FEATURE_ENSURED, FEATURE_ALREADY_ENSURED or FEATURE_NOT_AVAILABLE constants. |
||
137 | */ |
||
138 | protected function ensure_feature( $feature ) { |
||
164 | |||
165 | /** |
||
166 | * Dummy method to enable Terms of Service. |
||
167 | */ |
||
168 | protected function enable_tos() { |
||
171 | |||
172 | /** |
||
173 | * Enables the tracking feature. Depends on the Terms of Service package, so enables it too. |
||
174 | */ |
||
175 | protected function enable_tracking() { |
||
193 | |||
194 | /** |
||
195 | * Enables the JITM feature. |
||
196 | */ |
||
197 | protected function enable_jitm() { |
||
207 | |||
208 | /** |
||
209 | * Enables the Sync feature. |
||
210 | */ |
||
211 | protected function enable_sync() { |
||
216 | |||
217 | /** |
||
218 | * Enables the Connection feature. |
||
219 | */ |
||
220 | protected function enable_connection() { |
||
225 | |||
226 | /** |
||
227 | * Setup the Connection options. |
||
228 | */ |
||
229 | protected function ensure_options_connection() { |
||
248 | |||
249 | /** |
||
250 | * Temporary save initialization options for a feature. |
||
251 | * |
||
252 | * @param string $feature The feature slug. |
||
253 | * @param array $options The options. |
||
254 | * |
||
255 | * @return bool |
||
256 | */ |
||
257 | protected function set_feature_options( $feature, array $options ) { |
||
264 | |||
265 | /** |
||
266 | * Get initialization options for a feature from the temporary storage. |
||
267 | * |
||
268 | * @param string $feature The feature slug. |
||
269 | * |
||
270 | * @return array |
||
271 | */ |
||
272 | protected function get_feature_options( $feature ) { |
||
275 | |||
276 | } |
||
277 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.