Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 40 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| DefaultTracer | |
0.00% |
0 / 40 |
|
0.00% |
0 / 3 |
182 | |
0.00% |
0 / 1 |
| trace | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
30 | |||
| log | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| formatArgs | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Wikimedia\WikiPEG; |
| 5 | |
| 6 | use InvalidArgumentException; |
| 7 | |
| 8 | class DefaultTracer implements Tracer { |
| 9 | protected int $indentLevel = 0; |
| 10 | |
| 11 | public function trace( array $event ): void { |
| 12 | switch ( $event['type'] ) { |
| 13 | case 'rule.enter': |
| 14 | $this->log( $event ); |
| 15 | $this->indentLevel++; |
| 16 | break; |
| 17 | |
| 18 | case 'rule.match': |
| 19 | $this->indentLevel--; |
| 20 | $this->log( $event ); |
| 21 | break; |
| 22 | |
| 23 | case 'rule.fail': |
| 24 | $this->indentLevel--; |
| 25 | $this->log( $event ); |
| 26 | break; |
| 27 | |
| 28 | default: |
| 29 | throw new InvalidArgumentException( "Invalid event type {$event['type']}" ); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | protected function log( array $event ) { |
| 34 | print str_pad( |
| 35 | '' . $event['location'], |
| 36 | 20 |
| 37 | ) |
| 38 | . str_pad( $event['type'], 10 ) . ' ' |
| 39 | . str_repeat( ' ', $this->indentLevel ) . $event['rule'] |
| 40 | . $this->formatArgs( $event['args'] ?? null ) |
| 41 | . "\n"; |
| 42 | } |
| 43 | |
| 44 | protected function formatArgs( ?array $argMap ): string { |
| 45 | if ( !$argMap ) { |
| 46 | return ''; |
| 47 | } |
| 48 | |
| 49 | $argParts = []; |
| 50 | foreach ( $argMap as $argName => $argValue ) { |
| 51 | if ( $argName === '$silence' ) { |
| 52 | continue; |
| 53 | } |
| 54 | if ( $argName === '$boolParams' ) { |
| 55 | $argParts[] = '0x' . base_convert( $argValue, 10, 16 ); |
| 56 | } else { |
| 57 | $displayName = str_replace( '$param_', '', $argName ); |
| 58 | if ( $displayName[0] === '&' ) { |
| 59 | $displayName = substr( $displayName, 1 ); |
| 60 | $ref = '&'; |
| 61 | } else { |
| 62 | $ref = ''; |
| 63 | } |
| 64 | $argParts[] = "$displayName=$ref" . |
| 65 | json_encode( $argValue, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); |
| 66 | } |
| 67 | } |
| 68 | if ( $argParts ) { |
| 69 | return '<' . implode( ', ', $argParts ) . '>'; |
| 70 | } else { |
| 71 | return ''; |
| 72 | } |
| 73 | } |
| 74 | } |