Test Failed
Push — master ( b17eba...69b436 )
by Kirill
06:37
created

WhereBuilder   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 0
cbo 6
dl 0
loc 125
ccs 0
cts 89
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 12 2
D getDoctrineExpression() 0 98 16
1
<?php
2
/**
3
 * This file is part of Hydrogen package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace RDS\Hydrogen\Processor\DatabaseProcessor;
11
12
use Doctrine\ORM\Query\Expr;
13
use Doctrine\ORM\QueryBuilder;
14
use RDS\Hydrogen\Criteria\Common\Field;
15
use RDS\Hydrogen\Criteria\CriterionInterface;
16
use RDS\Hydrogen\Criteria\Where;
17
use RDS\Hydrogen\Criteria\Where\Operator;
18
19
/**
20
 * Class WhereBuilder
21
 */
22
class WhereBuilder extends Builder
23
{
24
    /**
25
     * @param QueryBuilder $builder
26
     * @param CriterionInterface|Where $where
27
     * @return \Generator
28
     */
29
    public function apply($builder, CriterionInterface $where): \Generator
30
    {
31
        $expression = $this->getDoctrineExpression($where, $builder->expr(), $where->getField());
0 ignored issues
show
Compatibility introduced by
$where of type object<RDS\Hydrogen\Criteria\CriterionInterface> is not a sub-type of object<RDS\Hydrogen\Criteria\Where>. It seems like you assume a concrete implementation of the interface RDS\Hydrogen\Criteria\CriterionInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
32
33
        yield from $this->extractResult($expression, function ($expr) use ($where, $builder) {
34
            if ($where->isAnd()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface RDS\Hydrogen\Criteria\CriterionInterface as the method isAnd() does only exist in the following implementations of said interface: RDS\Hydrogen\Criteria\Group, RDS\Hydrogen\Criteria\Where.

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...
35
                $builder->andWhere($expr);
36
            } else {
37
                $builder->orWhere($expr);
38
            }
39
        });
40
    }
41
42
    /**
43
     * @param Where $where
44
     * @param Expr $expr
45
     * @param Field $field
46
     * @return \Generator
47
     */
48
    protected function getDoctrineExpression(Where $where, Expr $expr, Field $field): \Generator
49
    {
50
        $operator = $where->getOperator()->toString();
51
52
        /**
53
         * Expr:
54
         * - "X IS NULL"
55
         * - "X IS NOT NULL"
56
         */
57
        if ($where->getValue() === null) {
58
            switch ($operator) {
59
                case Operator::EQ:
60
                    return $expr->isNull(yield $field);
61
62
                case Operator::NEQ:
63
                    return $expr->isNull(yield $field);
64
            }
65
        }
66
67
        switch ($operator) {
68
            case Operator::EQ:
69
                return $expr->eq(
70
                    yield $field,
71
                    yield $field => $where->getValue()
72
                );
73
74
            case Operator::NEQ:
75
                return $expr->neq(
76
                    yield $field,
77
                    yield $field => $where->getValue()
78
                );
79
80
            case Operator::GT:
81
                return $expr->gt(
82
                    yield $field,
83
                    yield $field => $where->getValue()
84
                );
85
86
            case Operator::LT:
87
                return $expr->lt(
88
                    yield $field,
89
                    yield $field => $where->getValue()
90
                );
91
92
            case Operator::GTE:
93
                return $expr->gte(
94
                    yield $field,
95
                    yield $field => $where->getValue()
96
                );
97
98
            case Operator::LTE:
99
                return $expr->lte(
100
                    yield $field,
101
                    yield $field => $where->getValue()
102
                );
103
104
            case Operator::IN:
105
                return $expr->in(
106
                    yield $field,
107
                    yield $field => $where->getValue()
108
                );
109
110
            case Operator::NOT_IN:
111
                return $expr->notIn(
112
                    yield $field,
113
                    yield $field => $where->getValue()
114
                );
115
116
            case Operator::LIKE:
117
                return $expr->like(
118
                    yield $field,
119
                    yield $field => $where->getValue()
120
                );
121
122
            case Operator::NOT_LIKE:
123
                return $expr->notLike(
124
                    yield $field,
125
                    yield $field => $where->getValue()
126
                );
127
128
            case Operator::BTW:
129
                return $expr->between(
130
                    yield $field,
131
                    yield $field => $where->getValue()[0] ?? null,
132
                    yield $field => $where->getValue()[1] ?? null
133
                );
134
135
            case Operator::NOT_BTW:
136
                return \vsprintf('%s NOT BETWEEN %s AND %s', [
137
                    yield $field,
138
                    yield $field => $where->getValue()[0] ?? null,
139
                    yield $field => $where->getValue()[1] ?? null,
140
                ]);
141
        }
142
143
        $error = \sprintf('Unexpected "%s" operator type', $operator);
144
        throw new \InvalidArgumentException($error);
145
    }
146
}
147