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

RelationProvider::with()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 13
cp 0
rs 8.8817
c 0
b 0
f 0
cc 6
nc 5
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\Relation;
13
use RDS\Hydrogen\Query;
14
15
/**
16
 * Trait RelationProvider
17
 * @mixin Query
18
 */
19
trait RelationProvider
20
{
21
    /**
22
     * @param string|array ...$relations
23
     * @return Query|$this|self
24
     */
25
    public function with(...$relations): self
26
    {
27
        foreach ($relations as $relation) {
28
            if (\is_string($relation)) {
29
                $this->add(new Relation($relation, $this));
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...
30
                continue;
31
            }
32
33
            if (\is_array($relation)) {
34
                foreach ($relation as $rel => $sub) {
35
                    \assert(\is_string($rel) && $sub instanceof \Closure);
36
37
                    $this->add(new Relation($rel, $this, $sub));
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...
38
                }
39
                continue;
40
            }
41
42
            $error = 'Relation should be string ("relation_name") '.
43
                'or array (["relation" => function]), ' .
44
                'but %s given';
45
46
            throw new \InvalidArgumentException(\sprintf($error, \gettype($relation)));
47
        }
48
49
        return $this;
50
    }
51
}
52