Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
4 / 6
CRAP
80.00% covered (warning)
80.00%
16 / 20
LeafVisitor
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
4 / 6
9.65
80.00% covered (warning)
80.00%
16 / 20
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 visitParsedBooleanNode
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 visitNegatedNode
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 visitNamespaceHeader
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 visitBooleanClause
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
8 / 8
 negated
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
<?php
namespace CirrusSearch\Parser\AST\Visitor;
use CirrusSearch\Parser\AST\BooleanClause;
use CirrusSearch\Parser\AST\NamespaceHeaderNode;
use CirrusSearch\Parser\AST\NegatedNode;
use CirrusSearch\Parser\AST\ParsedBooleanNode;
use Wikimedia\Assert\Assert;
/**
 * Visit leaves only
 */
abstract class LeafVisitor implements Visitor {
    /**
     * @var int[]
     */
    private $excludeOccurs;
    /**
     * @var bool true when this branch is "negated".
     */
    private $inNegation;
    /**
     * @param int[] $excludeOccurs
     */
    public function __construct( $excludeOccurs = [] ) {
        array_walk( $excludeOccurs, static function ( $x ) {
            BooleanClause::validateOccur( $x );
        } );
        $this->excludeOccurs = $excludeOccurs;
    }
    /**
     * @param ParsedBooleanNode $node
     */
    final public function visitParsedBooleanNode( ParsedBooleanNode $node ) {
        foreach ( $node->getClauses() as $clause ) {
            $clause->accept( $this );
        }
    }
    /**
     * @param NegatedNode $node
     */
    final public function visitNegatedNode( NegatedNode $node ) {
        /** @phan-suppress-next-line PhanImpossibleCondition I agree, this is impossible. */
        Assert::invariant( false, 'NegatedNode should be optimized at parse time' );
    }
    /**
     * @param NamespaceHeaderNode $node
     */
    final public function visitNamespaceHeader( NamespaceHeaderNode $node ) {
        /** @phan-suppress-next-line PhanImpossibleCondition I agree, this is impossible. */
        Assert::invariant( false, 'Not yet part of the AST, should not be visited.' );
    }
    /**
     * @param BooleanClause $node
     */
    final public function visitBooleanClause( BooleanClause $node ) {
        if ( in_array( $node->getOccur(), $this->excludeOccurs ) ) {
            return;
        }
        $oldNegated = $this->inNegation;
        if ( $node->getOccur() === BooleanClause::MUST_NOT ) {
            $this->inNegation = !$this->inNegation;
        }
        $node->getNode()->accept( $this );
        $this->inNegation = $oldNegated;
    }
    /**
     * @return bool true if this node is in a negation
     */
    final public function negated() {
        return $this->inNegation;
    }
}