Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
GettextNewlineValidator
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 getIssues
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
 removeTrailingSlash
100.00% covered (success)
100.00%
3 / 3
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\ValidationIssues;
8
9/**
10 * Ensures that the translation has the same number of newlines as the source
11 * message at the beginning and end of the string. This works specifically
12 * for GettextFormat.
13 * @author Abijeet Patro
14 * @license GPL-2.0-or-later
15 * @since 2019.09
16 */
17class GettextNewlineValidator extends NewlineValidator {
18    public function getIssues( Message $message, string $targetLanguage ): ValidationIssues {
19        $translation = $message->translation();
20        $definition = $message->definition();
21
22        // ending newlines in GetText are bounded by a "\"
23        $definition = $this->removeTrailingSlash( $definition );
24        $translation = $this->removeTrailingSlash( $translation );
25
26        $definitionStartNewline = $this->getStartingNewLinesCount( $definition );
27        $definitionEndNewline = $this->getEndingNewLineCount( $definition );
28
29        $translationStartNewline = $this->getStartingNewLinesCount( $translation );
30        $translationEndNewline = $this->getEndingNewLineCount( $translation );
31
32        $failingChecks = array_merge(
33            $this->validateStartingNewline( $definitionStartNewline, $translationStartNewline ),
34            $this->validateEndingNewline( $definitionEndNewline, $translationEndNewline )
35        );
36
37        return $this->createIssues( $failingChecks );
38    }
39
40    private function removeTrailingSlash( string $str ): string {
41        if ( substr( $str, -strlen( '\\' ) ) === '\\' ) {
42            return substr( $str, 0, -1 );
43        }
44
45        return $str;
46    }
47}