Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
65.96% |
31 / 47 |
|
63.64% |
7 / 11 |
CRAP | |
0.00% |
0 / 1 |
| Schema_Context | |
65.96% |
31 / 47 |
|
63.64% |
7 / 11 |
49.66 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| add_to_path | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| trace | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
42 | |||
| log | |
93.75% |
15 / 16 |
|
0.00% |
0 / 1 |
6.01 | |||
| verbose_log | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| get_log | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_path | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| remove_path | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| get_name | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| set_data | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| get_data | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack\Schema; |
| 4 | |
| 5 | class Schema_Context { |
| 6 | |
| 7 | private $name; |
| 8 | private $data; |
| 9 | private $path = array(); |
| 10 | |
| 11 | /** |
| 12 | * @var array $log Log of notable actions taken during parsing. |
| 13 | */ |
| 14 | private $log = array(); |
| 15 | |
| 16 | /** |
| 17 | * @param string $name |
| 18 | */ |
| 19 | public function __construct( $name ) { |
| 20 | $this->name = $name; |
| 21 | } |
| 22 | |
| 23 | public function add_to_path( $key ) { |
| 24 | $this->path[] = $key; |
| 25 | } |
| 26 | |
| 27 | private function trace( $depth_limit = 15 ) { |
| 28 | |
| 29 | if ( ! Utils::is_debug() ) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | $trace = array(); |
| 34 | // This is fine, it's guarded by `SCHEMA_TRACE` constant. |
| 35 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace |
| 36 | foreach ( debug_backtrace() as $stack_frame ) { |
| 37 | if ( isset( $stack_frame['line'], $stack_frame['file'] ) ) { |
| 38 | $filename_pieces = explode( '.', basename( $stack_frame['file'] ), 2 ); |
| 39 | $trace[] = $filename_pieces[0] . ':' . $stack_frame['line']; |
| 40 | } elseif ( isset( $stack_frame['class'], $stack_frame['function'] ) ) { |
| 41 | $trace[] = $stack_frame['class'] . '::' . $stack_frame['function']; |
| 42 | } |
| 43 | |
| 44 | --$depth_limit; |
| 45 | if ( $depth_limit <= 0 ) { |
| 46 | break; |
| 47 | } |
| 48 | } |
| 49 | return $trace; |
| 50 | } |
| 51 | |
| 52 | public function log( $message, $data, $error = null ) { |
| 53 | if ( ! Utils::is_debug() ) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | $meta = array( |
| 58 | 'name' => $this->get_name(), |
| 59 | 'path' => $this->get_path(), |
| 60 | ); |
| 61 | |
| 62 | $trace = defined( 'SCHEMA_TRACE' ) && \SCHEMA_TRACE > 0 ? $this->trace( \SCHEMA_TRACE ) : null; |
| 63 | if ( $trace ) { |
| 64 | $meta['trace'] = $trace; |
| 65 | } |
| 66 | |
| 67 | if ( $error instanceof Schema_Error ) { |
| 68 | $meta['error_message'] = $error->getMessage(); |
| 69 | $meta['value'] = $error->get_value(); |
| 70 | } |
| 71 | |
| 72 | $this->log[] = array( |
| 73 | 'message' => $message, |
| 74 | 'meta' => $meta, |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | public function verbose_log( $message, $data ) { |
| 79 | if ( ! Utils::is_verbose() ) { |
| 80 | return; |
| 81 | } |
| 82 | $this->log( $message, $data ); |
| 83 | } |
| 84 | |
| 85 | public function get_log() { |
| 86 | return $this->log; |
| 87 | } |
| 88 | |
| 89 | public function get_path() { |
| 90 | $path = $this->name; |
| 91 | if ( ! empty( $this->path ) ) { |
| 92 | $path .= '.' . implode( '.', $this->path ); |
| 93 | } |
| 94 | return $path; |
| 95 | } |
| 96 | |
| 97 | public function remove_path( $key ) { |
| 98 | $index = array_search( $key, $this->path, true ); |
| 99 | if ( $index !== false ) { |
| 100 | unset( $this->path[ $index ] ); |
| 101 | } |
| 102 | // Reindex the array. |
| 103 | $this->path = array_values( $this->path ); |
| 104 | } |
| 105 | |
| 106 | public function get_name() { |
| 107 | return $this->name; |
| 108 | } |
| 109 | |
| 110 | public function set_data( $data ) { |
| 111 | if ( ! isset( $this->data ) ) { |
| 112 | $this->data = $data; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | public function get_data() { |
| 117 | return $this->data; |
| 118 | } |
| 119 | } |