Passed
Push — master ( 3a7c7a...fe0bb9 )
by Sam
03:04
created

Work::getSubtitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Samwilson\SimpleWikidata\Items;
4
5
use Psr\Cache\CacheItemPoolInterface;
6
use Samwilson\SimpleWikidata\Item;
7
use Samwilson\SimpleWikidata\Query;
8
9
class Work extends Item {
10
11
	const ITEM_WORK = 'Q386724';
12
	const PROP_SUBTITLE = 'P1680';
13
	const PROP_GENRE = 'P136';
14
	const PROP_SUBJECT = 'P921';
15
16
	/**
17
	 * @param string $lang ISO639 language code.
18
	 * @param CacheItemPoolInterface $cache The cache.
19
	 * @return array|Item[]
20
	 */
21
	public static function getBookTypes( $lang = 'en', $cache ) {
22
		$sparql = "SELECT ?item WHERE {
23
			?item wdt:P279 wd:Q571 .
24
			?item rdfs:label ?label .
25
			FILTER(LANG(?label) = '$lang') .
26
			} ORDER BY ?label ";
27
		$query = new Query( $sparql, $lang );
0 ignored issues
show
Bug introduced by
The call to Samwilson\SimpleWikidata\Query::__construct() has too few arguments starting with cache. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
		$query = /** @scrutinizer ignore-call */ new Query( $sparql, $lang );

This check compares calls to functions or methods with their respective definitions. If the call has less 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. Please note the @ignore annotation hint above.

Loading history...
28
		$query->setCache( $cache );
0 ignored issues
show
Bug introduced by
The method setCache() does not exist on Samwilson\SimpleWikidata\Query. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
		$query->/** @scrutinizer ignore-call */ setCache( $cache );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
		$bookType = Item::factory( self::ITEM_WORK, $lang, $cache );
30
		return [ $bookType ] + $query->getItems();
31
	}
32
33
	/**
34
	 * @return bool|string
35
	 */
36
	public function getSubtitle() {
37
		return $this->getPropertyOfTypeText( self::PROP_SUBTITLE );
38
	}
39
40
	/**
41
	 * @param string $subtitle The new subtitle.
42
	 */
43
	public function setSubtitle( $subtitle ) {
44
		$this->setPropertyOfTypeText( self::PROP_SUBTITLE, $subtitle );
45
	}
46
47
	/**
48
	 * @param string $property A property identifier.
49
	 * @return array
50
	 */
51
	public function getPropertyOfTypeItems( $property ) {
52
		$entity = $this->getEntity( $this->id );
53
		if ( ! isset( $entity['claims'][ $property ] ) ) {
54
			return [];
55
		}
56
		$items = [];
57
		foreach ( $entity['claims'][ $property ] as $authorClaim ) {
58
			$item_id  = $authorClaim['mainsnak']['datavalue']['value']['id'];
59
			$items[] = Item::factory( $item_id, $this->lang, $this->cache );
60
		}
61
62
		return $items;
63
	}
64
65
	/**
66
	 * @return array
67
	 */
68
	public function getAuthors() {
69
		return $this->getPropertyOfTypeItems( self::PROP_AUTHOR );
70
	}
71
72
	/**
73
	 * @return Item[]
74
	 */
75
	public function getSubjects() {
76
		return $this->getPropertyOfTypeItems( self::PROP_SUBJECT );
77
	}
78
79
	/**
80
	 * @return Item[]
81
	 */
82
	public function getEditions() {
83
		$sparql = "SELECT ?item WHERE {"
84
			. " ?item wdt:" . self::PROP_EDITION_OR_TRANSLATION_OF . " wd:" . $this->getId()
0 ignored issues
show
Bug introduced by
The constant Samwilson\SimpleWikidata...ITION_OR_TRANSLATION_OF was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
85
			. "}";
86
		$query = new Query( $sparql, $this->lang );
0 ignored issues
show
Bug introduced by
The call to Samwilson\SimpleWikidata\Query::__construct() has too few arguments starting with cache. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
		$query = /** @scrutinizer ignore-call */ new Query( $sparql, $this->lang );

This check compares calls to functions or methods with their respective definitions. If the call has less 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. Please note the @ignore annotation hint above.

Loading history...
87
		$query->setCache( $this->cache );
88
		$editions = $query->getItems();
89
		usort( $editions, function ( Item $a, Item $b ) {
90
			if ( $a instanceof EditionItem and $b instanceof EditionItem ) {
0 ignored issues
show
Bug introduced by
The type Samwilson\SimpleWikidata\Items\EditionItem was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
91
				return $a->getPublicationYear() - $b->getPublicationYear();
92
			}
93
			return 0;
94
		} );
95
96
		return $editions;
97
	}
98
99
	public function newEdition() {
100
	}
101
102
}
103