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

WhereProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 6
dl 0
loc 92
ccs 21
cts 24
cp 0.875
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A or() 0 10 2
A and() 0 10 2
A orWhere() 0 4 1
A where() 0 18 3
A mode() 0 6 1
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\Query;
11
12
use RDS\Hydrogen\Criteria\Group;
13
use RDS\Hydrogen\Criteria\Where;
14
use RDS\Hydrogen\Query;
15
use RDS\Hydrogen\Query\WhereProvider\WhereBetweenProvider;
16
use RDS\Hydrogen\Query\WhereProvider\WhereInProvider;
17
use RDS\Hydrogen\Query\WhereProvider\WhereLikeProvider;
18
use RDS\Hydrogen\Query\WhereProvider\WhereNullProvider;
19
20
/**
21
 * Trait WhereProvider
22
 * @property-read Query|$this $or
23
 * @property-read Query|$this $and
24
 * @mixin Query
25
 */
26
trait WhereProvider
27
{
28
    use WhereInProvider;
29
    use WhereLikeProvider;
30
    use WhereNullProvider;
31
    use WhereBetweenProvider;
32
33
    /**
34
     * - AND if true
35
     * - OR if false
36
     *
37
     * @var bool
38
     */
39
    protected $conjunction = true;
40
41
    /**
42
     * @param \Closure|null $group
43
     * @return Query|$this|self
44
     */
45 17
    public function or(\Closure $group = null): self
46
    {
47 17
        $this->conjunction = false;
48
49 17
        if ($group !== null) {
50 1
            $this->add(new Group($this, $group, $this->mode()));
0 ignored issues
show
Bug introduced by
It seems like add() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
51
        }
52
53 17
        return $this;
54
    }
55
56
    /**
57
     * @param \Closure|null $group
58
     * @return Query|$this|self
59
     */
60 1
    public function and(\Closure $group = null): self
61
    {
62 1
        $this->conjunction = true;
63
64 1
        if ($group !== null) {
65 1
            $this->add(new Group($this, $group, $this->mode()));
0 ignored issues
show
Bug introduced by
It seems like add() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
66
        }
67
68 1
        return $this;
69
    }
70
71
    /**
72
     * @param string|\Closure $field
73
     * @param $valueOrOperator
74
     * @param null $value
75
     * @return Query|$this|self
76
     */
77 11
    public function orWhere($field, $valueOrOperator = null, $value = null): self
78
    {
79 11
        return $this->or->where($field, $valueOrOperator, $value);
80
    }
81
82
    /**
83
     * @param string|\Closure $field
84
     * @param $valueOrOperator
85
     * @param null $value
86
     * @return Query|$this|self
87
     */
88 25
    public function where($field, $valueOrOperator = null, $value = null): self
89
    {
90 25
        if (\is_string($field)) {
91 25
            [$operator, $value] = Where::completeMissingParameters($valueOrOperator, $value);
0 ignored issues
show
Bug introduced by
The variable $operator does not exist. Did you mean $valueOrOperator?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
92
93 25
            return $this->add(new Where($field, $operator, $value, $this->mode()));
0 ignored issues
show
Bug introduced by
The variable $operator does not exist. Did you mean $valueOrOperator?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
It seems like add() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
94
        }
95
96 1
        if ($field instanceof \Closure) {
97 1
            return $this->add(new Group($this, $field, $this->mode()));
0 ignored issues
show
Bug introduced by
It seems like add() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
98
        }
99
100
        $error = \vsprintf('Selection set should be a type of string or Closure, but %s given', [
101
            \studly_case(\gettype($field)),
102
        ]);
103
104
        throw new \InvalidArgumentException($error);
105
    }
106
107
108
    /**
109
     * @return bool
110
     */
111
    protected function mode(): bool
112
    {
113 35
        return \tap($this->conjunction, function () {
114 35
            $this->conjunction = true;
115 35
        });
116
    }
117
}
118