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