MongoDBEntityIdForTermLookup   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 4
c 7
b 0
f 0
lcom 1
cbo 5
dl 0
loc 49
ccs 22
cts 22
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getEntityIdsForTerm() 0 16 2
A buildGetEntityIdForTermQuery() 0 7 1
1
<?php
2
3
namespace Wikibase\EntityStore\MongoDB;
4
5
use Doctrine\MongoDB\Database;
6
use Doctrine\MongoDB\Query\Expr;
7
use Wikibase\DataModel\Term\Term;
8
use Wikibase\EntityStore\Internal\EntityIdForTermLookup;
9
10
/**
11
 * Internal class
12
 *
13
 * @licence GPLv2+
14
 * @author Thomas Pellissier Tanon
15
 */
16
class MongoDBEntityIdForTermLookup implements EntityIdForTermLookup {
17
18
	/**
19
	 * @var Database
20
	 */
21
	private $database;
22
23
	/**
24
	 * @var MongoDBDocumentBuilder
25
	 */
26
	private $documentBuilder;
27
28
	/**
29
	 * @param Database $database
30
	 * @param MongoDBDocumentBuilder $documentBuilder
31
	 */
32 2
	public function __construct( Database $database, MongoDBDocumentBuilder $documentBuilder ) {
33 2
		$this->database = $database;
34 2
		$this->documentBuilder = $documentBuilder;
35 2
	}
36
37
	/**
38
	 * @see EntityDocumentLookup::getEntityDocumentsForTerm
39
	 */
40 2
	public function getEntityIdsForTerm( Term $term, $entityType ) {
41 2
		$documents = $this->database
42 2
			->selectCollection( $entityType )
43 2
			->find(
44 2
				$this->buildGetEntityIdForTermQuery( $term ),
0 ignored issues
show
Documentation introduced by
$this->buildGetEntityIdForTermQuery($term) is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45 2
				[ '_id' => 1 ]
46 2
			);
47
48 2
		$entities = [];
49
50 2
		foreach( $documents as $document ) {
51 2
			$entities[] = $this->documentBuilder->buildEntityIdForDocument( $document );
52 2
		}
53
54 2
		return $entities;
55
	}
56
57 2
	private function buildGetEntityIdForTermQuery( Term $term ) {
58 2
		$expr = new Expr();
59 2
		$expr->field( 'sterms.' . $term->getLanguageCode() )->equals(
60 2
			$this->documentBuilder->cleanTextForSearch( $term->getText() )
61 2
		);
62 2
		return $expr->getQuery();
63
	}
64
}
65