Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.37% covered (success)
97.37%
37 / 38
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
PPDStackElement_Hash
97.37% covered (success)
97.37%
37 / 38
83.33% covered (warning)
83.33%
5 / 6
18
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getAccum
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addPart
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getCurrentPart
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFlags
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 breakSyntax
95.65% covered (success)
95.65%
22 / 23
0.00% covered (danger)
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/**
23 * @ingroup Parser
24 *
25 * @property PPDPart_Hash[] $parts
26 * @property int $startPos
27 */
28// phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
29class PPDStackElement_Hash {
30    /**
31     * @var string Opening character (\n for heading)
32     */
33    public $open;
34
35    /**
36     * @var string Matching closing character
37     */
38    public $close;
39
40    /**
41     * @var string Saved prefix that may affect later processing,
42     *  e.g. to differentiate `-{{{{` and `{{{{` after later seeing `}}}`.
43     */
44    public $savedPrefix = '';
45
46    /**
47     * @var int Start offset of this element in the source wikitext
48     */
49    public $startPos;
50
51    /**
52     * @var int Number of opening characters found (number of "=" for heading)
53     */
54    public $count;
55
56    /**
57     * @var PPDPart_Hash[] Array of PPDPart objects describing pipe-separated parts.
58     */
59    public $parts;
60
61    /**
62     * @var bool True if the open char appeared at the start of the input line.
63     *  Not set for headings.
64     */
65    public $lineStart;
66
67    /** @var string */
68    public $partClass = PPDPart_Hash::class;
69
70    public function __construct( $data = [] ) {
71        $class = $this->partClass;
72        $this->parts = [ new $class ];
73
74        foreach ( $data as $name => $value ) {
75            $this->$name = $value;
76        }
77    }
78
79    public function &getAccum() {
80        return $this->parts[count( $this->parts ) - 1]->out;
81    }
82
83    public function addPart( $s = '' ) {
84        $class = $this->partClass;
85        $this->parts[] = new $class( $s );
86    }
87
88    /**
89     * @return PPDPart_Hash
90     */
91    public function getCurrentPart() {
92        return $this->parts[count( $this->parts ) - 1];
93    }
94
95    /**
96     * @return array
97     */
98    public function getFlags() {
99        $partCount = count( $this->parts );
100        $findPipe = $this->open != "\n" && $this->open != '[';
101        return [
102            'findPipe' => $findPipe,
103            'findEquals' => $findPipe && $partCount > 1 && !isset( $this->parts[$partCount - 1]->eqpos ),
104            'inHeading' => $this->open == "\n",
105        ];
106    }
107
108    /**
109     * Get the accumulator that would result if the close is not found.
110     *
111     * @param int|false $openingCount
112     * @return array
113     */
114    public function breakSyntax( $openingCount = false ) {
115        if ( $this->open == "\n" ) {
116            $accum = array_merge( [ $this->savedPrefix ], $this->parts[0]->out );
117        } else {
118            if ( $openingCount === false ) {
119                $openingCount = $this->count;
120            }
121            $s = substr( $this->open, 0, -1 );
122            $s .= str_repeat(
123                substr( $this->open, -1 ),
124                $openingCount - strlen( $s )
125            );
126            $accum = [ $this->savedPrefix . $s ];
127            $lastIndex = 0;
128            $first = true;
129            foreach ( $this->parts as $part ) {
130                if ( $first ) {
131                    $first = false;
132                } elseif ( is_string( $accum[$lastIndex] ) ) {
133                    $accum[$lastIndex] .= '|';
134                } else {
135                    $accum[++$lastIndex] = '|';
136                }
137
138                foreach ( $part->out as $node ) {
139                    if ( is_string( $node ) && is_string( $accum[$lastIndex] ) ) {
140                        $accum[$lastIndex] .= $node;
141                    } else {
142                        $accum[++$lastIndex] = $node;
143                    }
144                }
145            }
146        }
147        return $accum;
148    }
149}