Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
18.18% covered (danger)
18.18%
2 / 11
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
MessageParam
18.18% covered (danger)
18.18%
2 / 11
33.33% covered (danger)
33.33%
2 / 6
53.36
0.00% covered (danger)
0.00%
0 / 1
 getType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 dump
n/a
0 / 0
n/a
0 / 0
0
 isSameAs
n/a
0 / 0
n/a
0 / 0
0
 jsonClassHintFor
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 newFromJsonArray
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 hint
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __wakeup
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Wikimedia\Message;
4
5use Wikimedia\JsonCodec\Hint;
6use Wikimedia\JsonCodec\JsonCodecable;
7use Wikimedia\JsonCodec\JsonCodecableTrait;
8
9/**
10 * Value object representing a message parameter that consists of a list of values.
11 *
12 * Message parameter classes are pure value objects and are newable and (de)serializable.
13 */
14abstract class MessageParam implements JsonCodecable {
15    use JsonCodecableTrait;
16
17    // We can't use PHP type hint here without breaking deserialization of
18    // old MessageParams saved with PHP serialize().
19    /** @var ParamType */
20    protected $type;
21    /** @var mixed */
22    protected $value;
23
24    /**
25     * Get the type of the parameter.
26     *
27     * @return ParamType One of the ParamType constants
28     */
29    public function getType(): ParamType {
30        return $this->type;
31    }
32
33    /**
34     * Get the input value of the parameter
35     *
36     * @return mixed
37     */
38    public function getValue() {
39        return $this->value;
40    }
41
42    /**
43     * Dump the object for testing/debugging
44     *
45     * @return string
46     */
47    abstract public function dump(): string;
48
49    /**
50     * Equality testing.
51     */
52    abstract public function isSameAs( MessageParam $mp ): bool;
53
54    /** @inheritDoc */
55    public static function jsonClassHintFor( string $keyName ) {
56        // Support Hint::INHERITED
57        if ( $keyName === ParamType::LIST->value ) {
58            return ListParam::jsonClassHintFor( $keyName );
59        }
60        return ScalarParam::jsonClassHintFor( $keyName );
61    }
62
63    /** @inheritDoc */
64    public static function newFromJsonArray( array $json ): MessageParam {
65        // WARNING: When changing how this class is serialized, follow the instructions
66        // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>!
67        // Because of the use of Hint::INHERITED,
68        // MessageParam::newFromJsonArray() needs to know how to dispatch to
69        // an appropriate subclass constructor.
70        if ( isset( $json[ParamType::LIST->value] ) ) {
71            return ListParam::newFromJsonArray( $json );
72        }
73        return ScalarParam::newFromJsonArray( $json );
74    }
75
76    /**
77     * If you are serializing a MessageParam, use
78     * this JsonCodec hint to suppress unnecessary type information.
79     */
80    public static function hint(): Hint {
81        return Hint::build( self::class, Hint::INHERITED );
82    }
83
84    public function __wakeup(): void {
85        // Backward-compatibility for PHP serialization:
86        // Fixup $type after deserialization
87        if ( is_string( $this->type ) ) {
88            $this->type = ParamType::from( $this->type );
89        }
90    }
91}