Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
TemplateDataNormalizer
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
4 / 4
15
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 normalize
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
8
 normalizeParameter
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
4
 normaliseInterfaceText
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace MediaWiki\Extension\TemplateData;
4
5use stdClass;
6
7/**
8 * @license GPL-2.0-or-later
9 */
10class TemplateDataNormalizer {
11
12    public const DEPRECATED_PARAMETER_TYPES = [
13        'string/line' => 'line',
14        'string/wiki-page-name' => 'wiki-page-name',
15        'string/wiki-user-name' => 'wiki-user-name',
16        'string/wiki-file-name' => 'wiki-file-name',
17    ];
18
19    private string $contentLanguageCode;
20
21    public function __construct( string $contentLanguageCode ) {
22        $this->contentLanguageCode = $contentLanguageCode;
23    }
24
25    /**
26     * @param stdClass $data Expected to be valid according to the {@see TemplateDataValidator}
27     */
28    public function normalize( stdClass $data ): void {
29        $data->description ??= null;
30        $data->sets ??= [];
31        $data->maps ??= (object)[];
32        $data->format ??= null;
33        $data->params ??= (object)[];
34
35        $this->normaliseInterfaceText( $data->description );
36        foreach ( $data->sets as $setObj ) {
37            $this->normaliseInterfaceText( $setObj->label );
38        }
39
40        foreach ( $data->params as $param ) {
41            if ( isset( $param->inherits ) && isset( $data->params->{ $param->inherits } ) ) {
42                $parent = $data->params->{ $param->inherits };
43                foreach ( $parent as $key => $value ) {
44                    if ( !isset( $param->$key ) ) {
45                        $param->$key = is_object( $parent->$key ) ?
46                            clone $parent->$key :
47                            $parent->$key;
48                    }
49                }
50                unset( $param->inherits );
51            }
52            $this->normalizeParameter( $param );
53        }
54    }
55
56    private function normalizeParameter( stdClass $paramObj ): void {
57        $paramObj->label ??= null;
58        $paramObj->description ??= null;
59        $paramObj->required ??= false;
60        $paramObj->suggested ??= false;
61        $paramObj->deprecated ??= false;
62        $paramObj->aliases ??= [];
63        $paramObj->type ??= 'unknown';
64        $paramObj->autovalue ??= null;
65        $paramObj->default ??= null;
66        $paramObj->suggestedvalues ??= [];
67        $paramObj->example ??= null;
68
69        $this->normaliseInterfaceText( $paramObj->label );
70        $this->normaliseInterfaceText( $paramObj->description );
71        $this->normaliseInterfaceText( $paramObj->default );
72        $this->normaliseInterfaceText( $paramObj->example );
73
74        foreach ( $paramObj->aliases as &$alias ) {
75            if ( is_int( $alias ) ) {
76                $alias = (string)$alias;
77            }
78        }
79
80        // Map deprecated types to newer versions
81        if ( isset( self::DEPRECATED_PARAMETER_TYPES[$paramObj->type] ) ) {
82            $paramObj->type = self::DEPRECATED_PARAMETER_TYPES[$paramObj->type];
83        }
84    }
85
86    /**
87     * @param string|stdClass &$text
88     */
89    private function normaliseInterfaceText( &$text ): void {
90        if ( is_string( $text ) ) {
91            $text = (object)[ $this->contentLanguageCode => $text ];
92        }
93    }
94
95}