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