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 | /** |
||
18 | * INTERNAL LIB - Check and get the template file path to use for a given display part |
||
19 | * |
||
20 | * @uses locate_template() |
||
21 | * @uses get_template_part() |
||
22 | * |
||
23 | * @param string $plugin_dir_name The main directory name containing the plugin |
||
24 | * @param string $main_template_dir THe main directory containing the templates used for display |
||
25 | * @param string $side The website part were the template will be displayed. Backend or frontend |
||
26 | * @param string $slug The slug name for the generic template. |
||
27 | * @param string $name The name of the specialised template. |
||
28 | * |
||
29 | * @return string The template file path to use |
||
30 | */ |
||
31 | public static function get_template_part( $plugin_dir_name, $main_template_dir, $side, $slug, $name=null, $debug = null ) { |
||
86 | |||
87 | /** |
||
88 | * Define the tools main page |
||
89 | */ |
||
90 | public static function main_page() { |
||
93 | |||
94 | /** |
||
95 | * Return a variable with some basic treatment |
||
96 | * |
||
97 | * @param mixed $varToSanitize The variable we want to treat for future use |
||
98 | * @param mixed $varDefaultValue The default value to set to the variable if the different test are not successfull |
||
99 | * @param string $varType optionnal The type of the var for better verification |
||
100 | * |
||
101 | * @return mixed $sanitizedVar The var after treatment |
||
102 | */ |
||
103 | public static function varSanitizer($varToSanitize, $varDefaultValue = '', $varType = '') { |
||
108 | |||
109 | /** |
||
110 | * Permit to force download a file |
||
111 | * @param string $Fichier_a_telecharger |
||
112 | * @param boolean $delete_after_download |
||
113 | */ |
||
114 | public static function forceDownload($Fichier_a_telecharger, $delete_after_download = false) { |
||
145 | |||
146 | /** |
||
147 | * Check if Send SMS is actived |
||
148 | * @return boolean |
||
149 | */ |
||
150 | public static function is_sendsms_actived() { |
||
162 | |||
163 | /** |
||
164 | * Search all variations possibilities |
||
165 | * @param unknown_type $input |
||
166 | * @return Ambigous <multitype:, multitype:multitype:unknown > |
||
167 | */ |
||
168 | public static function search_all_possibilities( $input ) { |
||
201 | |||
202 | /** |
||
203 | * Return Default currency |
||
204 | * @param boolean $code : false return sigle, true return code (€ or EUR) |
||
205 | * @return string currency code or sigle |
||
206 | */ |
||
207 | public static function wpshop_get_currency($code=false) { |
||
221 | |||
222 | /** |
||
223 | * Return unit sigle |
||
224 | * @param unknown_type $code |
||
225 | * @param unknown_type $column_to_return |
||
226 | */ |
||
227 | public static function wpshop_get_sigle($code, $column_to_return = "unit") { |
||
243 | |||
244 | /** |
||
245 | * Clean variable |
||
246 | * @param string $var : variable to clean |
||
247 | * @return string |
||
248 | */ |
||
249 | public static function wpshop_clean( $var ) { |
||
252 | |||
253 | /** |
||
254 | * Check if string have phone number structure |
||
255 | * @param string phone number |
||
256 | * @return boolean |
||
257 | */ |
||
258 | public static function is_phone( $phone ) { |
||
259 | return preg_match( '/(?=.*[0-9])([ 0-9\-\+\(\)]+)/', $phone ); |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Check if string have postcode valid structure |
||
264 | * @param string postcode |
||
265 | * @return boolean |
||
266 | */ |
||
267 | public static function is_postcode( $postcode ) { |
||
268 | return preg_match( '/(?=.*[0-9A-Za-z])([ \-A-Za-z0-9]+)/', $postcode ); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Return a form field type from a database field type |
||
273 | * |
||
274 | * @param string $dataFieldType The database field type we want to get the form field type for |
||
275 | * |
||
276 | * @return string $type The form input type to use for the given field |
||
277 | */ |
||
278 | public static function defineFieldType($dataFieldType, $input_type, $frontend_verification){ |
||
279 | $type = 'text'; |
||
280 | |||
281 | if ( $dataFieldType == 'datetime' ) { |
||
282 | $type = 'text'; |
||
283 | } |
||
284 | else { |
||
285 | switch ( $frontend_verification ) { |
||
286 | case 'phone': |
||
287 | $type = 'tel'; |
||
288 | break; |
||
289 | case 'email': |
||
290 | $type = 'email'; |
||
291 | break; |
||
292 | default: |
||
293 | $type = $input_type; |
||
294 | break; |
||
295 | } |
||
296 | } |
||
297 | return $type; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Get the method through which the data are transferred (POST OR GET) |
||
302 | * |
||
303 | * @return array The different element send by request method |
||
304 | */ |
||
305 | public static function getMethode(){ |
||
306 | $request_method = null; |
||
307 | if ($_SERVER["REQUEST_METHOD"] == "GET") { |
||
308 | $request_method = (array)$_GET; |
||
309 | } |
||
310 | if ($_SERVER["REQUEST_METHOD"] == "POST") { |
||
311 | $request_method = (array)$_POST; |
||
312 | } |
||
313 | |||
314 | if ( null === $request_method ) { |
||
315 | die ('Invalid REQUEST_METHOD (not GET, not POST).'); |
||
316 | } |
||
317 | else { |
||
318 | return $request_method; |
||
319 | } |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Transform a given text with a specific pattern, send by the second parameter |
||
324 | * |
||
325 | * @param string $toSlugify The string we want to "clean" for future use |
||
326 | * @param array|string $slugifyType The type of cleaning we are going to do on the input text |
||
327 | * |
||
328 | * @return string $slugified The input string that was slugified with the selected method |
||
329 | */ |
||
330 | public static function slugify($toSlugify, $slugifyType){ |
||
368 | |||
369 | /** |
||
370 | * Trunk a string too long |
||
371 | * |
||
372 | * @param string $string The string we want to "trunk" |
||
373 | * @param int $maxlength The max length of the result string |
||
374 | * |
||
375 | * @return string $string The output string that was trunk if necessary |
||
376 | */ |
||
377 | public static function trunk($string, $maxlength) { |
||
382 | |||
383 | /** |
||
384 | * Run a safe redirect in javascript |
||
385 | */ |
||
386 | public static function wpshop_safe_redirect($url='') { |
||
391 | |||
392 | /** |
||
393 | * Create a custom hook action |
||
394 | * @param string $hook_name |
||
395 | * @param array $args : Hook arguments |
||
396 | * @return string |
||
397 | */ |
||
398 | public static function create_custom_hook ($hook_name, $args = '') { |
||
410 | |||
411 | /** |
||
412 | * Return a plug-in activation code |
||
413 | * @param string $plugin_name |
||
414 | * @param string $encrypt_base_attribute |
||
415 | * @return string |
||
416 | */ |
||
417 | public static function get_plugin_validation_code($plugin_name, $encrypt_base_attribute) { |
||
432 | |||
433 | /** |
||
434 | * Check the WPShop Add-ons encrypt code validity |
||
435 | * @param string $plugin_name |
||
436 | * @param string $encrypt_base_attribute |
||
437 | * @return boolean |
||
438 | */ |
||
439 | public static function check_plugin_activation_code( $plugin_name, $encrypt_base_attribute, $from = 'file') { |
||
454 | |||
455 | /** |
||
456 | * Formate number, Add span on cents on hide cents if equals zero |
||
457 | * @param unknown_type $number |
||
458 | * @return string |
||
459 | */ |
||
460 | public static function formate_number( $number ) { |
||
469 | |||
470 | /** |
||
471 | * Return the translated element id of a page |
||
472 | * @param int $page_id |
||
473 | * @return int |
||
474 | */ |
||
475 | public static function get_page_id( $page_id ) { |
||
487 | |||
488 | public static function minutes_to_time( $minutes, $format = '%hh %imin' ) { |
||
489 | $dtF = new \DateTime( '@0' ); |
||
490 | $dtT = new \DateTime( '@' . ( $minutes * 60 ) ); |
||
491 | return $dtF->diff($dtT)->format( $format ); |
||
493 | |||
494 | public static function number_format_hack($n) { |
||
550 | } |
||
551 |
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.