Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.45% covered (warning)
70.45%
31 / 44
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
NewlineValidator
70.45% covered (warning)
70.45%
31 / 44
66.67% covered (warning)
66.67%
4 / 6
14.12
0.00% covered (danger)
0.00%
0 / 1
 getIssues
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 getStartingNewLinesCount
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEndingNewLineCount
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 validateStartingNewline
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
3
 validateEndingNewline
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
 createIssues
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Validation\Validators;
5
6use MediaWiki\Extension\Translate\MessageLoading\Message;
7use MediaWiki\Extension\Translate\Validation\MessageValidator;
8use MediaWiki\Extension\Translate\Validation\ValidationIssue;
9use MediaWiki\Extension\Translate\Validation\ValidationIssues;
10
11/**
12 * Ensures that the translation has the same number of newlines as the source
13 * message at the beginning of the string.
14 * @author Abijeet Patro
15 * @license GPL-2.0-or-later
16 * @since 2019.09
17 */
18class NewlineValidator implements MessageValidator {
19    public function getIssues( Message $message, string $targetLanguage ): ValidationIssues {
20        $translation = $message->translation();
21        $definition = $message->definition();
22
23        $definitionStartNewline = $this->getStartingNewLinesCount( $definition );
24        $translationStartNewline = $this->getStartingNewLinesCount( $translation );
25
26        $failingChecks = $this->validateStartingNewline(
27            $definitionStartNewline, $translationStartNewline
28        );
29
30        return $this->createIssues( $failingChecks );
31    }
32
33    protected function getStartingNewLinesCount( string $str ): int {
34        return strspn( $str, "\n" );
35    }
36
37    protected function getEndingNewLineCount( string $str ): int {
38        return strspn( strrev( $str ), "\n" );
39    }
40
41    protected function validateStartingNewline(
42        int $definitionStartNewline,
43        int $translationStartNewline
44    ): array {
45        $failingChecks = [];
46        if ( $definitionStartNewline < $translationStartNewline ) {
47            // Extra whitespace at beginning
48            $failingChecks[] = [
49                'extra-start',
50                $translationStartNewline - $definitionStartNewline
51            ];
52        } elseif ( $definitionStartNewline > $translationStartNewline ) {
53            // Missing whitespace at beginning
54            $failingChecks[] = [
55                'missing-start',
56                $definitionStartNewline - $translationStartNewline
57            ];
58        }
59
60        return $failingChecks;
61    }
62
63    protected function validateEndingNewline(
64        int $definitionEndNewline,
65        int $translationEndNewline
66    ): array {
67        $failingChecks = [];
68        if ( $definitionEndNewline < $translationEndNewline ) {
69            // Extra whitespace at end
70            $failingChecks[] = [
71                'extra-end',
72                $translationEndNewline - $definitionEndNewline
73            ];
74        } elseif ( $definitionEndNewline > $translationEndNewline ) {
75            // Missing whitespace at end
76            $failingChecks[] = [
77                'missing-end',
78                $definitionEndNewline - $translationEndNewline
79            ];
80        }
81
82        return $failingChecks;
83    }
84
85    protected function createIssues( array $failingChecks ): ValidationIssues {
86        $issues = new ValidationIssues();
87        foreach ( $failingChecks as [ $subType, $count ] ) {
88            $issue = new ValidationIssue(
89                'newline',
90                $subType,
91                "translate-checks-newline-$subType",
92                [ 'COUNT', $count ]
93            );
94
95            $issues->add( $issue );
96        }
97
98        return $issues;
99    }
100}