Completed
Push — master ( 5da39b...da06c4 )
by Dmitry
12:57
created

ConjunctionConditionBuilder::build()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 3.0261
1
<?php
2
3
namespace yii\db\conditions;
4
5
use yii\db\ExpressionBuilderInterface;
6
use yii\db\ExpressionBuilderTrait;
7
use yii\db\ExpressionInterface;
8
9
/**
10
 * Class ConjunctionConditionBuilder builds objects of abstract class [[ConjunctionCondition]]
11
 *
12
 * @author Dmytro Naumenko <[email protected]>
13
 * @since 2.0.14
14
 */
15
class ConjunctionConditionBuilder implements ExpressionBuilderInterface
16
{
17
    use ExpressionBuilderTrait;
18
19
    /**
20
     * Method builds the raw SQL from the $expression that will not be additionally
21
     * escaped or quoted.
22
     *
23
     * @param ExpressionInterface|ConjunctionCondition $condition the expression to be built.
24
     * @param array $params the binding parameters.
25
     * @return string the raw SQL that will not be additionally escaped or quoted.
26
     */
27 227
    public function build(ExpressionInterface $condition, array &$params = [])
28
    {
29 227
        $parts = $this->buildExpressionsFrom($condition, $params);
30
31 227
        if (empty($parts)) {
32
            return '';
33
        }
34
35 227
        if (count($parts) === 1) {
36 92
            return reset($parts);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression reset($parts); of type string|false adds false to the return on line 36 which is incompatible with the return type declared by the interface yii\db\ExpressionBuilderInterface::build of type string. It seems like you forgot to handle an error condition.
Loading history...
37
        }
38
39 153
        return '(' . implode(") {$condition->getOperator()} (", $parts) . ')';
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface yii\db\ExpressionInterface as the method getOperator() does only exist in the following implementations of said interface: yii\db\conditions\AndCondition, yii\db\conditions\BetweenColumnsCondition, yii\db\conditions\BetweenCondition, yii\db\conditions\ConjunctionCondition, yii\db\conditions\ExistsCondition, yii\db\conditions\InCondition, yii\db\conditions\LikeCondition, yii\db\conditions\OrCondition, yii\db\conditions\SimpleCondition.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
40
    }
41
42
    /**
43
     * Builds expressions, that are stored in $condition
44
     *
45
     * @param ExpressionInterface|ConjunctionCondition $condition the expression to be built.
46
     * @param array $params the binding parameters.
47
     * @return string[]
48
     */
49 227
    private function buildExpressionsFrom(ExpressionInterface $condition, &$params = [])
50
    {
51 227
        $parts = [];
52 227
        foreach ($condition->getExpressions() as $condition) {
53 227
            if (is_array($condition)) {
54 202
                $condition = $this->queryBuilder->buildCondition($condition, $params);
55
            }
56 227
            if ($condition instanceof ExpressionInterface) {
57 9
                $condition = $this->queryBuilder->buildExpression($condition, $params);
58
            }
59 227
            if ($condition !== '') {
60 227
                $parts[] = $condition;
61
            }
62
        }
63
64 227
        return $parts;
65
    }
66
}
67