Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
HeaderParserBase | |
0.00% |
0 / 17 |
|
0.00% |
0 / 6 |
132 | |
0.00% |
0 / 1 |
setInput | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
consumeString | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
skipWhitespace | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
error | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
consumeFixedDigits | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
assertEnd | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Rest\HeaderParser; |
4 | |
5 | /** |
6 | * @internal |
7 | */ |
8 | class HeaderParserBase { |
9 | /** |
10 | * @var string The input string being processed |
11 | */ |
12 | protected $input; |
13 | |
14 | /** |
15 | * @var int The position within $input |
16 | */ |
17 | protected $pos; |
18 | |
19 | /** |
20 | * @var int The length of $input |
21 | */ |
22 | protected $inputLength; |
23 | |
24 | /** |
25 | * Set the input, and derived convenience properties |
26 | * |
27 | * @param string $input |
28 | */ |
29 | protected function setInput( $input ) { |
30 | $this->input = $input; |
31 | $this->pos = 0; |
32 | $this->inputLength = strlen( $input ); |
33 | } |
34 | |
35 | /** |
36 | * Consume a specified string, or throw an exception. |
37 | * |
38 | * @param string $s |
39 | * @throws HeaderParserError |
40 | */ |
41 | protected function consumeString( $s ) { |
42 | if ( $this->pos >= $this->inputLength ) { |
43 | $this->error( "Expected \"$s\" but got end of string" ); |
44 | } |
45 | if ( substr_compare( $this->input, $s, $this->pos, strlen( $s ) ) === 0 ) { |
46 | $this->pos += strlen( $s ); |
47 | } else { |
48 | // (T350852) Give full context for error |
49 | $this->error( "Expected \"$s\" to be a substring of \"$this->input\" from position \"$this->pos\"" ); |
50 | } |
51 | } |
52 | |
53 | /** |
54 | * Skip whitespace at the input position (OWS) |
55 | */ |
56 | protected function skipWhitespace() { |
57 | $this->pos += strspn( $this->input, " \t", $this->pos ); |
58 | } |
59 | |
60 | /** |
61 | * Throw an exception to indicate a parse error |
62 | * |
63 | * @param string $message |
64 | * @throws HeaderParserError |
65 | * @return never |
66 | */ |
67 | protected function error( $message ) { |
68 | throw new HeaderParserError( "$message at {$this->pos}" ); |
69 | } |
70 | |
71 | /** |
72 | * Consume a specified number of digits, or throw an exception |
73 | * |
74 | * @param int $numDigits |
75 | * @return string |
76 | * @throws HeaderParserError |
77 | */ |
78 | protected function consumeFixedDigits( $numDigits ) { |
79 | $digits = substr( $this->input, $this->pos, $numDigits ); |
80 | if ( strlen( $digits ) !== $numDigits || !preg_match( '/^[0-9]*$/', $digits ) ) { |
81 | $this->error( "expected $numDigits digits" ); |
82 | } |
83 | $this->pos += $numDigits; |
84 | return $digits; |
85 | } |
86 | |
87 | /** |
88 | * If the position is not at the end of the input string, raise an error, |
89 | * complaining of trailing characters. |
90 | * |
91 | * @throws HeaderParserError |
92 | */ |
93 | protected function assertEnd() { |
94 | if ( $this->pos !== $this->inputLength ) { |
95 | $this->error( "trailing characters" ); |
96 | } |
97 | } |
98 | } |