Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 14 |
CRAP | |
0.00% |
0 / 1 |
| SerializerWithTracer | |
0.00% |
0 / 27 |
|
0.00% |
0 / 14 |
380 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| handle | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| trace | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| startDocument | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| endDocument | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| 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\Serializer; |
| 5 | |
| 6 | use Wikimedia\RemexHtml\Tokenizer\Attributes; |
| 7 | use Wikimedia\RemexHtml\TreeBuilder\Element; |
| 8 | use Wikimedia\RemexHtml\TreeBuilder\TraceFormatter; |
| 9 | |
| 10 | class SerializerWithTracer extends Serializer { |
| 11 | /** @var callable|null */ |
| 12 | private $traceCallback; |
| 13 | /** @var int */ |
| 14 | private $verbosity; |
| 15 | |
| 16 | /** |
| 17 | * @param Formatter $formatter |
| 18 | * @param callable|null $errorCallback |
| 19 | * @param callable|null $traceCallback |
| 20 | * @param int $verbosity |
| 21 | */ |
| 22 | public function __construct( Formatter $formatter, $errorCallback = null, $traceCallback = null, |
| 23 | $verbosity = 0 |
| 24 | ) { |
| 25 | $this->traceCallback = $traceCallback; |
| 26 | $this->verbosity = $verbosity; |
| 27 | parent::__construct( $formatter, $errorCallback ); |
| 28 | } |
| 29 | |
| 30 | private function handle( string $funcName, array $args ) { |
| 31 | $this->trace( TraceFormatter::$funcName( ...$args ) ); |
| 32 | parent::$funcName( ...$args ); |
| 33 | if ( $this->verbosity > 0 && $funcName !== 'endDocument' ) { |
| 34 | $this->trace( "Dump after $funcName: " . $this->dump() ); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | private function trace( string $msg ) { |
| 39 | ( $this->traceCallback )( "[Serializer] $msg" ); |
| 40 | } |
| 41 | |
| 42 | /** @inheritDoc */ |
| 43 | public function startDocument( $fragmentNamespace, $fragmentName ) { |
| 44 | $this->handle( __FUNCTION__, func_get_args() ); |
| 45 | } |
| 46 | |
| 47 | /** @inheritDoc */ |
| 48 | public function endDocument( $pos ) { |
| 49 | if ( $this->nodes ) { |
| 50 | $nodeTags = ''; |
| 51 | foreach ( $this->nodes as $node ) { |
| 52 | if ( $nodeTags !== '' ) { |
| 53 | $nodeTags .= ', '; |
| 54 | } |
| 55 | $nodeTags .= $node->getDebugTag(); |
| 56 | } |
| 57 | $this->trace( "endDocument: unclosed elements: $nodeTags" ); |
| 58 | } else { |
| 59 | $this->trace( "endDocument: no unclosed elements" ); |
| 60 | } |
| 61 | |
| 62 | $this->handle( __FUNCTION__, func_get_args() ); |
| 63 | } |
| 64 | |
| 65 | /** @inheritDoc */ |
| 66 | public function characters( $preposition, $refElement, $text, $start, $length, |
| 67 | $sourceStart, $sourceLength |
| 68 | ) { |
| 69 | $this->handle( __FUNCTION__, func_get_args() ); |
| 70 | } |
| 71 | |
| 72 | /** @inheritDoc */ |
| 73 | public function insertElement( $preposition, $refElement, Element $element, $void, |
| 74 | $sourceStart, $sourceLength |
| 75 | ) { |
| 76 | $this->handle( __FUNCTION__, func_get_args() ); |
| 77 | } |
| 78 | |
| 79 | /** @inheritDoc */ |
| 80 | public function endTag( Element $element, $sourceStart, $sourceLength ) { |
| 81 | $this->handle( __FUNCTION__, func_get_args() ); |
| 82 | } |
| 83 | |
| 84 | /** @inheritDoc */ |
| 85 | public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) { |
| 86 | $this->handle( __FUNCTION__, func_get_args() ); |
| 87 | } |
| 88 | |
| 89 | /** @inheritDoc */ |
| 90 | public function comment( $preposition, $refElement, $text, $sourceStart, $sourceLength ) { |
| 91 | $this->handle( __FUNCTION__, func_get_args() ); |
| 92 | } |
| 93 | |
| 94 | /** @inheritDoc */ |
| 95 | public function error( $text, $pos ) { |
| 96 | $this->handle( __FUNCTION__, func_get_args() ); |
| 97 | } |
| 98 | |
| 99 | /** @inheritDoc */ |
| 100 | public function mergeAttributes( Element $element, Attributes $attrs, $sourceStart ) { |
| 101 | $this->handle( __FUNCTION__, func_get_args() ); |
| 102 | } |
| 103 | |
| 104 | /** @inheritDoc */ |
| 105 | public function removeNode( Element $element, $sourceStart ) { |
| 106 | $this->handle( __FUNCTION__, func_get_args() ); |
| 107 | } |
| 108 | |
| 109 | /** @inheritDoc */ |
| 110 | public function reparentChildren( Element $element, Element $newParent, $sourceStart ) { |
| 111 | $this->handle( __FUNCTION__, func_get_args() ); |
| 112 | } |
| 113 | } |