Completed
Push — master ( 741bca...e69c57 )
by Kirill
02:43
created

GroupByProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 26.67%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 51
c 0
b 0
f 0
ccs 4
cts 15
cp 0.2667
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A groupBy() 0 8 2
A orHaving() 0 4 1
A having() 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\Group;
13
use RDS\Hydrogen\Criteria\GroupBy;
14
use RDS\Hydrogen\Criteria\Having;
15
use RDS\Hydrogen\Query;
16
17
/**
18
 * Trait GroupByProvider
19
 * @mixin Query
20
 */
21
trait GroupByProvider
22
{
23
    /**
24
     * @param string[] $fields
25
     * @return Query|$this|self
26
     */
27 2
    public function groupBy(string ...$fields): self
28
    {
29 2
        foreach ($fields as $field) {
30 2
            $this->add(new GroupBy($field));
0 ignored issues
show
Documentation introduced by
$field is of type array<integer,string>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
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...
31
        }
32
33 2
        return $this;
34
    }
35
36
    /**
37
     * @param string|\Closure $field
38
     * @param $valueOrOperator
39
     * @param null $value
40
     * @return Query|$this|self
41
     */
42
    public function orHaving($field, $valueOrOperator = null, $value = null): self
43
    {
44
        return $this->or->having($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...
45
    }
46
47
    /**
48
     * @param string|\Closure $field
49
     * @param $valueOrOperator
50
     * @param null $value
51
     * @return Query|$this|self
52
     */
53
    public function having($field, $valueOrOperator = null, $value = null): self
54
    {
55
        if (\is_string($field)) {
56
            [$operator, $value] = Having::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...
57
58
            return $this->add(new Having($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...
59
        }
60
61
        if ($field instanceof \Closure) {
62
            return $this->add(new Group($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...
63
        }
64
65
        $error = \vsprintf('Selection set should be a type of string or Closure, but %s given', [
66
            \studly_case(\gettype($field)),
67
        ]);
68
69
        throw new \InvalidArgumentException($error);
70
    }
71
}
72