ExtendedJsonLdItemFormatterTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 15

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 15
dl 0
loc 123
rs 9.1666
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B validProvider() 0 87 1
B getInstance() 0 26 1
1
<?php
2
3
namespace PPP\Wikidata\ValueFormatters;
4
5
use Doctrine\Common\Cache\ArrayCache;
6
use Mediawiki\Api\MediawikiApi;
7
use PPP\Wikidata\Cache\PerSiteLinkCache;
8
use PPP\Wikidata\ValueFormatters\JsonLd\Entity\JsonLdEntityFormatter;
9
use PPP\Wikidata\ValueFormatters\JsonLd\Entity\JsonLdItemFormatter;
10
use PPP\Wikidata\ValueFormatters\JsonLd\JsonLdFormatterTestBase;
11
use PPP\Wikidata\Wikipedia\MediawikiArticle;
12
use PPP\Wikidata\Wikipedia\MediawikiArticleImage;
13
use PPP\Wikidata\Wikipedia\MediawikiArticleProvider;
14
use ValueFormatters\FormatterOptions;
15
use ValueFormatters\ValueFormatter;
16
use Wikibase\DataModel\Entity\Item;
17
use Wikibase\DataModel\Entity\ItemId;
18
use Wikibase\DataModel\SiteLink;
19
use Wikibase\DataModel\SiteLinkList;
20
21
/**
22
 * @covers PPP\Wikidata\ValueFormatters\ExtendedJsonLdItemFormatter
23
 *
24
 * @licence AGPLv3+
25
 * @author Thomas Pellissier Tanon
26
 */
27
class ExtendedJsonLdItemFormatterTest extends JsonLdFormatterTestBase {
28
29
	/**
30
	 * @see JsonLdFormatterTestBase::validProvider
31
	 */
32
	public function validProvider() {
33
		$item = new Item(
34
			new ItemId('Q42'),
35
			null,
36
			new SiteLinkList(array(new SiteLink('enwiki', 'Douglas Adams')))
37
		);
38
39
		return array(
40
			array(
41
				$item,
42
				(object) array(
43
					'@type' => 'Thing',
44
					'@id' => 'http://www.wikidata.org/entity/Q42',
45
					'name' => 'Q42',
46
					'potentialAction' => array(
47
						(object) array(
48
							'@type' => 'ViewAction',
49
							'name' => array(
50
								(object) array('@value' => 'View on Wikidata', '@language' => 'en'),
51
								(object) array('@value' => 'Voir sur Wikidata', '@language' => 'fr')
52
							),
53
							'image' => '//upload.wikimedia.org/wikipedia/commons/f/ff/Wikidata-logo.svg',
54
							'target' => '//www.wikidata.org/entity/Q42'
55
						),
56
						(object) array(
57
							'@type' => 'ViewAction',
58
							'name' => array(
59
								(object) array('@value' => 'View on Wikipedia', '@language' => 'en'),
60
								(object) array('@value' => 'Voir sur Wikipédia', '@language' => 'fr')
61
							),
62
							'image' => '//upload.wikimedia.org/wikipedia/commons/thumb/8/80/Wikipedia-logo-v2.svg/64px-Wikipedia-logo-v2.svg.png',
63
							'target' => 'http://en.wikipedia.org/wiki/Douglas_Adams'
64
						)
65
					),
66
					'image' => (object) array(
67
						'@type' => 'ImageObject',
68
						'@id' => 'http://commons.wikimedia.org/wiki/Image:Douglas_adams_portrait_cropped.jpg',
69
						'contentUrl' => '//upload.wikimedia.org/wikipedia/commons/c/c0/Douglas_adams_portrait_cropped.jpg',
70
						'name' => 'Douglas adams portrait cropped.jpg',
71
						'width' => 100,
72
						'height' => 200
73
					),
74
					'@reverse' => (object) array(
75
						'about'=> (object) array(
76
							'@type' => 'Article',
77
							'@id'=> 'http://en.wikipedia.org/wiki/Douglas_Adams',
78
							'inLanguage'=> 'en',
79
							'headline'=> 'Fooo barr baz gaaaaaaa...',
80
							'author'=> (object) array(
81
								'@type'=> 'Organization',
82
								'@id' => 'http://www.wikidata.org/entity/Q52',
83
								'name' => 'Wikipedia'
84
							),
85
							'license'=> 'http://creativecommons.org/licenses/by-sa/3.0/'
86
						)
87
					)
88
				),
89
				new FormatterOptions(array(
90
					JsonLdEntityFormatter::OPT_ENTITY_BASE_URI => 'http://www.wikidata.org/entity/',
91
					ValueFormatter::OPT_LANG => 'en'
92
				))
93
			),
94
			array(
95
				$item,
96
				(object) array(
97
					'@type' => 'Thing',
98
					'@id' => 'http://www.wikidata.org/entity/Q42',
99
					'name' => 'Q42',
100
					'potentialAction' => array(
101
						(object) array(
102
							'@type' => 'ViewAction',
103
							'name' => array(
104
								(object) array('@value' => 'View on Wikidata', '@language' => 'en'),
105
								(object) array('@value' => 'Voir sur Wikidata', '@language' => 'fr')
106
							),
107
							'image' => '//upload.wikimedia.org/wikipedia/commons/f/ff/Wikidata-logo.svg',
108
							'target' => '//www.wikidata.org/entity/Q42'
109
						)
110
					)
111
				),
112
				new FormatterOptions(array(
113
					JsonLdEntityFormatter::OPT_ENTITY_BASE_URI => 'http://www.wikidata.org/entity/',
114
					ValueFormatter::OPT_LANG => 'ru'
115
				))
116
			)
117
		);
118
	}
119
120
	/**
121
	 * @see JsonLdFormatterTestBase::getInstance
122
	 */
123
	protected function getInstance(FormatterOptions $options = null) {
124
		$articleHeaderCache = new PerSiteLinkCache(new ArrayCache(), 'wphead');
125
		$articleHeaderCache->save(new MediawikiArticle(
126
			new SiteLink('enwiki', 'Douglas Adams'),
127
			'Fooo barr baz gaaaaaaa...',
128
			'en',
129
			'http://en.wikipedia.org/wiki/Douglas_Adams',
130
			new MediawikiArticleImage(
131
				'//upload.wikimedia.org/wikipedia/commons/c/c0/Douglas_adams_portrait_cropped.jpg',
132
				100,
133
				200,
134
				'Douglas adams portrait cropped.jpg'
135
			)
136
		));
137
138
		return new ExtendedJsonLdItemFormatter(
139
			new JsonLdItemFormatter(new JsonLdEntityFormatter($options), $options),
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 123 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...
Bug introduced by
It seems like $options defined by parameter $options on line 123 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...
140
			new MediawikiArticleProvider(
141
				array(
142
					'enwiki' => new MediawikiApi('http://example.org')
143
				),
144
				$articleHeaderCache
145
			),
146
			$options
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 123 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...
147
		);
148
	}
149
}
150