Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 13 |
CRAP | |
0.00% |
0 / 1 |
| DispatchTracer | |
0.00% |
0 / 38 |
|
0.00% |
0 / 13 |
420 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| trace | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| excerpt | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| wrap | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| getHandlerName | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| startDocument | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| endDocument | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| error | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| characters | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| startTag | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| endTag | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| doctype | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| comment | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Wikimedia\RemexHtml\TreeBuilder; |
| 5 | |
| 6 | use Wikimedia\RemexHtml\Tokenizer\Attributes; |
| 7 | use Wikimedia\RemexHtml\Tokenizer\TokenHandler; |
| 8 | use Wikimedia\RemexHtml\Tokenizer\Tokenizer; |
| 9 | |
| 10 | /** |
| 11 | * This is a debugging helper class which calls a callback function with a |
| 12 | * descriptive message each time a token event comes from the Tokenizer. The |
| 13 | * messages include information about the current state and transitions of the |
| 14 | * Dispatcher which is the next stage in the pipeline. |
| 15 | */ |
| 16 | class DispatchTracer implements TokenHandler { |
| 17 | /** @var string */ |
| 18 | private $input; |
| 19 | private Dispatcher $dispatcher; |
| 20 | /** @var callable */ |
| 21 | private $callback; |
| 22 | |
| 23 | public function __construct( string $input, Dispatcher $dispatcher, callable $callback ) { |
| 24 | $this->input = $input; |
| 25 | $this->dispatcher = $dispatcher; |
| 26 | $this->callback = $callback; |
| 27 | } |
| 28 | |
| 29 | private function trace( string $msg ) { |
| 30 | ( $this->callback )( "[Dispatch] $msg" ); |
| 31 | } |
| 32 | |
| 33 | private function excerpt( string $text ): string { |
| 34 | if ( strlen( $text ) > 20 ) { |
| 35 | $text = substr( $text, 0, 20 ) . '...'; |
| 36 | } |
| 37 | return str_replace( "\n", "\\n", $text ); |
| 38 | } |
| 39 | |
| 40 | private function wrap( string $funcName, int $sourceStart, int $sourceLength, array $args ) { |
| 41 | $prevHandler = $this->getHandlerName(); |
| 42 | $excerpt = $this->excerpt( substr( $this->input, $sourceStart, $sourceLength ) ); |
| 43 | $msg = "$funcName $prevHandler \"$excerpt\""; |
| 44 | $this->trace( $msg ); |
| 45 | $this->dispatcher->$funcName( ...$args ); |
| 46 | $handler = $this->getHandlerName(); |
| 47 | if ( $prevHandler !== $handler ) { |
| 48 | $this->trace( "$prevHandler -> $handler" ); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | private function getHandlerName(): string { |
| 53 | $handler = $this->dispatcher->getHandler(); |
| 54 | $name = $handler ? get_class( $handler ) : 'NULL'; |
| 55 | $slashPos = strrpos( $name, '\\' ); |
| 56 | if ( $slashPos === false ) { |
| 57 | return $name; |
| 58 | } else { |
| 59 | return substr( $name, $slashPos + 1 ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** @inheritDoc */ |
| 64 | public function startDocument( Tokenizer $tokenizer, $ns, $name ) { |
| 65 | $prevHandler = $this->getHandlerName(); |
| 66 | $nsMsg = $ns === null ? 'NULL' : $ns; |
| 67 | $nameMsg = $name === null ? 'NULL' : $name; |
| 68 | $this->trace( "startDocument $prevHandler $nsMsg $nameMsg" ); |
| 69 | $this->dispatcher->startDocument( $tokenizer, $ns, $name ); |
| 70 | $handler = $this->getHandlerName(); |
| 71 | if ( $prevHandler !== $handler ) { |
| 72 | $this->trace( "$prevHandler -> $handler" ); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** @inheritDoc */ |
| 77 | public function endDocument( $pos ) { |
| 78 | $this->wrap( __FUNCTION__, $pos, 0, func_get_args() ); |
| 79 | } |
| 80 | |
| 81 | /** @inheritDoc */ |
| 82 | public function error( $text, $pos ) { |
| 83 | $handler = $this->getHandlerName(); |
| 84 | $this->trace( "error $handler \"$text\"" ); |
| 85 | $this->dispatcher->error( $text, $pos ); |
| 86 | } |
| 87 | |
| 88 | /** @inheritDoc */ |
| 89 | public function characters( $text, $start, $length, $sourceStart, $sourceLength ) { |
| 90 | $this->wrap( __FUNCTION__, $sourceStart, $sourceLength, func_get_args() ); |
| 91 | } |
| 92 | |
| 93 | /** @inheritDoc */ |
| 94 | public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) { |
| 95 | $this->wrap( __FUNCTION__, $sourceStart, $sourceLength, func_get_args() ); |
| 96 | } |
| 97 | |
| 98 | /** @inheritDoc */ |
| 99 | public function endTag( $name, $sourceStart, $sourceLength ) { |
| 100 | $this->wrap( __FUNCTION__, $sourceStart, $sourceLength, func_get_args() ); |
| 101 | } |
| 102 | |
| 103 | /** @inheritDoc */ |
| 104 | public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) { |
| 105 | $this->wrap( __FUNCTION__, $sourceStart, $sourceLength, func_get_args() ); |
| 106 | } |
| 107 | |
| 108 | /** @inheritDoc */ |
| 109 | public function comment( $text, $sourceStart, $sourceLength ) { |
| 110 | $this->wrap( __FUNCTION__, $sourceStart, $sourceLength, func_get_args() ); |
| 111 | } |
| 112 | } |