QueryBuilder   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 33
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildCondition() 0 7 2
A build() 0 8 2
1
<?php
2
/**
3
 *
4
 * @author Mihkel Viilveer <[email protected]>
5
 * @date 27.08.2014
6
 */
7
8
namespace opus\elastic\components;
9
10
use Elastica\Param;
11
use yii\elasticsearch\Query;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, opus\elastic\components\Query.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
13
/**
14
 * Class QueryBuilder
15
 *
16
 * @author Mihkel Viilveer <[email protected]>
17
 * @package opus\elastic\components
18
 */
19
class QueryBuilder extends \yii\elasticsearch\QueryBuilder
20
{
21
    /**
22
     * Parses the condition specification and generates the corresponding SQL expression.
23
     *
24
     * @param string|array $condition the condition specification. Please refer to [[Query::where()]] on how to specify a condition.
25
     * @throws \yii\base\InvalidParamException if unknown operator is used in query
26
     * @throws \yii\base\NotSupportedException if string conditions are used in where
27
     * @return string the generated SQL expression
28
     */
29
    public function buildCondition($condition)
30
    {
31
        if ($condition instanceof Param) {
32
            return $condition->toArray();
33
        }
34
        return parent::buildCondition($condition);
35
    }
36
37
    /**
38
     * Generates query from a [[Query]] object.
39
     * @param Query $query the [[Query]] object from which the query will be generated
40
     * @return array the generated SQL statement (the first array element) and the corresponding
41
     * parameters to be bound to the SQL statement (the second array element).
42
     */
43
    public function build($query)
44
    {
45
        if ($query->query instanceof Param) {
46
            $query->query = $query->query->toArray();
47
        }
48
        return parent::build($query);
49
50
    }
51
}
52