Completed
Push — master ( b5d4f7...73f810 )
by
unknown
05:22 queued 02:09
created

deserializeArgument()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 17

Duplication

Lines 8
Ratio 30.77 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 8
loc 26
rs 8.8571
cc 3
eloc 17
nc 4
nop 2
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 ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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 );
0 ignored issues
show
Unused Code introduced by
The call to ViolationMessage::withArgument() has too many arguments starting with $alternativeMessageKey.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
69
	}
70
71
}
72