Complex classes like wpshop_tools 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 wpshop_tools, and based on these observations, apply Extract Interface, too.
1 | <?php if ( !defined( 'ABSPATH' ) ) exit; |
||
15 | class wpshop_tools { |
||
16 | |||
17 | public static $currency_cache = null; |
||
18 | |||
19 | /** |
||
20 | * INTERNAL LIB - Check and get the template file path to use for a given display part |
||
21 | * |
||
22 | * @uses locate_template() |
||
23 | * @uses get_template_part() |
||
24 | * |
||
25 | * @param string $plugin_dir_name The main directory name containing the plugin |
||
26 | * @param string $main_template_dir THe main directory containing the templates used for display |
||
27 | * @param string $side The website part were the template will be displayed. Backend or frontend |
||
28 | * @param string $slug The slug name for the generic template. |
||
29 | * @param string $name The name of the specialised template. |
||
30 | * |
||
31 | * @return string The template file path to use |
||
32 | */ |
||
33 | public static function get_template_part( $plugin_dir_name, $main_template_dir, $side, $slug, $name=null, $debug = null ) { |
||
88 | |||
89 | /** |
||
90 | * Define the tools main page |
||
91 | */ |
||
92 | public static function main_page() { |
||
95 | |||
96 | /** |
||
97 | * Return a variable with some basic treatment |
||
98 | * |
||
99 | * @param mixed $varToSanitize The variable we want to treat for future use |
||
100 | * @param mixed $varDefaultValue The default value to set to the variable if the different test are not successfull |
||
101 | * @param string $varType optionnal The type of the var for better verification |
||
102 | * |
||
103 | * @return mixed $sanitizedVar The var after treatment |
||
104 | */ |
||
105 | public static function varSanitizer($varToSanitize, $varDefaultValue = '', $varType = '') { |
||
110 | |||
111 | /** |
||
112 | * Permit to force download a file |
||
113 | * @param string $Fichier_a_telecharger |
||
114 | * @param boolean $delete_after_download |
||
115 | */ |
||
116 | public static function forceDownload($Fichier_a_telecharger, $delete_after_download = false) { |
||
147 | |||
148 | /** |
||
149 | * Check if Send SMS is actived |
||
150 | * @return boolean |
||
151 | */ |
||
152 | public static function is_sendsms_actived() { |
||
164 | |||
165 | /** |
||
166 | * Search all variations possibilities |
||
167 | * @param unknown_type $input |
||
168 | * @return Ambigous <multitype:, multitype:multitype:unknown > |
||
169 | */ |
||
170 | public static function search_all_possibilities( $input ) { |
||
203 | |||
204 | /** |
||
205 | * Return Default currency |
||
206 | * @param boolean $code : false return sigle, true return code (€ or EUR) |
||
207 | * @return string currency code or sigle |
||
208 | */ |
||
209 | public static function wpshop_get_currency($code=false) { |
||
225 | |||
226 | /** |
||
227 | * Return unit sigle |
||
228 | * @param unknown_type $code |
||
229 | * @param unknown_type $column_to_return |
||
230 | */ |
||
231 | public static function wpshop_get_sigle($code, $column_to_return = "unit") { |
||
247 | |||
248 | /** |
||
249 | * Clean variable |
||
250 | * @param string $var : variable to clean |
||
251 | * @return string |
||
252 | */ |
||
253 | public static function wpshop_clean( $var ) { |
||
256 | |||
257 | /** |
||
258 | * Check if string have phone number structure |
||
259 | * @param string phone number |
||
260 | * @return boolean |
||
261 | */ |
||
262 | public static function is_phone( $phone ) { |
||
265 | |||
266 | /** |
||
267 | * Check if string have postcode valid structure |
||
268 | * @param string postcode |
||
269 | * @return boolean |
||
270 | */ |
||
271 | public static function is_postcode( $postcode ) { |
||
274 | |||
275 | /** |
||
276 | * Return a form field type from a database field type |
||
277 | * |
||
278 | * @param string $dataFieldType The database field type we want to get the form field type for |
||
279 | * |
||
280 | * @return string $type The form input type to use for the given field |
||
281 | */ |
||
282 | public static function defineFieldType($dataFieldType, $input_type, $frontend_verification){ |
||
303 | |||
304 | /** |
||
305 | * Get the method through which the data are transferred (POST OR GET) |
||
306 | * |
||
307 | * @return array The different element send by request method |
||
308 | */ |
||
309 | public static function getMethode(){ |
||
325 | |||
326 | /** |
||
327 | * Transform a given text with a specific pattern, send by the second parameter |
||
328 | * |
||
329 | * @param string $toSlugify The string we want to "clean" for future use |
||
330 | * @param array|string $slugifyType The type of cleaning we are going to do on the input text |
||
331 | * |
||
332 | * @return string $slugified The input string that was slugified with the selected method |
||
333 | */ |
||
334 | public static function slugify($toSlugify, $slugifyType){ |
||
372 | |||
373 | /** |
||
374 | * Trunk a string too long |
||
375 | * |
||
376 | * @param string $string The string we want to "trunk" |
||
377 | * @param int $maxlength The max length of the result string |
||
378 | * |
||
379 | * @return string $string The output string that was trunk if necessary |
||
380 | */ |
||
381 | public static function trunk($string, $maxlength) { |
||
386 | |||
387 | /** |
||
388 | * Run a safe redirect in javascript |
||
389 | */ |
||
390 | public static function wpshop_safe_redirect($url='') { |
||
395 | |||
396 | /** |
||
397 | * Create a custom hook action |
||
398 | * @param string $hook_name |
||
399 | * @param array $args : Hook arguments |
||
400 | * @return string |
||
401 | */ |
||
402 | public static function create_custom_hook ($hook_name, $args = '') { |
||
414 | |||
415 | /** |
||
416 | * Return a plug-in activation code |
||
417 | * @param string $plugin_name |
||
418 | * @param string $encrypt_base_attribute |
||
419 | * @return string |
||
420 | */ |
||
421 | public static function get_plugin_validation_code($plugin_name, $encrypt_base_attribute) { |
||
436 | |||
437 | /** |
||
438 | * Check the WPShop Add-ons encrypt code validity |
||
439 | * @param string $plugin_name |
||
440 | * @param string $encrypt_base_attribute |
||
441 | * @return boolean |
||
442 | */ |
||
443 | public static function check_plugin_activation_code( $plugin_name, $encrypt_base_attribute, $from = 'file') { |
||
458 | |||
459 | /** |
||
460 | * Formate number, Add span on cents on hide cents if equals zero |
||
461 | * @param unknown_type $number |
||
462 | * @return string |
||
463 | */ |
||
464 | public static function formate_number( $number ) { |
||
473 | |||
474 | /** |
||
475 | * Return the translated element id of a page |
||
476 | * @param int $page_id |
||
477 | * @return int |
||
478 | */ |
||
479 | public static function get_page_id( $page_id ) { |
||
491 | |||
492 | public static function minutes_to_time( $minutes, $format = '%hh %imin' ) { |
||
497 | |||
498 | public static function number_format_hack($n) { |
||
554 | } |
||
555 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.