Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 18 |
CRAP | |
0.00% |
0 / 1 |
| TreeMutationTracer | |
0.00% |
0 / 26 |
|
0.00% |
0 / 18 |
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 | |||
| traceEvent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| handleMutation | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| handleSimple | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| before | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| after | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| startDocument | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| endDocument | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| characters | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| insertElement | |
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 | |||
| error | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| mergeAttributes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| removeNode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| reparentChildren | |
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 | |
| 8 | /** |
| 9 | * This is a debugging helper class which calls the supplied callback function |
| 10 | * each time there is a TreeHandler event, giving a descriptive message. It |
| 11 | * then forwards the event through to the supplied handler. |
| 12 | */ |
| 13 | class TreeMutationTracer implements TreeHandler { |
| 14 | private TreeHandler $handler; |
| 15 | |
| 16 | /** @var callable */ |
| 17 | private $callback; |
| 18 | |
| 19 | /** @var int */ |
| 20 | private $verbosity; |
| 21 | |
| 22 | /** |
| 23 | * Constructor. |
| 24 | * |
| 25 | * @param TreeHandler $handler The next pipeline stage |
| 26 | * @param callable $callback The message output function |
| 27 | * @param int $verbosity Set to non-zero to call dump() on the handler |
| 28 | * before and after each event. |
| 29 | */ |
| 30 | public function __construct( TreeHandler $handler, callable $callback, $verbosity = 0 ) { |
| 31 | $this->handler = $handler; |
| 32 | $this->callback = $callback; |
| 33 | $this->verbosity = $verbosity; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Send a message |
| 38 | * |
| 39 | * @param string $msg |
| 40 | */ |
| 41 | private function trace( $msg ) { |
| 42 | ( $this->callback )( "[Tree] $msg" ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Send a message for an event |
| 47 | * |
| 48 | * @param string $funcName |
| 49 | * @param array $args |
| 50 | */ |
| 51 | private function traceEvent( $funcName, $args ) { |
| 52 | $this->trace( TraceFormatter::$funcName( ...$args ) ); |
| 53 | } |
| 54 | |
| 55 | private function handleMutation( string $funcName, array $args ) { |
| 56 | $this->traceEvent( $funcName, $args ); |
| 57 | $this->before(); |
| 58 | $this->handler->$funcName( ...$args ); |
| 59 | $this->after(); |
| 60 | } |
| 61 | |
| 62 | private function handleSimple( string $funcName, array $args ) { |
| 63 | $this->traceEvent( $funcName, $args ); |
| 64 | $this->handler->$funcName( ...$args ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * A helper called before the underlying handler is called. |
| 69 | */ |
| 70 | private function before() { |
| 71 | if ( $this->verbosity > 0 ) { |
| 72 | // @phan-suppress-next-line PhanUndeclaredMethod |
| 73 | $this->trace( "Before: " . $this->handler->dump() . "\n" ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * A helper called after the underlying handler is called. |
| 79 | */ |
| 80 | private function after() { |
| 81 | if ( $this->verbosity > 0 ) { |
| 82 | // @phan-suppress-next-line PhanUndeclaredMethod |
| 83 | $this->trace( "After: " . $this->handler->dump() . "\n" ); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @inheritDoc |
| 89 | */ |
| 90 | public function startDocument( $fns, $fn ) { |
| 91 | $this->handleSimple( __FUNCTION__, func_get_args() ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @inheritDoc |
| 96 | */ |
| 97 | public function endDocument( $pos ) { |
| 98 | $this->handleSimple( __FUNCTION__, func_get_args() ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @inheritDoc |
| 103 | */ |
| 104 | public function characters( $preposition, $refNode, $text, $start, $length, |
| 105 | $sourceStart, $sourceLength |
| 106 | ) { |
| 107 | $this->handleMutation( __FUNCTION__, func_get_args() ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @inheritDoc |
| 112 | */ |
| 113 | public function insertElement( $preposition, $refNode, Element $element, $void, |
| 114 | $sourceStart, $sourceLength |
| 115 | ) { |
| 116 | $this->handleMutation( __FUNCTION__, func_get_args() ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @inheritDoc |
| 121 | */ |
| 122 | public function endTag( Element $element, $sourceStart, $sourceLength ) { |
| 123 | $this->handleMutation( __FUNCTION__, func_get_args() ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @inheritDoc |
| 128 | */ |
| 129 | public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) { |
| 130 | $this->handleMutation( __FUNCTION__, func_get_args() ); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @inheritDoc |
| 135 | */ |
| 136 | public function comment( $preposition, $refNode, $text, $sourceStart, $sourceLength ) { |
| 137 | $this->handleMutation( __FUNCTION__, func_get_args() ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @inheritDoc |
| 142 | */ |
| 143 | public function error( $text, $pos ) { |
| 144 | $this->handleSimple( __FUNCTION__, func_get_args() ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @inheritDoc |
| 149 | */ |
| 150 | public function mergeAttributes( Element $element, Attributes $attrs, $sourceStart ) { |
| 151 | $this->handleMutation( __FUNCTION__, func_get_args() ); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @inheritDoc |
| 156 | */ |
| 157 | public function removeNode( Element $element, $sourceStart ) { |
| 158 | $this->handleMutation( __FUNCTION__, func_get_args() ); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @inheritDoc |
| 163 | */ |
| 164 | public function reparentChildren( Element $element, Element $newParent, $sourceStart ) { |
| 165 | $this->handleMutation( __FUNCTION__, func_get_args() ); |
| 166 | } |
| 167 | } |