Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
14 / 15
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Constants
93.33% covered (success)
93.33%
14 / 15
83.33% covered (warning)
83.33%
5 / 6
11.04
0.00% covered (danger)
0.00%
0 / 1
 is_true
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 is_defined
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 get_constant
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 set_constant
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 clear_single_constant
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 clear_constants
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * A constants manager for Jetpack.
4 *
5 * @package automattic/jetpack-constants
6 */
7
8namespace 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 */
25class 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 a truthy value (e.g. not null, not false, not 0, any string).
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. If that also hasn't
65     * been set, attempts to get a value from filters.
66     *
67     * @param string $name The name of the constant.
68     *
69     * @return mixed null if the constant does not exist or the value of the constant.
70     */
71    public static function get_constant( $name ) {
72        if ( array_key_exists( $name, self::$set_constants ) ) {
73            return self::$set_constants[ $name ];
74        }
75
76        if ( defined( $name ) ) {
77            return constant( $name );
78        }
79
80        /**
81         * Filters the value of the constant.
82         *
83         * @since 1.2.0
84         *
85         * @param null The constant value to be filtered. The default is null.
86         * @param String $name The constant name.
87         */
88        return apply_filters( 'jetpack_constant_default_value', null, $name );
89    }
90
91    /**
92     * Sets the value of the "constant" within constants Manager.
93     *
94     * @param string                           $name The name of the constant.
95     * @param int|float|string|bool|array|null $value The value of the constant.
96     */
97    public static function set_constant( $name, $value ) {
98        self::$set_constants[ $name ] = $value;
99    }
100
101    /**
102     * Will unset a "constant" from constants Manager if the constant exists.
103     *
104     * @param string $name The name of the constant.
105     *
106     * @return bool Whether the constant was removed.
107     */
108    public static function clear_single_constant( $name ) {
109        if ( ! array_key_exists( $name, self::$set_constants ) ) {
110            return false;
111        }
112
113        unset( self::$set_constants[ $name ] );
114
115        return true;
116    }
117
118    /**
119     * Resets all of the constants within constants Manager.
120     */
121    public static function clear_constants() {
122        self::$set_constants = array();
123    }
124}