Test Failed
Push — master ( 4c6731...e27b5b )
by Sam
03:04 queued 17s
created

RailwayStation   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 23
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hasAdjacentStations() 0 2 1
A isAdjacentTo() 0 6 1
1
<?php
2
3
require_once __DIR__ . '/../vendor/autoload.php';
4
5
// Any PSR6 cache can be used.
6
$cache = new Stash\Pool( new \Stash\Driver\FileSystem() );
7
8
// @codingStandardsIgnoreStart
9
/**
10
 * We want to be able to easily retrieve information
11
 * about things that are instances of railway station (Q55488),
12
 * so we register a new class that defines its INSTANCE_OF constant.
13
 * @link https://www.wikidata.org/wiki/Wikidata:WikiProject_Railways
14
 */
15
class RailwayStation extends \Samwilson\SimpleWikidata\Item {
16
	// @codingStandardsIgnoreEnd
17
	/**
18
	 * Every subclass of Item should define its 'instance of' ID.
19
	 */
20
	const INSTANCE_OF = 'Q55488';
21
	/**
22
	 * It can then contain any type-specific methods that are required.
23
	 * @return RailwayStation[]
24
	 */
25
	public function hasAdjacentStations() {
26
		return $this->getPropertyOfTypeItem( 'P197' );
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getPropertyOfTypeItem('P197') returns the type Samwilson\SimpleWikidata\Properties\Item[] which is incompatible with the documented return type RailwayStation[].
Loading history...
27
	}
28
	/**
29
	 * Including queries.
30
	 * @return RailwayStation[]
31
	 */
32
	public function isAdjacentTo() {
33
		$sparql = "SELECT ?item WHERE { ?item wdt:P197 wd:" . $this->getId() . " }";
34
		$query = new \Samwilson\SimpleWikidata\Query( $sparql, $this->lang, $this->cache );
35
		// Each query result will also be a RailwayStation
36
		// (if they're correctly recorded as such on Wikidata).
37
		return $query->getItems();
38
	}
39
}
40
41
// Then the class must register itself.
42
RailwayStation::register();
43
44
// Then we can create items with the factory and use them.
45
/** @var RailwayStation $eustonStation */
46
$eustonStation = \Samwilson\SimpleWikidata\Item::factory( 'Q800751', 'en', $cache );
47
echo $eustonStation->getLabel()
48
	. " has " . count( $eustonStation->hasAdjacentStations() ) . " adjacent stations"
49
	. " and is adjacent to " . count( $eustonStation->isAdjacentTo() ) . " stations.\n";
50