deserializationProvider()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 117

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 117
rs 8
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Tests\Wikibase\DataModel\Deserializers;
4
5
use Deserializers\Deserializer;
6
use Wikibase\DataModel\Deserializers\PropertyDeserializer;
7
use Wikibase\DataModel\Entity\Property;
8
use Wikibase\DataModel\Entity\PropertyId;
9
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
10
use Wikibase\DataModel\Statement\Statement;
11
use Wikibase\DataModel\Statement\StatementList;
12
use Wikibase\DataModel\Term\AliasGroup;
13
use Wikibase\DataModel\Term\AliasGroupList;
14
use Wikibase\DataModel\Term\Term;
15
use Wikibase\DataModel\Term\TermList;
16
17
/**
18
 * @covers Wikibase\DataModel\Deserializers\PropertyDeserializer
19
 *
20
 * @license GPL-2.0-or-later
21
 * @author Thomas Pellissier Tanon
22
 * @author Bene* < [email protected] >
23
 */
24
class PropertyDeserializerTest extends DispatchableDeserializerTest {
25
26
	protected function buildDeserializer() {
27
		$entityIdDeserializerMock = $this->getMockBuilder( Deserializer::class )->getMock();
28
		$entityIdDeserializerMock->expects( $this->any() )
29
			->method( 'deserialize' )
30
			->with( $this->equalTo( 'P42' ) )
31
			->will( $this->returnValue( new PropertyId( 'P42' ) ) );
32
33
		$termListDeserializerMock = $this->getMockBuilder( Deserializer::class )->getMock();
34
		$termListDeserializerMock->expects( $this->any() )
35
			->method( 'deserialize' )
36
			->with( $this->equalTo( [
37
				'en' => [
38
					'lang' => 'en',
39
					'value' => 'foo'
40
				]
41
			] ) )
42
			->will( $this->returnValue( new TermList( [ new Term( 'en', 'foo' ) ] ) ) );
43
44
		$aliasGroupListDeserializerMock = $this->getMockBuilder( Deserializer::class )->getMock();
45
		$aliasGroupListDeserializerMock->expects( $this->any() )
46
			->method( 'deserialize' )
47
			->with( $this->equalTo( [
48
				'en' => [
49
					'lang' => 'en',
50
					'values' => [ 'foo', 'bar' ]
51
				]
52
			] ) )
53
			->will( $this->returnValue(
54
				new AliasGroupList( [ new AliasGroup( 'en', [ 'foo', 'bar' ] ) ] ) )
55
			);
56
57
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
58
		$statement->setGuid( 'test' );
59
60
		$statementListDeserializerMock = $this->getMockBuilder( Deserializer::class )->getMock();
61
		$statementListDeserializerMock->expects( $this->any() )
62
			->method( 'deserialize' )
63
			->with( $this->equalTo( [
64
				'P42' => [
65
					[
66
						'mainsnak' => [
67
							'snaktype' => 'novalue',
68
							'property' => 'P42'
69
						],
70
						'type' => 'statement',
71
						'rank' => 'normal'
72
					]
73
				]
74
			] ) )
75
			->will( $this->returnValue( new StatementList( [ $statement ] ) ) );
76
77
		return new PropertyDeserializer(
78
			$entityIdDeserializerMock,
0 ignored issues
show
Documentation introduced by
$entityIdDeserializerMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Deserializers\Deserializer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
			$termListDeserializerMock,
0 ignored issues
show
Documentation introduced by
$termListDeserializerMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Deserializers\Deserializer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
80
			$aliasGroupListDeserializerMock,
0 ignored issues
show
Documentation introduced by
$aliasGroupListDeserializerMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Deserializers\Deserializer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81
			$statementListDeserializerMock
0 ignored issues
show
Documentation introduced by
$statementListDeserializerMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Deserializers\Deserializer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
		);
83
	}
84
85
	public function deserializableProvider() {
86
		return [
87
			[
88
				[
89
					'type' => 'property'
90
				]
91
			],
92
		];
93
	}
94
95
	public function nonDeserializableProvider() {
96
		return [
97
			[
98
				5
99
			],
100
			[
101
				[]
102
			],
103
			[
104
				[
105
					'type' => 'item'
106
				]
107
			],
108
		];
109
	}
110
111
	public function deserializationProvider() {
112
		$property = Property::newFromType( 'string' );
113
114
		$provider = [
115
			[
116
				$property,
117
				[
118
					'type' => 'property',
119
					'datatype' => 'string'
120
				]
121
			],
122
		];
123
124
		$property = new Property( new PropertyId( 'P42' ), null, 'string' );
125
		$provider[] = [
126
			$property,
127
			[
128
				'type' => 'property',
129
				'datatype' => 'string',
130
				'id' => 'P42'
131
			]
132
		];
133
134
		$property = Property::newFromType( 'string' );
135
		$property->setLabel( 'en', 'foo' );
136
		$provider[] = [
137
			$property,
138
			[
139
				'type' => 'property',
140
				'datatype' => 'string',
141
				'labels' => [
142
					'en' => [
143
						'lang' => 'en',
144
						'value' => 'foo'
145
					]
146
				]
147
			]
148
		];
149
150
		$property = Property::newFromType( 'string' );
151
		$property->setDescription( 'en', 'foo' );
152
		$provider[] = [
153
			$property,
154
			[
155
				'type' => 'property',
156
				'datatype' => 'string',
157
				'descriptions' => [
158
					'en' => [
159
						'lang' => 'en',
160
						'value' => 'foo'
161
					]
162
				]
163
			]
164
		];
165
166
		$property = Property::newFromType( 'string' );
167
		$property->setAliases( 'en', [ 'foo', 'bar' ] );
168
		$provider[] = [
169
			$property,
170
			[
171
				'type' => 'property',
172
				'datatype' => 'string',
173
				'aliases' => [
174
					'en' => [
175
						'lang' => 'en',
176
						'values' => [ 'foo', 'bar' ]
177
					]
178
				]
179
			]
180
		];
181
182
		$property = Property::newFromType( 'string' );
183
		$property->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ), null, null, 'test' );
184
		$provider[] = [
185
			$property,
186
			[
187
				'type' => 'property',
188
				'datatype' => 'string',
189
				'claims' => [
190
					'P42' => [
191
						[
192
							'mainsnak' => [
193
								'snaktype' => 'novalue',
194
								'property' => 'P42'
195
							],
196
							'type' => 'statement',
197
							'rank' => 'normal'
198
						]
199
					]
200
				]
201
			]
202
		];
203
204
		$property = Property::newFromType( 'string' );
205
		$property->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ), null, null, 'test' );
206
		$provider[] = [
207
			$property,
208
			[
209
				'type' => 'property',
210
				'datatype' => 'string',
211
				'claims' => [
212
					'P42' => [
213
						[
214
							'mainsnak' => [
215
								'snaktype' => 'novalue',
216
								'property' => 'P42'
217
							],
218
							'type' => 'statement',
219
							'rank' => 'normal'
220
						]
221
					]
222
				]
223
			]
224
		];
225
226
		return $provider;
227
	}
228
229
}
230