OrderByBuilder   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 22
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 12 3
1
<?php
2
/**
3
 * @link https://github.com/vuongxuongminh/yii2-searchable
4
 * @copyright Copyright (c) 2019 Vuong Xuong Minh
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
namespace vxm\searchable\expression;
9
10
use yii\db\ExpressionBuilderInterface;
11
use yii\db\ExpressionBuilderTrait;
12
use yii\db\ExpressionInterface;
13
14
/**
15
 * Class OrderByBuilder for build the [[OrderBy]].
16
 *
17
 * @author Vuong Minh <[email protected]>
18
 * @since 1.0.0
19
 */
20
class OrderByBuilder implements ExpressionBuilderInterface
21
{
22
23
    use ExpressionBuilderTrait;
24
25
    /**
26
     * @param ExpressionInterface|OrderBy $expression
27
     * @inheritDoc
28
     */
29 4
    public function build(ExpressionInterface $expression, array &$params = [])
30
    {
31 4
        $orderBy = $expression->query->orderBy;
0 ignored issues
show
Bug introduced by
Accessing query on the interface yii\db\ExpressionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
32
33 4
        if ($orderBy[0] === $expression && count($orderBy) === 1) {
34
35 4
            return $this->queryBuilder->buildExpression($expression->getExpression());
36
        } else { // user choice
37
38 1
            return '(SELECT NULL)';
39
        }
40
    }
41
}
42