Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.74% covered (success)
94.74%
36 / 38
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
InsertableRegexValidator
94.74% covered (success)
94.74%
36 / 38
50.00% covered (danger)
50.00%
1 / 2
7.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
4.25
 getIssues
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Validation\Validators;
5
6use InvalidArgumentException;
7use MediaWiki\Extension\Translate\MessageLoading\Message;
8use MediaWiki\Extension\Translate\TranslatorInterface\Insertable\RegexInsertablesSuggester;
9use MediaWiki\Extension\Translate\Validation\MessageValidator;
10use MediaWiki\Extension\Translate\Validation\ValidationIssue;
11use MediaWiki\Extension\Translate\Validation\ValidationIssues;
12
13/**
14 * A generic regex validator and insertable that can be reused by other classes.
15 * @author Abijeet Patro
16 * @license GPL-2.0-or-later
17 * @since 2019.06
18 */
19class InsertableRegexValidator extends RegexInsertablesSuggester implements MessageValidator {
20    /** @var string */
21    private $validationRegex;
22
23    public function __construct( $params ) {
24        parent::__construct( $params );
25
26        if ( is_string( $params ) ) {
27            $this->validationRegex = $params;
28        } elseif ( is_array( $params ) ) {
29            $this->validationRegex = $params['regex'] ?? null;
30        }
31
32        if ( $this->validationRegex === null ) {
33            throw new InvalidArgumentException( 'The configuration for InsertableRegexValidator does not ' .
34                'specify a regular expression.' );
35        }
36    }
37
38    public function getIssues( Message $message, string $targetLanguage ): ValidationIssues {
39        $issues = new ValidationIssues();
40
41        preg_match_all( $this->validationRegex, $message->definition(), $definitionMatch );
42        preg_match_all( $this->validationRegex, $message->translation(), $translationMatch );
43        $definitionVariables = $definitionMatch[0];
44        $translationVariables = $translationMatch[0];
45
46        $missingVariables = array_diff( $definitionVariables, $translationVariables );
47        if ( $missingVariables ) {
48            $issue = new ValidationIssue(
49                'variable',
50                'missing',
51                'translate-checks-parameters',
52                [
53                    [ 'PLAIN-PARAMS', $missingVariables ],
54                    [ 'COUNT', count( $missingVariables ) ]
55                ]
56            );
57
58            $issues->add( $issue );
59        }
60
61        $unknownVariables = array_diff( $translationVariables, $definitionVariables );
62        if ( $unknownVariables ) {
63            $issue = new ValidationIssue(
64                'variable',
65                'unknown',
66                'translate-checks-parameters-unknown',
67                [
68                    [ 'PLAIN-PARAMS', $unknownVariables ],
69                    [ 'COUNT', count( $unknownVariables ) ]
70                ]
71            );
72
73            $issues->add( $issue );
74        }
75
76        return $issues;
77    }
78}