1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Message; |
4
|
|
|
|
5
|
|
|
use Deserializers\Deserializer; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Wikimedia\Assert\Assert; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A deserializer for {@link ViolationMessage}s. |
11
|
|
|
* |
12
|
|
|
* @license GNU GPL v2+ |
13
|
|
|
*/ |
14
|
|
|
class ViolationMessageDeserializer implements Deserializer { |
15
|
|
|
|
16
|
|
|
public function unabbreviateViolationMessageKey( $messageKeySuffix ) { |
17
|
|
|
return ViolationMessage::MESSAGE_KEY_PREFIX . $messageKeySuffix; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param array $serialization |
22
|
|
|
* @return ViolationMessage |
23
|
|
|
*/ |
24
|
|
|
public function deserialize( $serialization ) { |
25
|
|
|
Assert::parameterType( 'array', $serialization, '$serialization' ); |
26
|
|
|
|
27
|
|
|
$message = new ViolationMessage( |
28
|
|
|
$this->unabbreviateViolationMessageKey( $serialization['k'] ) |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
foreach ( $serialization['a'] as $serializedArgument ) { |
32
|
|
|
$message = $this->deserializeArgument( $message, $serializedArgument ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $message; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param ViolationMessage $message |
40
|
|
|
* @param array $serializedArgument [ 't' => ViolationMessage::TYPE_*, 'v' => serialized value, |
41
|
|
|
* 'r' => $role, (optional) 'a' => $alternativeMessageKey ] |
42
|
|
|
* @return ViolationMessage $message with the deserialized argument appended |
43
|
|
|
*/ |
44
|
|
|
private function deserializeArgument( ViolationMessage $message, array $serializedArgument ) { |
45
|
|
|
$methods = [ |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
$type = $serializedArgument['t']; |
49
|
|
|
$serializedValue = $serializedArgument['v']; |
50
|
|
|
$role = $serializedArgument['r']; |
51
|
|
|
if ( array_key_exists( 'a', $serializedArgument ) ) { |
52
|
|
|
$alternativeMessageKey = $this->unabbreviateViolationMessageKey( |
53
|
|
|
$serializedArgument['a'] |
54
|
|
|
); |
55
|
|
|
} else { |
56
|
|
|
$alternativeMessageKey = null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
if ( array_key_exists( $type, $methods ) ) { |
|
|
|
|
60
|
|
|
$method = $methods[$type]; |
61
|
|
|
$value = $this->$method( $serializedValue ); |
62
|
|
|
} else { |
63
|
|
|
throw new InvalidArgumentException( |
64
|
|
|
'Unknown ViolationMessage argument type ' . $type . '!' |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $message->withArgument( $type, $role, $value, $alternativeMessageKey ); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.