NullPrefetchingTermLookup::getLabel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Wikibase\DataAccess;
4
5
use Wikibase\DataModel\Entity\EntityId;
6
7
/**
8
 * PrefetchingTermLookup implementation that does nothing.
9
 *
10
 * Intended to be used with some services that require a default PrefetchingTermLookup in
11
 * situations that a default does not make sense.
12
 *
13
 * @author Addshore
14
 *
15
 * @license GPL-2.0-or-later
16
 */
17
class NullPrefetchingTermLookup implements PrefetchingTermLookup {
18
19
	/**
20
	 * @inheritDoc
21
	 */
22
	public function prefetchTerms( array $entityIds, array $termTypes, array $languageCodes ) {
23
	}
24
25
	/**
26
	 * @inheritDoc
27
	 */
28
	public function getPrefetchedTerm( EntityId $entityId, $termType, $languageCode ) {
29
		return false;
30
	}
31
32
	/**
33
	 * @inheritDoc
34
	 */
35
	public function getLabel( EntityId $entityId, $languageCode ) {
36
		return null;
37
	}
38
39
	/**
40
	 * @inheritDoc
41
	 */
42
	public function getLabels( EntityId $entityId, array $languageCodes ) {
43
		return [];
44
	}
45
46
	/**
47
	 * @inheritDoc
48
	 */
49
	public function getDescription( EntityId $entityId, $languageCode ) {
50
		return null;
51
	}
52
53
	/**
54
	 * @inheritDoc
55
	 */
56
	public function getDescriptions( EntityId $entityId, array $languageCodes ) {
57
		return [];
58
	}
59
60
	/**
61
	 * @inheritDoc
62
	 */
63
	public function getPrefetchedAliases( EntityId $entityId, $languageCode ) {
64
		return [];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type declared by the interface Wikibase\DataAccess\Alia...r::getPrefetchedAliases of type string[]|false|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
65
	}
66
}
67