FakePrefetchingTermLookup   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 115
c 0
b 0
f 0
wmc 27
lcom 1
cbo 1
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A prefetchTerms() 0 9 3
A bufferFakeTermsForEntities() 0 13 5
A bufferNonAliasTerm() 0 3 1
A bufferAliasTerms() 0 5 1
A generateFakeTerm() 0 5 2
A getPrefetchedTerms() 0 13 4
A getPrefetchedTerm() 0 4 1
A getLabel() 0 3 1
A getLabels() 0 8 2
A getDescription() 0 3 1
A getDescriptions() 0 8 2
A getPrefetchedAliases() 0 12 4
1
<?php
2
3
namespace Wikibase\DataAccess\Tests;
4
5
use Wikibase\DataAccess\PrefetchingTermLookup;
6
use Wikibase\DataModel\Entity\EntityId;
7
use Wikibase\DataModel\Term\TermTypes;
8
9
/**
10
 * A PrefetchingTermLookup providing dummy TermLookup functionality, i.e. always returning a fake label/description,
11
 * and optional aliases, and also a Spy on TermBuffer
12
 * i.e. provides access to "prefetched" terms stored in the buffer after prefetchTerms method is called.
13
 *
14
 * @license GPL-2.0-or-later
15
 */
16
class FakePrefetchingTermLookup implements PrefetchingTermLookup {
17
18
	private $buffer;
19
20
	/**
21
	 * @param array $entityIds
22
	 * @param array|null $termTypes if null, defaults to labels and descriptions only
23
	 * @param array|null $languageCodes if null, defaults to de and en
24
	 */
25
	public function prefetchTerms( array $entityIds, array $termTypes, array $languageCodes ) {
26
		if ( $termTypes === null ) {
27
			$termTypes = [ TermTypes::TYPE_LABEL, TermTypes::TYPE_DESCRIPTION ];
28
		}
29
		if ( $languageCodes === null ) {
30
			$languageCodes = [ 'de', 'en' ];
31
		}
32
		$this->bufferFakeTermsForEntities( $entityIds, $termTypes, $languageCodes );
33
	}
34
35
	private function bufferFakeTermsForEntities( array $entityIds, array $termTypes, array $languageCodes ) {
36
		foreach ( $entityIds as $id ) {
37
			foreach ( $termTypes as $type ) {
38
				foreach ( $languageCodes as $lang ) {
39
					if ( $type !== TermTypes::TYPE_ALIAS ) {
40
						$this->bufferNonAliasTerm( $id, $type, $lang );
41
					} else {
42
						$this->bufferAliasTerms( $id, $type, $lang );
43
					}
44
				}
45
			}
46
		}
47
	}
48
49
	private function bufferNonAliasTerm( EntityId $id, $type, $lang ) {
50
		$this->buffer[$id->getSerialization()][$type][$lang] = $this->generateFakeTerm( $id, $type, $lang );
51
	}
52
53
	private function bufferAliasTerms( EntityId $id, $type, $lang ) {
54
		$this->buffer[$id->getSerialization()][$type][$lang] = [];
55
		$this->buffer[$id->getSerialization()][$type][$lang][] = $this->generateFakeTerm( $id, $type, $lang, 1 );
56
		$this->buffer[$id->getSerialization()][$type][$lang][] = $this->generateFakeTerm( $id, $type, $lang, 2 );
57
	}
58
59
	/**
60
	 * @param EntityId $id
61
	 * @param string $type
62
	 * @param string $lang
63
	 * @param int $count Used for aliases
64
	 * @return string
65
	 */
66
	private function generateFakeTerm( EntityId $id, $type, $lang, $count = 0 ) {
67
		$suffix = $count ? ' ' . $count : '';
68
69
		return $id->getSerialization() . ' ' . $lang . ' ' . $type . $suffix;
70
	}
71
72
	public function getPrefetchedTerms() {
73
		$terms = [];
74
75
		foreach ( $this->buffer as $entityTerms ) {
76
			foreach ( $entityTerms as $termsByLang ) {
77
				foreach ( $termsByLang as $term ) {
78
					$terms[] = $term;
79
				}
80
			}
81
		}
82
83
		return $terms;
84
	}
85
86
	public function getPrefetchedTerm( EntityId $entityId, $termType, $languageCode ) {
87
		$id = $entityId->getSerialization();
88
		return $this->buffer[$id][$termType][$languageCode] ?? null;
89
	}
90
91
	public function getLabel( EntityId $entityId, $languageCode ) {
92
		return $this->generateFakeTerm( $entityId, TermTypes::TYPE_LABEL, $languageCode );
93
	}
94
95
	public function getLabels( EntityId $entityId, array $languageCodes ) {
96
		$labels = [];
97
98
		foreach ( $languageCodes as $lang ) {
99
			$labels[$lang] = $this->generateFakeTerm( $entityId, TermTypes::TYPE_LABEL, $lang );
100
		}
101
		return $labels;
102
	}
103
104
	public function getDescription( EntityId $entityId, $languageCode ) {
105
		return $this->generateFakeTerm( $entityId, TermTypes::TYPE_DESCRIPTION, $languageCode );
106
	}
107
108
	public function getDescriptions( EntityId $entityId, array $languageCodes ) {
109
		$descriptions = [];
110
111
		foreach ( $languageCodes as $lang ) {
112
			$descriptions[$lang] = $this->generateFakeTerm( $entityId, TermTypes::TYPE_DESCRIPTION, $lang );
113
		}
114
		return $descriptions;
115
	}
116
117
	public function getPrefetchedAliases( EntityId $entityId, $languageCode ) {
118
		$id = $entityId->getSerialization();
119
		if ( array_key_exists( $id, $this->buffer ) ) {
120
			if ( array_key_exists( TermTypes::TYPE_ALIAS, $this->buffer[$id] ) ) {
121
				if ( array_key_exists( $languageCode, $this->buffer[$id][TermTypes::TYPE_ALIAS] ) ) {
122
					return $this->buffer[$id][TermTypes::TYPE_ALIAS][$languageCode];
123
				}
124
			}
125
		}
126
127
		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...
128
	}
129
130
}
131