Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
RadioRangeCommentBallot
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 4
182
0.00% covered (danger)
0.00%
0 / 1
 getForm
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 submitForm
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 unpackRecord
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
110
 readComment
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\SecurePoll\Ballots;
4
5use LogicException;
6
7/**
8 * A ballot used specifically for the Wikimedia Referendum on the personal image filter.
9 * Allows voters to send comments in with their ballot.
10 *
11 * Now deprecated in favour of the comment feature in VotePage.
12 */
13class RadioRangeCommentBallot extends RadioRangeBallot {
14    /** @inheritDoc */
15    public function getForm( $prevStatus = false ) {
16        // @phan-suppress-previous-line PhanPluginNeverReturnMethod
17        throw new LogicException( 'This ballot type has been archived and can no longer be used for voting.' );
18    }
19
20    /** @inheritDoc */
21    public function submitForm() {
22        // @phan-suppress-previous-line PhanPluginNeverReturnMethod
23        throw new LogicException( 'This ballot type has been archived and can no longer be used for voting.' );
24    }
25
26    /**
27     * Copy and modify from parent function, complex to refactor.
28     * @param string $record
29     * @return array|bool
30     */
31    public function unpackRecord( $record ) {
32        $scores = [];
33        $itemLength = 8 + 8 + 11 + 7;
34        $questions = [];
35        foreach ( $this->election->getQuestions() as $question ) {
36            $questions[$question->getId()] = $question;
37        }
38        for ( $offset = 0, $len = strlen( $record ); $offset < $len; $offset += $itemLength ) {
39            if ( !preg_match(
40                '/Q([0-9A-F]{8})-A([0-9A-F]{8})-S([+-][0-9]{10})--/A',
41                $record,
42                $m,
43                0,
44                $offset
45            )
46            ) {
47                // Allow comments
48                if ( $record[$offset] == '/' ) {
49                    break;
50                }
51
52                wfDebug( __METHOD__ . ": regex doesn't match\n" );
53
54                return false;
55            }
56            $qid = intval( base_convert( $m[1], 16, 10 ) );
57            $oid = intval( base_convert( $m[2], 16, 10 ) );
58            $score = intval( $m[3] );
59            if ( !isset( $questions[$qid] ) ) {
60                wfDebug( __METHOD__ . ": invalid question ID\n" );
61
62                return false;
63            }
64            [ $min, $max ] = $this->getMinMax( $questions[$qid] );
65            if ( $score < $min || $score > $max ) {
66                wfDebug( __METHOD__ . ": score out of range\n" );
67
68                return false;
69            }
70            $scores[$qid][$oid] = $score;
71        }
72
73        // Read comments
74        $scores['comment'] = [];
75
76        $scores['comment']['native'] = $this->readComment( $record, $offset );
77
78        if ( substr( $record, $offset, 2 ) !== '--' ) {
79            wfDebug( __METHOD__ . ": Invalid format\n" );
80
81            return false;
82        }
83        $offset += 2;
84
85        $scores['comment']['en'] = $this->readComment( $record, $offset );
86
87        if ( $offset < strlen( $record ) ) {
88            wfDebug( __METHOD__ . ": Invalid format\n" );
89
90            return false;
91        }
92
93        return $scores;
94    }
95
96    /**
97     * @param string $record
98     * @param int &$offset
99     * @return string
100     */
101    public function readComment( $record, &$offset ) {
102        $commentOffset = strpos( $record, '/', $offset + 1 );
103        $commentLength = intval(
104            substr(
105                $record,
106                $offset + 1,
107                ( $commentOffset - $offset ) - 1
108            )
109        );
110        $result = substr( $record, $commentOffset + 1, $commentLength );
111        // Fast-forward
112        $offset = $commentOffset + $commentLength + 1;
113
114        return $result;
115    }
116}