EntityTermSearchHelper   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 116
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getRankedSearchResults() 0 29 2
A mergeSearchResults() 0 19 4
A searchEntities() 0 16 1
1
<?php
2
3
namespace Wikibase\Repo\Api;
4
5
use Wikibase\Lib\Interactors\ConfigurableTermSearchInteractor;
6
use Wikibase\Lib\Interactors\TermSearchOptions;
7
use Wikibase\Lib\Interactors\TermSearchResult;
8
use Wikibase\Lib\TermIndexEntry;
9
10
/**
11
 * Helper class to search for entities.
12
 *
13
 * @license GPL-2.0-or-later
14
 * @author Bene* < [email protected] >
15
 */
16
class EntityTermSearchHelper implements EntitySearchHelper {
17
18
	/**
19
	 * @var ConfigurableTermSearchInteractor
20
	 */
21
	private $termSearchInteractor;
22
23
	/**
24
	 * @param ConfigurableTermSearchInteractor $termSearchInteractor
25
	 */
26
	public function __construct(
27
		ConfigurableTermSearchInteractor $termSearchInteractor
28
	) {
29
		$this->termSearchInteractor = $termSearchInteractor;
30
	}
31
32
	/**
33
	 * Gets exact matches. If there are not enough exact matches, it gets prefixed matches.
34
	 *
35
	 * @param string $text
36
	 * @param string $languageCode
37
	 * @param string $entityType
38
	 * @param int $limit
39
	 * @param bool $strictLanguage
40
	 *
41
	 * @return TermSearchResult[] Key: string Serialized EntityId
42
	 */
43
	public function getRankedSearchResults( $text, $languageCode, $entityType, $limit, $strictLanguage ) {
44
		$allSearchResults = [];
45
46
		$exactSearchResults = $this->searchEntities(
47
			$text,
48
			$languageCode,
49
			$entityType,
50
			$limit,
51
			false,
52
			$strictLanguage
53
		);
54
		$allSearchResults = $this->mergeSearchResults( $allSearchResults, $exactSearchResults, $limit );
55
56
		// If still not enough matched then search for prefix matches
57
		$missing = $limit - count( $allSearchResults );
58
		if ( $missing > 0 ) {
59
			$prefixSearchResults = $this->searchEntities(
60
				$text,
61
				$languageCode,
62
				$entityType,
63
				$limit, // needs to be the full limit as exact matches are also contained in the prefix search
64
				true,
65
				$strictLanguage
66
			);
67
			$allSearchResults = $this->mergeSearchResults( $allSearchResults, $prefixSearchResults, $limit );
68
		}
69
70
		return $allSearchResults;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $allSearchResults; (array<*,Wikibase\Lib\Int...ctors\TermSearchResult>) is incompatible with the return type declared by the interface Wikibase\Repo\Api\Entity...:getRankedSearchResults of type Wikibase\Lib\Interactors\TermSearchResult[].

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...
71
	}
72
73
	/**
74
	 * @param TermSearchResult[] $searchResults
75
	 * @param TermSearchResult[] $newSearchResults
76
	 * @param int $limit
77
	 *
78
	 * @return TermSearchResult[]
79
	 */
80
	private function mergeSearchResults( array $searchResults, array $newSearchResults, $limit ) {
81
		$searchResultEntityIdSerializations = array_keys( $searchResults );
82
83
		foreach ( $newSearchResults as $searchResultToAdd ) {
84
			$entityIdString = $searchResultToAdd->getEntityId()->getSerialization();
85
86
			if ( !in_array( $entityIdString, $searchResultEntityIdSerializations ) ) {
87
				$searchResults[$entityIdString] = $searchResultToAdd;
88
				$searchResultEntityIdSerializations[] = $entityIdString;
89
				$missing = $limit - count( $searchResults );
90
91
				if ( $missing <= 0 ) {
92
					return $searchResults;
93
				}
94
			}
95
		}
96
97
		return $searchResults;
98
	}
99
100
	/**
101
	 * Wrapper around TermSearchInteractor::searchForEntities
102
	 *
103
	 * @see TermSearchInteractor::searchForEntities
104
	 *
105
	 * @param string $text
106
	 * @param string $languageCode
107
	 * @param string $entityType
108
	 * @param int $limit
109
	 * @param bool $prefixSearch
110
	 * @param bool $strictLanguage
111
	 *
112
	 * @return TermSearchResult[]
113
	 */
114
	private function searchEntities( $text, $languageCode, $entityType, $limit, $prefixSearch, $strictLanguage ) {
115
		$searchOptions = new TermSearchOptions();
116
		$searchOptions->setLimit( $limit );
117
		$searchOptions->setIsPrefixSearch( $prefixSearch );
118
		$searchOptions->setIsCaseSensitive( false );
119
		$searchOptions->setUseLanguageFallback( !$strictLanguage );
120
121
		$this->termSearchInteractor->setTermSearchOptions( $searchOptions );
122
123
		return $this->termSearchInteractor->searchForEntities(
124
			$text,
125
			$languageCode,
126
			$entityType,
127
			[ TermIndexEntry::TYPE_LABEL, TermIndexEntry::TYPE_ALIAS ]
128
		);
129
	}
130
131
}
132