CMB2_Utils   D
last analyzed

Complexity

Total Complexity 91

Size/Duplication

Total Lines 590
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 87.56%

Importance

Changes 0
Metric Value
dl 0
loc 590
ccs 197
cts 225
cp 0.8756
rs 4.8717
c 0
b 0
f 0
wmc 91
lcom 1
cbo 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
C image_id_from_url() 0 41 8
B get_available_image_sizes() 0 18 5
C get_named_size() 0 68 17
B timezone_offset() 0 19 5
B timezone_string() 0 21 5
A make_valid_time_stamp() 0 9 3
A is_valid_time_stamp() 0 5 3
A isempty() 0 3 4
A notempty() 0 3 4
A filter_empty() 0 3 1
A array_insert() 0 5 1
A url() 0 16 2
B get_url_from_dir() 0 40 5
A normalize_path() 0 14 3
A get_timestamp_from_value() 0 4 2
B php_to_js_dateformat() 0 34 2
A wrap_escaped_chars() 0 3 1
A log_if_debug() 0 5 4
A get_file_ext() 0 4 2
A get_file_name_from_path() 0 4 2
A wp_at_least() 0 3 1
B concat_attrs() 0 14 6
B ensure_array() 0 16 5

How to fix   Complexity   

Complex Class

Complex classes like CMB2_Utils 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 CMB2_Utils, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * CMB2 Utilities
4
 *
5
 * @since  1.1.0
6
 *
7
 * @category  WordPress_Plugin
8
 * @package   CMB2
9
 * @author    WebDevStudios
10
 * @license   GPL-2.0+
11
 * @link      http://webdevstudios.com
12
 */
13
class CMB2_Utils {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
14
15
	/**
16
	 * The WordPress ABSPATH constant.
17
	 *
18
	 * @var   string
19
	 * @since 2.2.3
20
	 */
21
	protected static $ABSPATH = ABSPATH;
22
23
	/**
24
	 * The url which is used to load local resources.
25
	 *
26
	 * @var   string
27
	 * @since 2.0.0
28
	 */
29
	protected static $url = '';
30
31
	/**
32
	 * Utility method that attempts to get an attachment's ID by it's url
33
	 *
34
	 * @since  1.0.0
35
	 * @param  string $img_url Attachment url
36
	 * @return int|false            Attachment ID or false
37
	 */
38 1
	public static function image_id_from_url( $img_url ) {
39 1
		$attachment_id = 0;
40 1
		$dir = wp_upload_dir();
41
42
		// Is URL in uploads directory?
43 1
		if ( false === strpos( $img_url, $dir['baseurl'] . '/' ) ) {
44
			return false;
45
		}
46
47 1
		$file = basename( $img_url );
48
49
		$query_args = array(
50 1
			'post_type'   => 'attachment',
51 1
			'post_status' => 'inherit',
52 1
			'fields'      => 'ids',
53
			'meta_query'  => array(
54
				array(
55 1
					'value'   => $file,
56 1
					'compare' => 'LIKE',
57 1
					'key'     => '_wp_attachment_metadata',
58 1
				),
59 1
			),
60 1
		);
61
62 1
		$query = new WP_Query( $query_args );
63
64 1
		if ( $query->have_posts() ) {
65
66 1
			foreach ( $query->posts as $post_id ) {
67 1
				$meta = wp_get_attachment_metadata( $post_id );
68 1
				$original_file       = basename( $meta['file'] );
69 1
				$cropped_image_files = isset( $meta['sizes'] ) ? wp_list_pluck( $meta['sizes'], 'file' ) : array();
70 1
				if ( $original_file === $file || in_array( $file, $cropped_image_files ) ) {
71 1
					$attachment_id = $post_id;
72 1
					break;
73
				}
74 1
			}
75 1
		}
76
77 1
		return 0 === $attachment_id ? false : $attachment_id;
78
	}
79
80
	/**
81
	 * Utility method to get a combined list of default and custom registered image sizes
82
	 *
83
	 * @since  2.2.4
84
	 * @link   http://core.trac.wordpress.org/ticket/18947
85
	 * @global array $_wp_additional_image_sizes
86
	 * @return array The image sizes
87
	 */
88 6
	static function get_available_image_sizes() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
89 6
		global $_wp_additional_image_sizes;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
90
91 6
		$default_image_sizes = array( 'thumbnail', 'medium', 'large' );
92 6
		foreach ( $default_image_sizes as $size ) {
93 6
			$image_sizes[ $size ] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$image_sizes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $image_sizes = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
94 6
				'height' => intval( get_option( "{$size}_size_h" ) ),
95 6
				'width'  => intval( get_option( "{$size}_size_w" ) ),
96 6
				'crop'   => get_option( "{$size}_crop" ) ? get_option( "{$size}_crop" ) : false,
97
			);
98 6
		}
99
100 6
		if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) ) {
101 6
			$image_sizes = array_merge( $image_sizes, $_wp_additional_image_sizes );
0 ignored issues
show
Bug introduced by
The variable $image_sizes does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
102 6
		}
103
104 6
		return $image_sizes;
105
	}
106
107
	/**
108
	 * Utility method to return the closest named size from an array of values
109
	 *
110
	 * Based off of WordPress's image_get_intermediate_size()
111
	 * If the size matches an existing size then it will be used. If there is no
112
	 * direct match, then the nearest image size larger than the specified size
113
	 * will be used. If nothing is found, then the function will return false.
114
	 * Uses get_available_image_sizes() to get all available sizes.
115
	 *
116
	 * @since  2.2.4
117
	 * @param  array|string $size Image size. Accepts an array of width and height (in that order)
118
	 * @return false|string       Named image size e.g. 'thumbnail'
119
	 */
120 6
	public static function get_named_size( $size ) {
121 6
		$data = array();
122
123
		// Find the best match when '$size' is an array.
124 6
		if ( is_array( $size ) ) {
125 6
			$image_sizes = self::get_available_image_sizes();
126 6
			$candidates = array();
127
128 6
			foreach ( $image_sizes as $_size => $data ) {
129
130
				// If there's an exact match to an existing image size, short circuit.
131 6
				if ( $data['width'] == $size[0] && $data['height'] == $size[1] ) {
132
					$candidates[ $data['width'] * $data['height'] ] = array( $_size, $data );
133
					break;
134
				}
135
136
				// If it's not an exact match, consider larger sizes with the same aspect ratio.
137 6
				if ( $data['width'] >= $size[0] && $data['height'] >= $size[1] ) {
138
139
					/*
140
					 * To test for varying crops, we constrain the dimensions of the larger image
141
					 * to the dimensions of the smaller image and see if they match.
142
					 */
143 6
					if ( $data['width'] > $size[0] ) {
144 6
						$constrained_size = wp_constrain_dimensions( $data['width'], $data['height'], $size[0] );
145 6
						$expected_size = array( $size[0], $size[1] );
146 6
					} else {
147
						$constrained_size = wp_constrain_dimensions( $size[0], $size[1], $data['width'] );
148
						$expected_size = array( $data['width'], $data['height'] );
149
					}
150
151
					// If the image dimensions are within 1px of the expected size, we consider it a match.
152 6
					$matched = ( abs( $constrained_size[0] - $expected_size[0] ) <= 1 && abs( $constrained_size[1] - $expected_size[1] ) <= 1 );
153
154 6
					if ( $matched ) {
155 6
						$candidates[ $data['width'] * $data['height'] ] = array( $_size, $data );
156 6
					}
157 6
				}
158 6
			}
159
160 6
			if ( ! empty( $candidates ) ) {
161
				// Sort the array by size if we have more than one candidate.
162 6
				if ( 1 < count( $candidates ) ) {
163 4
					ksort( $candidates );
164 4
				}
165
166 6
				$data = array_shift( $candidates );
167 6
				$data = $data[0];
168 6
			} elseif ( ! empty( $image_sizes['thumbnail'] ) && $image_sizes['thumbnail']['width'] >= $size[0] && $image_sizes['thumbnail']['width'] >= $size[1] ) {
169
				/*
170
				 * When the size requested is smaller than the thumbnail dimensions, we
171
				 * fall back to the thumbnail size.
172
				 */
173
				$data = 'thumbnail';
174
			} else {
175
				return false;
176
			}
177 6
		} elseif ( ! empty( $image_sizes[ $size ] ) ) {
0 ignored issues
show
Bug introduced by
The variable $image_sizes seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
178
			$data = $size;
179
		}// End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
180
181
		// If we still don't have a match at this point, return false.
182 6
		if ( empty( $data ) ) {
183
			return false;
184
		}
185
186 6
		return $data;
187
	}
188
189
	/**
190
	 * Utility method that returns time string offset by timezone
191
	 *
192
	 * @since  1.0.0
193
	 * @param  string $tzstring Time string
194
	 * @return string           Offset time string
195
	 */
196 2
	public static function timezone_offset( $tzstring ) {
197 2
		$tz_offset = 0;
198
199 2
		if ( ! empty( $tzstring ) && is_string( $tzstring ) ) {
200 2
			if ( 'UTC' === substr( $tzstring, 0, 3 ) ) {
201 1
				$tzstring = str_replace( array( ':15', ':30', ':45' ), array( '.25', '.5', '.75' ), $tzstring );
202 1
				return intval( floatval( substr( $tzstring, 3 ) ) * HOUR_IN_SECONDS );
203
			}
204
205
			try {
206 1
				$date_time_zone_selected = new DateTimeZone( $tzstring );
207 1
				$tz_offset = timezone_offset_get( $date_time_zone_selected, date_create() );
208 1
			} catch ( Exception $e ) {
209
				self::log_if_debug( __METHOD__, __LINE__, $e->getMessage() );
210
			}
211
		}
212 1
213
		return $tz_offset;
214 1
	}
215
216
	/**
217
	 * Utility method that returns a timezone string representing the default timezone for the site.
218
	 *
219
	 * Roughly copied from WordPress, as get_option('timezone_string') will return
220
	 * an empty string if no value has been set on the options page.
221
	 * A timezone string is required by the wp_timezone_choice() used by the
222
	 * select_timezone field.
223
	 *
224
	 * @since  1.0.0
225
	 * @return string Timezone string
226
	 */
227
	public static function timezone_string() {
228 1
		$current_offset = get_option( 'gmt_offset' );
229 1
		$tzstring       = get_option( 'timezone_string' );
230 1
231
		// Remove old Etc mappings. Fallback to gmt_offset.
232
		if ( false !== strpos( $tzstring, 'Etc/GMT' ) ) {
233 1
			$tzstring = '';
234
		}
235
236
		if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists
237 1
			if ( 0 == $current_offset ) {
238 1
				$tzstring = 'UTC+0';
239 1
			} elseif ( $current_offset < 0 ) {
240 1
				$tzstring = 'UTC' . $current_offset;
241
			} else {
242
				$tzstring = 'UTC+' . $current_offset;
243
			}
244
		}
245 1
246
		return $tzstring;
247 1
	}
248
249
	/**
250
	 * Returns a timestamp, first checking if value already is a timestamp.
251
	 *
252
	 * @since  2.0.0
253
	 * @param  string|int $string Possible timestamp string
254
	 * @return int   	            Time stamp
255
	 */
256
	public static function make_valid_time_stamp( $string ) {
257 10
		if ( ! $string ) {
258 10
			return 0;
259
		}
260
261
		return self::is_valid_time_stamp( $string )
262 10
			? (int) $string :
263 10
			strtotime( (string) $string );
264 10
	}
265
266
	/**
267
	 * Determine if a value is a valid timestamp
268
	 *
269
	 * @since  2.0.0
270
	 * @param  mixed $timestamp Value to check
271
	 * @return boolean           Whether value is a valid timestamp
272
	 */
273
	public static function is_valid_time_stamp( $timestamp ) {
274 10
		return (string) (int) $timestamp === (string) $timestamp
275 10
			&& $timestamp <= PHP_INT_MAX
276 10
			&& $timestamp >= ~PHP_INT_MAX;
277 10
	}
278
279
	/**
280
	 * Checks if a value is 'empty'. Still accepts 0.
281
	 *
282
	 * @since  2.0.0
283
	 * @param  mixed $value Value to check
284
	 * @return bool         True or false
285
	 */
286
	public static function isempty( $value ) {
287 61
		return null === $value || '' === $value || false === $value || array() === $value;
288 61
	}
289
290
	/**
291
	 * Checks if a value is not 'empty'. 0 doesn't count as empty.
292
	 *
293
	 * @since  2.2.2
294
	 * @param  mixed $value Value to check
295
	 * @return bool         True or false
296
	 */
297
	public static function notempty( $value ) {
298 4
		return null !== $value && '' !== $value && false !== $value && array() !== $value;
299 4
	}
300
301
	/**
302
	 * Filters out empty values (not including 0).
303
	 *
304
	 * @since  2.2.2
305
	 * @param  mixed $value Value to check
306
	 * @return bool         True or false
307
	 */
308
	public static function filter_empty( $value ) {
309 3
		return array_filter( $value, array( __CLASS__, 'notempty' ) );
310 3
	}
311
312
	/**
313
	 * Insert a single array item inside another array at a set position
314
	 *
315
	 * @since  2.0.2
316
	 * @param  array &$array   Array to modify. Is passed by reference, and no return is needed.
317
	 * @param  array $new      New array to insert
318
	 * @param  int   $position Position in the main array to insert the new array
319
	 */
320
	public static function array_insert( &$array, $new, $position ) {
321 2
		$before = array_slice( $array, 0, $position - 1 );
322 2
		$after  = array_diff_key( $array, $before );
323 2
		$array  = array_merge( $before, $new, $after );
324 2
	}
325 2
326
	/**
327
	 * Defines the url which is used to load local resources.
328
	 * This may need to be filtered for local Window installations.
329
	 * If resources do not load, please check the wiki for details.
330
	 *
331
	 * @since  1.0.1
332
	 * @return string URL to CMB2 resources
333
	 */
334
	public static function url( $path = '' ) {
335 2
		if ( self::$url ) {
336 2
			return self::$url . $path;
337 1
		}
338
339
		$cmb2_url = self::get_url_from_dir( cmb2_dir() );
340 1
341
		/**
342
		 * Filter the CMB location url
343
		 *
344
		 * @param string $cmb2_url Currently registered url
345
		 */
346
		self::$url = trailingslashit( apply_filters( 'cmb2_meta_box_url', $cmb2_url, CMB2_VERSION ) );
347 1
348
		return self::$url . $path;
349 1
	}
350
351
	/**
352
	 * Converts a system path to a URL
353
	 *
354
	 * @since  2.2.2
355
	 * @param  string $dir Directory path to convert.
356
	 * @return string      Converted URL.
357
	 */
358
	public static function get_url_from_dir( $dir ) {
359 2
		$dir = self::normalize_path( $dir );
360 2
361
		// Let's test if We are in the plugins or mu-plugins dir.
362
		$test_dir = trailingslashit( $dir ) . 'unneeded.php';
363 2
		if (
364
			0 === strpos( $test_dir, self::normalize_path( WPMU_PLUGIN_DIR ) )
365 2
			|| 0 === strpos( $test_dir, self::normalize_path( WP_PLUGIN_DIR ) )
366 2
		) {
367 2
			// Ok, then use plugins_url, as it is more reliable.
368
			return trailingslashit( plugins_url( '', $test_dir ) );
369 1
		}
370
371
		// Ok, now let's test if we are in the theme dir.
372
		$theme_root = self::normalize_path( get_theme_root() );
373 2
		if ( 0 === strpos( $dir, $theme_root ) ) {
374 2
			// Ok, then use get_theme_root_uri.
375
			return set_url_scheme(
376 1
				trailingslashit(
377 1
					str_replace(
378 1
						untrailingslashit( $theme_root ),
379 1
						untrailingslashit( get_theme_root_uri() ),
380 1
						$dir
381
					)
382 1
				)
383 1
			);
384 1
		}
385
386
		// Check to see if it's anywhere in the root directory
387
		$site_dir = self::normalize_path( self::$ABSPATH );
388 2
		$site_url = trailingslashit( is_multisite() ? network_site_url() : site_url() );
389 2
390
		$url = str_replace(
391 2
			array( $site_dir, WP_PLUGIN_DIR ),
392 2
			array( $site_url, WP_PLUGIN_URL ),
393 2
			$dir
394
		);
395 2
396
		return set_url_scheme( $url );
397 2
	}
398
399
	/**
400
	 * `wp_normalize_path` wrapper for back-compat. Normalize a filesystem path.
401
	 *
402
	 * On windows systems, replaces backslashes with forward slashes
403
	 * and forces upper-case drive letters.
404
	 * Allows for two leading slashes for Windows network shares, but
405
	 * ensures that all other duplicate slashes are reduced to a single.
406
	 *
407
	 * @since 2.2.0
408
	 *
409
	 * @param string $path Path to normalize.
410
	 * @return string Normalized path.
411
	 */
412
	protected static function normalize_path( $path ) {
413 2
		if ( function_exists( 'wp_normalize_path' ) ) {
414 2
			return wp_normalize_path( $path );
415
		}
416
417
		// Replace newer WP's version of wp_normalize_path.
418
		$path = str_replace( '\\', '/', $path );
419 2
		$path = preg_replace( '|(?<=.)/+|', '/', $path );
420 2
		if ( ':' === substr( $path, 1, 1 ) ) {
421 2
			$path = ucfirst( $path );
422 1
		}
423 1
424
		return $path;
425 2
	}
426
427
	/**
428
	 * Get timestamp from text date
429
	 *
430
	 * @since  2.2.0
431
	 * @param  string $value       Date value
432
	 * @param  string $date_format Expected date format
433
	 * @return mixed               Unix timestamp representing the date.
434
	 */
435
	public static function get_timestamp_from_value( $value, $date_format ) {
436
		$date_object = date_create_from_format( $date_format, $value );
437
		return $date_object ? $date_object->setTime( 0, 0, 0 )->getTimeStamp() : strtotime( $value );
438
	}
439
440
	/**
441
	 * Takes a php date() format string and returns a string formatted to suit for the date/time pickers
442
	 * It will work with only with the following subset ot date() options:
443
	 *
444
	 *  d, j, z, m, n, y, and Y.
445
	 *
446
	 * A slight effort is made to deal with escaped characters.
447
	 *
448
	 * Other options are ignored, because they would either bring compatibility problems between PHP and JS, or
449
	 * bring even more translation troubles.
450
	 *
451
	 * @since 2.2.0
452
	 * @param string $format php date format
453
	 * @return string reformatted string
454
	 */
455
	public static function php_to_js_dateformat( $format ) {
456 5
457
		// order is relevant here, since the replacement will be done sequentially.
458
		$supported_options = array(
459
			'd' => 'dd',  // Day, leading 0
460 5
			'j' => 'd',   // Day, no 0
461 5
			'z' => 'o',   // Day of the year, no leading zeroes,
462 5
			// 'D' => 'D',   // Day name short, not sure how it'll work with translations
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
463
			// 'l' => 'DD',  // Day name full, idem before
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
464
			'm' => 'mm',  // Month of the year, leading 0
465 5
			'n' => 'm',   // Month of the year, no leading 0
466 5
			// 'M' => 'M',   // Month, Short name
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
467
			// 'F' => 'MM',  // Month, full name,
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
468
			'y' => 'y',   // Year, two digit
469 5
			'Y' => 'yy',  // Year, full
470 5
			'H' => 'HH',  // Hour with leading 0 (24 hour)
471 5
			'G' => 'H',   // Hour with no leading 0 (24 hour)
472 5
			'h' => 'hh',  // Hour with leading 0 (12 hour)
473 5
			'g' => 'h',   // Hour with no leading 0 (12 hour),
474 5
			'i' => 'mm',  // Minute with leading 0,
475 5
			's' => 'ss',  // Second with leading 0,
476 5
			'a' => 'tt',  // am/pm
477 5
			'A' => 'TT',// AM/PM
478 5
		);
479 5
480
		foreach ( $supported_options as $php => $js ) {
481 5
			// replaces every instance of a supported option, but skips escaped characters
482
			$format = preg_replace( "~(?<!\\\\)$php~", $js, $format );
483 5
		}
484 5
485
		$format = preg_replace_callback( '~(?:\\\.)+~', array( __CLASS__, 'wrap_escaped_chars' ), $format );
486 5
487
		return $format;
488 5
	}
489
490
	/**
491
	 * Helper function for CMB_Utils->php_to_js_dateformat, because php 5.2 was retarded.
492
	 *
493
	 * @since  2.2.0
494
	 * @param  $value Value to wrap/escape
495
	 * @return string Modified value
496
	 */
497
	public static function wrap_escaped_chars( $value ) {
498 4
		return '&#39;' . str_replace( '\\', '', $value[0] ) . '&#39;';
499 4
	}
500
501
	/**
502
	 * Send to debug.log if WP_DEBUG is defined and true
503
	 *
504
	 * @since  2.2.0
505
	 *
506
	 * @param  string $function Function name
507
	 * @param  int    $line     Line number
508
	 * @param  mixed  $msg      Message to output
509
	 * @param  mixed  $debug    Variable to print_r
510
	 */
511
	public static function log_if_debug( $function, $line, $msg, $debug = null ) {
512
		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
513
			error_log( "In $function, $line:" . print_r( $msg, true ) . ( $debug ? print_r( $debug, true ) : '' ) );
514
		}
515
	}
516
517
	/**
518
	 * Determine a file's extension
519
	 *
520
	 * @since  1.0.0
521
	 * @param  string $file File url
522
	 * @return string|false       File extension or false
523
	 */
524
	public static function get_file_ext( $file ) {
525 6
		$parsed = parse_url( $file, PHP_URL_PATH );
526 6
		return $parsed ? strtolower( pathinfo( $parsed, PATHINFO_EXTENSION ) ) : false;
527 6
	}
528
529
	/**
530
	 * Get the file name from a url
531
	 *
532
	 * @since  2.0.0
533
	 * @param  string $value File url or path
534
	 * @return string        File name
535
	 */
536
	public static function get_file_name_from_path( $value ) {
537 5
		$parts = explode( '/', $value );
538 5
		return is_array( $parts ) ? end( $parts ) : $value;
539 5
	}
540
541
	/**
542
	 * Check if WP version is at least $version.
543
	 *
544
	 * @since  2.2.2
545
	 * @param  string $version WP version string to compare.
546
	 * @return bool             Result of comparison check.
547
	 */
548
	public static function wp_at_least( $version ) {
549 7
		return version_compare( get_bloginfo( 'version' ), $version, '>=' );
550 7
	}
551
552
	/**
553
	 * Combines attributes into a string for a form element.
554
	 *
555
	 * @since  1.1.0
556
	 * @param  array $attrs        Attributes to concatenate.
557
	 * @param  array $attr_exclude Attributes that should NOT be concatenated.
558
	 * @return string               String of attributes for form element.
559
	 */
560
	public static function concat_attrs( $attrs, $attr_exclude = array() ) {
561 46
		$attr_exclude[] = 'rendered';
562 46
		$attributes = '';
563 46
		foreach ( $attrs as $attr => $val ) {
564 46
			$excluded = in_array( $attr, (array) $attr_exclude, true );
565 46
			$empty    = false === $val && 'value' !== $attr;
566 46
			if ( ! $excluded && ! $empty ) {
567 46
				// if data attribute, use single quote wraps, else double
568
				$quotes = false !== stripos( $attr, 'data-' ) ? "'" : '"';
569 46
				$attributes .= sprintf( ' %1$s=%3$s%2$s%3$s', $attr, $val, $quotes );
570 46
			}
571 46
		}
572 46
		return $attributes;
573 46
	}
574
575
	/**
576
	 * Ensures value is an array.
577
	 *
578
	 * @since  2.2.3
579
	 *
580
	 * @param  mixed $value   Value to ensure is array.
581
	 * @param  array $default Default array. Defaults to empty array.
582
	 *
583
	 * @return array          The array.
584
	 */
585
	public static function ensure_array( $value, $default = array() ) {
586 48
		if ( empty( $value ) ) {
587 48
			return $default;
588 44
		}
589
590
		if ( is_array( $value ) || is_object( $value ) ) {
591 6
			return (array) $value;
592 6
		}
593
594
		// Not sure anything would be non-scalar that is not an array or object?
595
		if ( ! is_scalar( $value ) ) {
596 1
			return $default;
597
		}
598
599
		return (array) $value;
600 1
	}
601
602
}
603