Completed
Push — fix/conflicting-libraries-load... ( e7daa5...555144 )
by
unknown
08:39 queued 01:50
created

class.jetpack-constants.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Class Jetpack_Constants
5
 *
6
 * Testing constants is hard. Once you define a constant, it's defined. Jetpack_Constants is an
7
 * abstraction layer so that unit tests can set "constants" for tests.
8
 *
9
 * To test your code, you'll need to swap out `defined( 'CONSTANT' )` with `Jetpack_Constants::is_defined( 'CONSTANT' )`
10
 * and replace `CONSTANT` with `Jetpack_Constants::get_constant( 'CONSTANT' )`. Then in the unit test, you can set the
11
 * constant with `Jetpack::set_constant( 'CONSTANT', $value )` and then clean up after each test with something like
12
 * this:
13
 *
14
 * function tearDown() {
15
 *     Jetpack_Constants::clear_constants();
16
 * }
17
 */
18 View Code Duplication
class Jetpack_Constants {
0 ignored issues
show
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
	static $set_constants = array();
20
21
	/**
22
	 * Checks if a "constant" has been set in Jetpack_Constants, and if not,
23
	 * checks if the constant was defined with define( 'name', 'value ).
24
	 *
25
	 * @param $name string The name of the constant
26
	 *
27
	 * @return bool
28
	 */
29
	public static function is_defined( $name ) {
30
		return array_key_exists( $name, self::$set_constants )
31
			? true
32
			: defined( $name );
33
	}
34
	/**
35
	 * Checks if a "constant" has been set in Jetpack_Constants
36
	 * and has the value of true
37
	 *
38
	 * @param $name string The name of the constant
39
	 *
40
	 * @return bool
41
	 */
42
	public static function is_true( $name ) {
43
		return self::is_defined( $name) && self::get_constant( $name );
44
	}
45
46
	/**
47
	 * Attempts to retrieve the "constant" from Jetpack_Constants, and if it hasn't been set,
48
	 * then attempts to get the constant with the constant() function.
49
	 *
50
	 * @param $name
51
	 *
52
	 * @return mixed null if the constant does not exist or the value of the constant.
53
	 */
54
	public static function get_constant( $name ) {
55
		if ( array_key_exists( $name,  self::$set_constants ) ) {
56
			return self::$set_constants[ $name ];
57
		}
58
59
		return defined( $name ) ? constant( $name ) : null;
60
	}
61
62
	/**
63
	 * Sets the value of the "constant" within Jetpack_Constants.
64
	 *
65
	 * @param $name  string The name of the "constant"
66
	 * @param $value string The value of the "constant"
67
	 */
68
	public static function set_constant( $name, $value ) {
69
		self::$set_constants[ $name ] = $value;
70
	}
71
72
	/**
73
	 * Will unset a "constant" from Jetpack_Constants if the constant exists.
74
	 *
75
	 * @param $name string The name of the "constant"
76
	 *
77
	 * @return bool Whether the constant was removed.
78
	 */
79
	public static function clear_single_constant( $name ) {
80
		if ( ! array_key_exists( $name, self::$set_constants ) ) {
81
			return false;
82
		}
83
84
		unset( self::$set_constants[ $name ] );
85
		return true;
86
	}
87
88
	/**
89
	 * Resets all of the constants within Jetpack_Constants.
90
	 */
91
	public static function clear_constants() {
92
		self::$set_constants = array();
93
	}
94
}
95