ExtendedJsonLdEntityFormatterTest   B
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 16

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 16
dl 0
loc 62
rs 8.4614
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validProvider() 0 22 1
A getQ42() 0 14 1
A getInstance() 0 15 1
1
<?php
2
3
namespace PPP\Wikidata\ValueFormatters\JsonLd\Entity;
4
5
use PPP\Wikidata\ValueFormatters\JsonLd\JsonLdFormatterTestBase;
6
use ValueFormatters\FormatterOptions;
7
use Wikibase\DataModel\Entity\EntityIdValue;
8
use Wikibase\DataModel\Entity\Item;
9
use Wikibase\DataModel\Entity\ItemId;
10
use Wikibase\DataModel\Entity\PropertyId;
11
use Wikibase\DataModel\Snak\PropertyValueSnak;
12
use Wikibase\DataModel\Statement\Statement;
13
use Wikibase\DataModel\Statement\StatementList;
14
use Wikibase\DataModel\Term\AliasGroup;
15
use Wikibase\DataModel\Term\AliasGroupList;
16
use Wikibase\DataModel\Term\Fingerprint;
17
use Wikibase\DataModel\Term\Term;
18
use Wikibase\DataModel\Term\TermList;
19
20
/**
21
 * @covers PPP\Wikidata\ValueFormatters\JsonLd\Entity\ExtendedJsonLdEntityFormatter
22
 *
23
 * @licence AGPLv3+
24
 * @author Thomas Pellissier Tanon
25
 */
26
class ExtendedJsonLdEntityFormatterTest extends JsonLdFormatterTestBase {
27
28
	/**
29
	 * @see JsonLdFormatterTestBase::validProvider
30
	 */
31
	public function validProvider() {
32
		return array(
33
			array(
34
				$this->getQ42(),
35
				(object) array(
36
					'@type' => 'Thing',
37
					'@id' => 'http://www.wikidata.org/entity/Q42',
38
					'name' => (object) array('@value' => 'Douglas Adams', '@language' => 'en'),
39
					'description' => (object) array('@value' => 'Author', '@language' => 'en'),
40
					'alternateName' => array(
41
						(object) array('@value' => '42', '@language' => 'en')
42
					),
43
					'gender' => array(
44
						(object) array('name' => 'foo')
45
					)
46
				),
47
				new FormatterOptions(array(
48
					JsonLdEntityFormatter::OPT_ENTITY_BASE_URI => 'http://www.wikidata.org/entity/'
49
				))
50
			),
51
		);
52
	}
53
54
	/**
55
	 * @see JsonLdFormatterTestBase::getInstance
56
	 */
57
	protected function getInstance(FormatterOptions $options = null) {
58
		$snakFormatterMock = $this->getMock('ValueFormatters\ValueFormatter');
59
		$snakFormatterMock->expects($this->any())
60
			->method('format')
61
			->with($this->equalTo(new PropertyValueSnak(new PropertyId('P21'), new EntityIdValue(new ItemId('Q1')))))
62
			->willReturn(array(
63
				'gender' => (object) array('name' => 'foo')
64
			));
65
66
		return new ExtendedJsonLdEntityFormatter(
67
			new JsonLdEntityFormatter($options),
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 57 can be null; however, PPP\Wikidata\ValueFormat...ormatter::__construct() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
68
			$snakFormatterMock,
69
			$options
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 57 can be null; however, PPP\Wikidata\ValueFormat...ormatter::__construct() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
70
		);
71
	}
72
73
	private function getQ42() {
74
		return new Item(
75
			new ItemId('Q42'),
76
			new Fingerprint(
77
				new TermList(array(new Term('en', 'Douglas Adams'), new Term('ru', 'Дуглас Адамс'))),
78
				new TermList(array(new Term('en', 'Author'))),
79
				new AliasGroupList(array(new AliasGroup('en', array('42'))))
80
			),
81
			null,
82
			new StatementList(array(
83
				new Statement(new PropertyValueSnak(new PropertyId('P21'), new EntityIdValue(new ItemId('Q1'))))
84
			))
85
		);
86
	}
87
}
88