Completed
Push — try/xmlrpc-server-package-clas... ( 6199c0...8e0ceb )
by
unknown
07:26
created

Constants   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A is_true() 0 3 2
A is_defined() 0 5 2
A get_constant() 0 7 3
A set_constant() 0 3 1
A clear_single_constant() 0 9 2
A clear_constants() 0 3 1
1
<?php
2
/**
3
 * A constants manager for Jetpack.
4
 *
5
 * @package jetpack-constants
6
 */
7
8
namespace Automattic\Jetpack;
9
10
/**
11
 * Class Automattic\Jetpack\Constants
12
 *
13
 * Testing constants is hard. Once you define a constant, it's defined. Constants Manager is an
14
 * abstraction layer so that unit tests can set "constants" for tests.
15
 *
16
 * To test your code, you'll need to swap out `defined( 'CONSTANT' )` with `Automattic\Jetpack\Constants::is_defined( 'CONSTANT' )`
17
 * and replace `CONSTANT` with `Automattic\Jetpack\Constants::get_constant( 'CONSTANT' )`. Then in the unit test, you can set the
18
 * constant with `Automattic\Jetpack\Constants::set_constant( 'CONSTANT', $value )` and then clean up after each test with something like
19
 * this:
20
 *
21
 * function tearDown() {
22
 *     Automattic\Jetpack\Constants::clear_constants();
23
 * }
24
 */
25
class Constants {
26
	/**
27
	 * A container for all defined constants.
28
	 *
29
	 * @access public
30
	 * @static
31
	 *
32
	 * @var array.
33
	 */
34
	public static $set_constants = array();
35
36
	/**
37
	 * Checks if a "constant" has been set in constants Manager
38
	 * and has the value of true
39
	 *
40
	 * @param string $name The name of the constant.
41
	 *
42
	 * @return bool
43
	 */
44
	public static function is_true( $name ) {
45
		return self::is_defined( $name ) && self::get_constant( $name );
46
	}
47
48
	/**
49
	 * Checks if a "constant" has been set in constants Manager, and if not,
50
	 * checks if the constant was defined with define( 'name', 'value ).
51
	 *
52
	 * @param string $name The name of the constant.
53
	 *
54
	 * @return bool
55
	 */
56
	public static function is_defined( $name ) {
57
		return array_key_exists( $name, self::$set_constants )
58
			? true
59
			: defined( $name );
60
	}
61
62
	/**
63
	 * Attempts to retrieve the "constant" from constants Manager, and if it hasn't been set,
64
	 * then attempts to get the constant with the constant() function.
65
	 *
66
	 * @param string $name The name of the constant.
67
	 *
68
	 * @return mixed null if the constant does not exist or the value of the constant.
69
	 */
70
	public static function get_constant( $name ) {
71
		if ( array_key_exists( $name, self::$set_constants ) ) {
72
			return self::$set_constants[ $name ];
73
		}
74
75
		return defined( $name ) ? constant( $name ) : null;
76
	}
77
78
	/**
79
	 * Sets the value of the "constant" within constants Manager.
80
	 *
81
	 * @param string $name The name of the constant.
82
	 * @param string $value The value of the constant.
83
	 */
84
	public static function set_constant( $name, $value ) {
85
		self::$set_constants[ $name ] = $value;
86
	}
87
88
	/**
89
	 * Will unset a "constant" from constants Manager if the constant exists.
90
	 *
91
	 * @param string $name The name of the constant.
92
	 *
93
	 * @return bool Whether the constant was removed.
94
	 */
95
	public static function clear_single_constant( $name ) {
96
		if ( ! array_key_exists( $name, self::$set_constants ) ) {
97
			return false;
98
		}
99
100
		unset( self::$set_constants[ $name ] );
101
102
		return true;
103
	}
104
105
	/**
106
	 * Resets all of the constants within constants Manager.
107
	 */
108
	public static function clear_constants() {
109
		self::$set_constants = array();
110
	}
111
}
112