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