SearchEngineFactory   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 18 4
B getSearchEngineClass() 0 16 6
1
<?php
2
3
/**
4
 * Factory class for SearchEngine.
5
 * Allows to create engine of the specific type.
6
 */
7
class SearchEngineFactory {
8
	/**
9
	 * Configuration for SearchEngine classes.
10
	 * @var SearchEngineConfig
11
	 */
12
	private $config;
13
14
	public function __construct( SearchEngineConfig $config ) {
15
		$this->config = $config;
16
	}
17
18
	/**
19
	 * Create SearchEngine of the given type.
20
	 * @param string $type
21
	 * @return SearchEngine
22
	 */
23
	public function create( $type = null ) {
24
		$dbr = null;
25
26
		$configType = $this->config->getSearchType();
27
		$alternatives = $this->config->getSearchTypes();
28
29
		if ( $type && in_array( $type, $alternatives ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $type of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
30
			$class = $type;
31
		} elseif ( $configType !== null ) {
32
			$class = $configType;
33
		} else {
34
			$dbr = wfGetDB( DB_REPLICA );
35
			$class = self::getSearchEngineClass( $dbr );
0 ignored issues
show
Bug introduced by
It seems like $dbr defined by wfGetDB(DB_REPLICA) on line 34 can be null; however, SearchEngineFactory::getSearchEngineClass() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
36
		}
37
38
		$search = new $class( $dbr );
39
		return $search;
40
	}
41
42
	/**
43
	 * @param IDatabase $db
44
	 * @return string SearchEngine subclass name
45
	 * @since 1.28
46
	 */
47
	public static function getSearchEngineClass( IDatabase $db ) {
48
		switch ( $db->getType() ) {
49
			case 'sqlite':
50
				return 'SearchSqlite';
51
			case 'mysql':
52
				return 'SearchMySQL';
53
			case 'postgres':
54
				return 'SearchPostgres';
55
			case 'mssql':
56
				return 'SearchMssql';
57
			case 'oracle':
58
				return 'SearchOracle';
59
			default:
60
				return 'SearchEngineDummy';
61
		}
62
	}
63
}
64