Completed
Push — master ( b7ef6a...58a002 )
by Kirill
03:12
created

WhereProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 72.72%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 43
ccs 8
cts 11
cp 0.7272
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A orWhere() 0 4 1
A where() 0 18 3
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\Where;
13
use RDS\Hydrogen\Criteria\WhereGroup;
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
 * @mixin Query
23
 */
24
trait WhereProvider
25
{
26
    use WhereInProvider;
27
    use WhereLikeProvider;
28
    use WhereNullProvider;
29
    use WhereBetweenProvider;
30
31
    /**
32
     * @param string|\Closure $field
33
     * @param $valueOrOperator
34
     * @param null $value
35
     * @return Query|$this|self
36
     */
37 13
    public function orWhere($field, $valueOrOperator = null, $value = null): self
38
    {
39 13
        return $this->or->where($field, $valueOrOperator, $value);
0 ignored issues
show
Bug introduced by
The property or does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40
    }
41
42
    /**
43
     * @param string|\Closure $field
44
     * @param $valueOrOperator
45
     * @param null $value
46
     * @return Query|$this|self
47
     */
48 41
    public function where($field, $valueOrOperator = null, $value = null): self
49
    {
50 41
        if (\is_string($field)) {
51 41
            [$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...
52
53 41
            return $this->add(new Where($this, $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 mode() 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...
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...
54
        }
55
56 5
        if ($field instanceof \Closure) {
57 5
            return $this->add(new WhereGroup($this, $field, $this->mode()));
0 ignored issues
show
Bug introduced by
It seems like mode() 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...
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...
58
        }
59
60
        $error = \vsprintf('Selection set should be a type of string or Closure, but %s given', [
61
            \studly_case(\gettype($field)),
62
        ]);
63
64
        throw new \InvalidArgumentException($error);
65
    }
66
}
67