Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
88.89% covered (warning)
88.89%
16 / 18
CRAP
95.56% covered (success)
95.56%
43 / 45
TreeMutationTracer
0.00% covered (danger)
0.00%
0 / 1
88.89% covered (warning)
88.89%
16 / 18
20
95.45% covered (success)
95.45%
42 / 44
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 trace
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 traceEvent
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 handleMutation
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
5 / 5
 handleSimple
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 before
0.00% covered (danger)
0.00%
0 / 1
2.15
66.67% covered (warning)
66.67%
2 / 3
 after
0.00% covered (danger)
0.00%
0 / 1
2.15
66.67% covered (warning)
66.67%
2 / 3
 startDocument
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 endDocument
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 characters
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 insertElement
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 endTag
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 doctype
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 comment
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 error
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 mergeAttributes
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 removeNode
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 reparentChildren
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
<?php
namespace Wikimedia\RemexHtml\TreeBuilder;
use Wikimedia\RemexHtml\Tokenizer\Attributes;
/**
 * This is a debugging helper class which calls the supplied callback function
 * each time there is a TreeHandler event, giving a descriptive message. It
 * then forwards the event through to the supplied handler.
 */
class TreeMutationTracer implements TreeHandler {
    /** @var TreeHandler */
    private $handler;
    /** @var callable */
    private $callback;
    /** @var int */
    private $verbosity;
    /**
     * Constructor.
     *
     * @param TreeHandler $handler The next pipeline stage
     * @param callable $callback The message output function
     * @param int $verbosity Set to non-zero to call dump() on the handler
     *   before and after each event.
     */
    public function __construct( TreeHandler $handler, callable $callback, $verbosity = 0 ) {
        $this->handler = $handler;
        $this->callback = $callback;
        $this->verbosity = $verbosity;
    }
    /**
     * Send a message
     *
     * @param string $msg
     */
    private function trace( $msg ) {
        call_user_func( $this->callback, "[Tree] $msg" );
    }
    /**
     * Send a message for an event
     *
     * @param string $funcName
     * @param array $args
     */
    private function traceEvent( $funcName, $args ) {
        $this->trace( call_user_func_array( [ TraceFormatter::class, $funcName ], $args ) );
    }
    private function handleMutation( $funcName, $args ) {
        $this->traceEvent( $funcName, $args );
        $this->before();
        call_user_func_array( [ $this->handler, $funcName ], $args );
        $this->after();
    }
    private function handleSimple( $funcName, $args ) {
        $this->traceEvent( $funcName, $args );
        call_user_func_array( [ $this->handler, $funcName ], $args );
    }
    /**
     * A helper called before the underlying handler is called.
     */
    private function before() {
        if ( $this->verbosity > 0 ) {
            $this->trace( "Before: " . $this->handler->dump() . "\n" );
        }
    }
    /**
     * A helper called after the underlying handler is called.
     */
    private function after() {
        if ( $this->verbosity > 0 ) {
            $this->trace( "After:  " . $this->handler->dump() . "\n" );
        }
    }
    /**
     * @inheritDoc
     */
    public function startDocument( $fns, $fn ) {
        $this->handleSimple( __FUNCTION__, func_get_args() );
    }
    /**
     * @inheritDoc
     */
    public function endDocument( $pos ) {
        $this->handleSimple( __FUNCTION__, func_get_args() );
    }
    /**
     * @inheritDoc
     */
    public function characters( $preposition, $refNode, $text, $start, $length,
        $sourceStart, $sourceLength
    ) {
        $this->handleMutation( __FUNCTION__, func_get_args() );
    }
    /**
     * @inheritDoc
     */
    public function insertElement( $preposition, $refNode, Element $element, $void,
        $sourceStart, $sourceLength
    ) {
        $this->handleMutation( __FUNCTION__, func_get_args() );
    }
    /**
     * @inheritDoc
     */
    public function endTag( Element $element, $sourceStart, $sourceLength ) {
        $this->handleMutation( __FUNCTION__, func_get_args() );
    }
    /**
     * @inheritDoc
     */
    public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) {
        $this->handleMutation( __FUNCTION__, func_get_args() );
    }
    /**
     * @inheritDoc
     */
    public function comment( $preposition, $refNode, $text, $sourceStart, $sourceLength ) {
        $this->handleMutation( __FUNCTION__, func_get_args() );
    }
    /**
     * @inheritDoc
     */
    public function error( $text, $pos ) {
        $this->handleSimple( __FUNCTION__, func_get_args() );
    }
    /**
     * @inheritDoc
     */
    public function mergeAttributes( Element $element, Attributes $attrs, $sourceStart ) {
        $this->handleMutation( __FUNCTION__, func_get_args() );
    }
    /**
     * @inheritDoc
     */
    public function removeNode( Element $element, $sourceStart ) {
        $this->handleMutation( __FUNCTION__, func_get_args() );
    }
    /**
     * @inheritDoc
     */
    public function reparentChildren( Element $element, Element $newParent, $sourceStart ) {
        $this->handleMutation( __FUNCTION__, func_get_args() );
    }
}
// Retain the old namespace for backwards compatibility.
class_alias( TreeMutationTracer::class, 'RemexHtml\TreeBuilder\TreeMutationTracer' );