Completed
Pull Request — master (#15643)
by Dmitry
10:34
created

SimpleConditionBuilder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 33
ccs 11
cts 12
cp 0.9167
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 20 4
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
use yii\db\Query;
9
10
/**
11
 * Class NotConditionBuilder builds objects of [[SimpleCondition]]
12
 *
13
 * @author Dmytro Naumenko <[email protected]>
14
 * @since 2.0.14
15
 */
16
class SimpleConditionBuilder implements ExpressionBuilderInterface
17
{
18
    use ExpressionBuilderTrait;
19
20
    /**
21
     * Method builds the raw SQL from the $expression that will not be additionally
22
     * escaped or quoted.
23
     *
24
     * @param ExpressionInterface|SimpleCondition $expression the expression to be built.
25
     * @param array $params the binding parameters.
26
     * @return string the raw SQL that will not be additionally escaped or quoted.
27
     */
28 84
    public function build(ExpressionInterface $expression, array &$params = [])
29
    {
30 84
        $operator = $expression->getOperator();
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...
31 84
        $column = $expression->getColumn();
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 getColumn() does only exist in the following implementations of said interface: yii\db\conditions\BetweenCondition, yii\db\conditions\InCondition, yii\db\conditions\LikeCondition, 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...
32 84
        $value = $expression->getValue();
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 getValue() does only exist in the following implementations of said interface: yii\db\ArrayExpression, yii\db\JsonExpression, yii\db\PdoValue, yii\db\conditions\BetweenColumnsCondition, yii\db\conditions\LikeCondition, 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...
33
34 84
        if (strpos($column, '(') === false) {
35 84
            $column = $this->queryBuilder->db->quoteColumnName($column);
36
        }
37
38 84
        if ($value === null) {
39
            return "$column $operator NULL";
40
        }
41 84
        if ($value instanceof ExpressionInterface) {
42 54
            return "$column $operator {$this->queryBuilder->buildExpression($value, $params)}";
43
        }
44
45 30
        $phName = $this->queryBuilder->bindParam($value, $params);
46 30
        return "$column $operator $phName";
47
    }
48
}
49