Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.87% |
37 / 39 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
PPDStackElement_Hash | |
97.37% |
37 / 38 |
|
83.33% |
5 / 6 |
18 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getAccum | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addPart | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getCurrentPart | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFlags | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
breakSyntax | |
95.65% |
22 / 23 |
|
0.00% |
0 / 1 |
9 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | * @ingroup Parser |
20 | */ |
21 | |
22 | namespace MediaWiki\Parser; |
23 | |
24 | /** |
25 | * @ingroup Parser |
26 | * |
27 | * @property PPDPart_Hash[] $parts |
28 | * @property int $startPos |
29 | */ |
30 | // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps |
31 | class PPDStackElement_Hash { |
32 | /** |
33 | * @var string Opening character (\n for heading) |
34 | */ |
35 | public $open; |
36 | |
37 | /** |
38 | * @var string Matching closing character |
39 | */ |
40 | public $close; |
41 | |
42 | /** |
43 | * @var string Saved prefix that may affect later processing, |
44 | * e.g. to differentiate `-{{{{` and `{{{{` after later seeing `}}}`. |
45 | */ |
46 | public $savedPrefix = ''; |
47 | |
48 | /** |
49 | * @var int Start offset of this element in the source wikitext |
50 | */ |
51 | public $startPos; |
52 | |
53 | /** |
54 | * @var int Number of opening characters found (number of "=" for heading) |
55 | */ |
56 | public $count; |
57 | |
58 | /** |
59 | * @var PPDPart_Hash[] Array of PPDPart objects describing pipe-separated parts. |
60 | */ |
61 | public $parts; |
62 | |
63 | /** |
64 | * @var bool True if the open char appeared at the start of the input line. |
65 | * Not set for headings. |
66 | */ |
67 | public $lineStart; |
68 | |
69 | /** @var string */ |
70 | public $partClass = PPDPart_Hash::class; |
71 | |
72 | public function __construct( $data = [] ) { |
73 | $class = $this->partClass; |
74 | $this->parts = [ new $class ]; |
75 | |
76 | foreach ( $data as $name => $value ) { |
77 | $this->$name = $value; |
78 | } |
79 | } |
80 | |
81 | public function &getAccum() { |
82 | return $this->parts[count( $this->parts ) - 1]->out; |
83 | } |
84 | |
85 | public function addPart( $s = '' ) { |
86 | $class = $this->partClass; |
87 | $this->parts[] = new $class( $s ); |
88 | } |
89 | |
90 | /** |
91 | * @return PPDPart_Hash |
92 | */ |
93 | public function getCurrentPart() { |
94 | return $this->parts[count( $this->parts ) - 1]; |
95 | } |
96 | |
97 | /** |
98 | * @return array |
99 | */ |
100 | public function getFlags() { |
101 | $partCount = count( $this->parts ); |
102 | $findPipe = $this->open != "\n" && $this->open != '['; |
103 | return [ |
104 | 'findPipe' => $findPipe, |
105 | 'findEquals' => $findPipe && $partCount > 1 && !isset( $this->parts[$partCount - 1]->eqpos ), |
106 | 'inHeading' => $this->open == "\n", |
107 | ]; |
108 | } |
109 | |
110 | /** |
111 | * Get the accumulator that would result if the close is not found. |
112 | * |
113 | * @param int|false $openingCount |
114 | * @return array |
115 | */ |
116 | public function breakSyntax( $openingCount = false ) { |
117 | if ( $this->open == "\n" ) { |
118 | $accum = array_merge( [ $this->savedPrefix ], $this->parts[0]->out ); |
119 | } else { |
120 | if ( $openingCount === false ) { |
121 | $openingCount = $this->count; |
122 | } |
123 | $s = substr( $this->open, 0, -1 ); |
124 | $s .= str_repeat( |
125 | substr( $this->open, -1 ), |
126 | $openingCount - strlen( $s ) |
127 | ); |
128 | $accum = [ $this->savedPrefix . $s ]; |
129 | $lastIndex = 0; |
130 | $first = true; |
131 | foreach ( $this->parts as $part ) { |
132 | if ( $first ) { |
133 | $first = false; |
134 | } elseif ( is_string( $accum[$lastIndex] ) ) { |
135 | $accum[$lastIndex] .= '|'; |
136 | } else { |
137 | $accum[++$lastIndex] = '|'; |
138 | } |
139 | |
140 | foreach ( $part->out as $node ) { |
141 | if ( is_string( $node ) && is_string( $accum[$lastIndex] ) ) { |
142 | $accum[$lastIndex] .= $node; |
143 | } else { |
144 | $accum[++$lastIndex] = $node; |
145 | } |
146 | } |
147 | } |
148 | } |
149 | return $accum; |
150 | } |
151 | } |
152 | |
153 | /** @deprecated class alias since 1.43 */ |
154 | class_alias( PPDStackElement_Hash::class, 'PPDStackElement_Hash' ); |