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

SelectProvider::select()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 11
cp 0
rs 9.0111
c 0
b 0
f 0
cc 6
nc 4
nop 1
crap 42
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\Selection;
13
use RDS\Hydrogen\Query;
14
15
/**
16
 * Trait SelectProvider
17
 * @mixin Query
18
 */
19
trait SelectProvider
20
{
21
    /**
22
     * @param array|string ...$fields
23
     * @return Query|$this|self
24
     */
25
    public function select(...$fields): self
26
    {
27
        foreach ($fields as $field) {
28
            if (! \is_array($field) && ! \is_string($field)) {
29
                $error = 'Selection should be array (["field" => "alias"]) or string ("field") but %s given';
30
                throw new \InvalidArgumentException(\sprintf($error, \gettype($field)));
31
            }
32
33
            if (\is_string($field)) {
34
                $this->add(new Selection($field));
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...
35
                continue;
36
            }
37
38
            foreach ($field as $name => $alias) {
39
                $this->add(new Selection($name, $alias));
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...
40
            }
41
        }
42
        return $this;
43
    }
44
}
45