Passed
Push — master ( 495efb...ace576 )
by Brian
06:14 queued 01:46
created

raise_memory_limit()   B

Complexity

Conditions 10
Paths 7

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 21
c 1
b 0
f 0
nc 7
nop 0
dl 0
loc 31
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Class ActionScheduler_Compatibility
5
 */
6
class ActionScheduler_Compatibility {
7
8
	/**
9
	 * Converts a shorthand byte value to an integer byte value.
10
	 *
11
	 * Wrapper for wp_convert_hr_to_bytes(), moved to load.php in WordPress 4.6 from media.php
12
	 *
13
	 * @link https://secure.php.net/manual/en/function.ini-get.php
14
	 * @link https://secure.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
15
	 *
16
	 * @param string $value A (PHP ini) byte value, either shorthand or ordinary.
17
	 * @return int An integer byte value.
18
	 */
19
	public static function convert_hr_to_bytes( $value ) {
20
		if ( function_exists( 'wp_convert_hr_to_bytes' ) ) {
21
			return wp_convert_hr_to_bytes( $value );
22
		}
23
24
		$value = strtolower( trim( $value ) );
25
		$bytes = (int) $value;
26
27
		if ( false !== strpos( $value, 'g' ) ) {
28
			$bytes *= GB_IN_BYTES;
29
		} elseif ( false !== strpos( $value, 'm' ) ) {
30
			$bytes *= MB_IN_BYTES;
31
		} elseif ( false !== strpos( $value, 'k' ) ) {
32
			$bytes *= KB_IN_BYTES;
33
		}
34
35
		// Deal with large (float) values which run into the maximum integer size.
36
		return min( $bytes, PHP_INT_MAX );
37
	}
38
39
	/**
40
	 * Attempts to raise the PHP memory limit for memory intensive processes.
41
	 *
42
	 * Only allows raising the existing limit and prevents lowering it.
43
	 *
44
	 * Wrapper for wp_raise_memory_limit(), added in WordPress v4.6.0
45
	 *
46
	 * @return bool|int|string The limit that was set or false on failure.
47
	 */
48
	public static function raise_memory_limit() {
49
		if ( function_exists( 'wp_raise_memory_limit' ) ) {
50
			return wp_raise_memory_limit( 'admin' );
51
		}
52
53
		$current_limit     = @ini_get( 'memory_limit' );
54
		$current_limit_int = self::convert_hr_to_bytes( $current_limit );
55
56
		if ( -1 === $current_limit_int ) {
57
			return false;
58
		}
59
60
		$wp_max_limit       = WP_MAX_MEMORY_LIMIT;
61
		$wp_max_limit_int   = self::convert_hr_to_bytes( $wp_max_limit );
62
		$filtered_limit     = apply_filters( 'admin_memory_limit', $wp_max_limit );
63
		$filtered_limit_int = self::convert_hr_to_bytes( $filtered_limit );
64
65
		if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) {
66
			if ( false !== @ini_set( 'memory_limit', $filtered_limit ) ) {
67
				return $filtered_limit;
68
			} else {
69
				return false;
70
			}
71
		} elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) {
72
			if ( false !== @ini_set( 'memory_limit', $wp_max_limit ) ) {
73
				return $wp_max_limit;
74
			} else {
75
				return false;
76
			}
77
		}
78
		return false;
79
	}
80
81
	/**
82
	 * Attempts to raise the PHP timeout for time intensive processes.
83
	 *
84
	 * Only allows raising the existing limit and prevents lowering it. Wrapper for wc_set_time_limit(), when available.
85
	 *
86
	 * @param int The time limit in seconds.
87
	 */
88
	public static function raise_time_limit( $limit = 0 ) {
89
		if ( $limit < ini_get( 'max_execution_time' ) ) {
90
			return;
91
		}
92
93
		if ( function_exists( 'wc_set_time_limit' ) ) {
94
			wc_set_time_limit( $limit );
95
		} elseif ( function_exists( 'set_time_limit' ) && false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
96
			@set_time_limit( $limit );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for set_time_limit(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

96
			/** @scrutinizer ignore-unhandled */ @set_time_limit( $limit );

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
97
		}
98
	}
99
}
100