Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
54.29% |
19 / 35 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Utils | |
54.29% |
19 / 35 |
|
40.00% |
2 / 5 |
58.21 | |
0.00% |
0 / 1 |
| is_debug | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| is_verbose | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
| set_mode | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
2.26 | |||
| describe | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| human_readable_schema | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
72 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack\Schema; |
| 4 | |
| 5 | class Utils { |
| 6 | |
| 7 | private static $mode; |
| 8 | |
| 9 | /** |
| 10 | * Is the current environment a development environment? |
| 11 | * |
| 12 | * @return bool |
| 13 | */ |
| 14 | public static function is_debug(): bool { |
| 15 | return ( |
| 16 | ( defined( 'SCHEMA_DEBUG' ) && \SCHEMA_DEBUG ) |
| 17 | || |
| 18 | self::$mode === 'debug' |
| 19 | || |
| 20 | self::$mode === 'debug-verbose' |
| 21 | ); |
| 22 | } |
| 23 | |
| 24 | public static function is_verbose() { |
| 25 | return ( |
| 26 | self::is_debug() |
| 27 | && |
| 28 | ( |
| 29 | self::$mode === 'debug-verbose' |
| 30 | || |
| 31 | ( defined( 'SCHEMA_VERBOSE' ) && \SCHEMA_VERBOSE ) |
| 32 | ) |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | public static function set_mode( $mode ) { |
| 37 | $valid_modes = array( 'debug', 'debug-verbose', null ); |
| 38 | |
| 39 | if ( ! in_array( $mode, $valid_modes, true ) ) { |
| 40 | self::$mode = null; |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | self::$mode = $mode; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * The schema generated by the parser can be very verbose and hard to read. |
| 49 | * This is a helper utility that converts the schema into a more human-readable format. |
| 50 | * |
| 51 | * @param Parser $parser The parser instance. |
| 52 | * This is the same parser instance that was used to generate the schema. |
| 53 | * It is used to convert the schema into a human-readable format. |
| 54 | * @param bool $encode Whether to encode the schema as a JSON string. |
| 55 | * |
| 56 | * @return array|string |
| 57 | */ |
| 58 | public static function describe( |
| 59 | Parser $parser, |
| 60 | $encode = false |
| 61 | ) { |
| 62 | // Process the top-level schema array |
| 63 | $description = self::human_readable_schema( $parser->schema() ); |
| 64 | |
| 65 | if ( $encode === true ) { |
| 66 | return $description; |
| 67 | } |
| 68 | // Convert the processed schema to a JSON-like string |
| 69 | return wp_json_encode( $description, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Recursive function that will crawl through the schema, |
| 74 | * and call on itself to process each sub-item. |
| 75 | * |
| 76 | * @param mixed $item The item to process. |
| 77 | * |
| 78 | * @return mixed The processed item. |
| 79 | */ |
| 80 | private static function human_readable_schema( |
| 81 | $item |
| 82 | ) { |
| 83 | // If the item is an associative array with 'type' as a key, return its value directly |
| 84 | if ( is_array( $item ) && isset( $item['type'] ) && count( $item ) === 1 ) { |
| 85 | return $item['type']; |
| 86 | } |
| 87 | |
| 88 | // If the item is an associative array with 'type' and 'value', process the value |
| 89 | if ( isset( $item['type'], $item['value'] ) && is_array( $item ) ) { |
| 90 | return self::human_readable_schema( $item['value'] ); |
| 91 | } |
| 92 | |
| 93 | // If the item is any other kind of array, process each sub-item |
| 94 | if ( is_array( $item ) ) { |
| 95 | $result = array(); |
| 96 | foreach ( $item as $key => $value ) { |
| 97 | $result[ $key ] = self::human_readable_schema( $value ); |
| 98 | } |
| 99 | return $result; |
| 100 | } |
| 101 | return $item; |
| 102 | } |
| 103 | } |