Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
10 / 10
CRAP
100.00% covered (success)
100.00%
1 / 1
GrammarMatch
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
10 / 10
14
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 getValues
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStart
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLength
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getNext
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCapturedMatches
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUniqueID
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 fixWhitespace
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare( strict_types = 1 );
3
4/**
5 * @file
6 * @license https://opensource.org/licenses/Apache-2.0 Apache-2.0
7 */
8
9namespace Wikimedia\CSS\Grammar;
10
11use Wikimedia\CSS\Objects\ComponentValue;
12use Wikimedia\CSS\Objects\ComponentValueList;
13use Wikimedia\CSS\Objects\Token;
14use Wikimedia\CSS\Util;
15
16/**
17 * Represent a match from a Matcher.
18 */
19class GrammarMatch {
20
21    /** @var int */
22    protected $start;
23
24    /** @var int */
25    protected $length;
26
27    /** @var ComponentValue[] Matched ComponentValues */
28    protected $values;
29
30    /** @var string|null */
31    protected $name = null;
32
33    /** @var GrammarMatch[] Captured submatches */
34    protected $capturedMatches = [];
35
36    /**
37     * @param ComponentValueList $list List containing the match
38     * @param int $start Starting index of the match.
39     * @param int $length Number of tokens in the match.
40     * @param string|null $name Give a name to this match.
41     * @param GrammarMatch[] $capturedMatches Captured submatches of this match.
42     */
43    public function __construct(
44        ComponentValueList $list, $start, $length, $name = null, array $capturedMatches = []
45    ) {
46        Util::assertAllInstanceOf( $capturedMatches, self::class, '$capturedMatches' );
47
48        $this->values = $list->slice( $start, $length );
49        $this->start = $start;
50        $this->length = $length;
51        $this->name = $name;
52        $this->capturedMatches = $capturedMatches;
53    }
54
55    /**
56     * The matched values
57     * @return ComponentValue[]
58     */
59    public function getValues() {
60        return $this->values;
61    }
62
63    /**
64     * The starting position of this match within the original list of values.
65     * @return int
66     */
67    public function getStart() {
68        return $this->start;
69    }
70
71    /**
72     * The length of this match
73     * @return int
74     */
75    public function getLength() {
76        return $this->length;
77    }
78
79    /**
80     * The position after this match, as in index into the original list of values.
81     * @return int
82     */
83    public function getNext() {
84        return $this->start + $this->length;
85    }
86
87    /**
88     * The name of this match
89     * @return string|null
90     */
91    public function getName() {
92        return $this->name;
93    }
94
95    /**
96     * The captured submatches of this match
97     *
98     * This returns the matches from capturing submatchers (see
99     * Matcher::capture()) that matched during the matching of the top-level
100     * matcher that returned this match. If capturing submatchers were nested,
101     * the GrammarMatch objects returned here will themselves have captured sub-
102     * matches to return.
103     *
104     * To borrow PCRE regular expression syntax, if the "pattern" described by
105     * the Matchers resembled `www(?<A>xxx(?<B>yyy)xxx)(?<C>zzz)*` then the
106     * top-level GrammarMatch's getCapturedMatches() would return a GrammarMatch
107     * named "A" (containing the "xxxyyyxxx" bit) and zero or more matches named
108     * "C" (for each "zzz"), and that "A" GrammarMatch's getCapturedMatches()
109     * would return a GrammarMatch named "B" (containing just the "yyy").
110     *
111     * Note that the start and end positions reported by captured matches may be
112     * relative to a containing SimpleBlock or CSSFunction's value rather than
113     * to the ComponentValueList passed to the top-level Matcher.
114     *
115     * @return GrammarMatch[]
116     */
117    public function getCapturedMatches() {
118        return $this->capturedMatches;
119    }
120
121    /**
122     * Return a key for this matcher's state
123     * @return string
124     */
125    public function getUniqueID() {
126        $data = [ $this->start, $this->length, $this->name ];
127        foreach ( $this->capturedMatches as $m ) {
128            $data[] = $m->getUniqueId();
129        }
130        return md5( implode( "\n", $data ) );
131    }
132
133    /**
134     * Replace whitespace in the matched values
135     * @private For use by Matcher only
136     * @param Token $old
137     * @param Token $new
138     */
139    public function fixWhitespace( Token $old, Token $new ) {
140        foreach ( $this->values as $k => $v ) {
141            if ( $v === $old ) {
142                $this->values[$k] = $new;
143            }
144        }
145        foreach ( $this->capturedMatches as $m ) {
146            $m->fixWhitespace( $old, $new );
147        }
148    }
149
150    public function __toString() {
151        return Util::stringify( $this->getValues() );
152    }
153}