Test Failed
Push — master ( 1c5165...b7ef6a )
by Kirill
03:37
created

RelationProvider::with()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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\Join;
13
use RDS\Hydrogen\Criteria\Relation;
14
use RDS\Hydrogen\Query;
15
16
/**
17
 * Trait RelationProvider
18
 * @mixin Query
19
 */
20
trait RelationProvider
21
{
22
    /**
23
     * @param string|array ...$relations
24
     * @return Query|$this|self
25
     */
26
    public function with(...$relations): self
27
    {
28
        return $this->addRelation(function(string $field, \Closure $inner = null) {
29
            return new Relation($field, $this, $inner);
30
        }, ...$relations);
31
    }
32
33
    /**
34
     * @param string|array ...$relations
35
     * @return Query|$this|self
36
     */
37
    public function join(...$relations): self
38
    {
39
        return $this->addRelation(function(string $field, \Closure $inner = null) {
40
            return new Join($field, $this, Join::TYPE_JOIN, $inner);
41
        }, ...$relations);
42
    }
43
44
    /**
45
     * @param string|array ...$relations
46
     * @return Query|$this|self
47
     */
48
    public function leftJoin(...$relations): self
49
    {
50 2
        return $this->addRelation(function(string $field, \Closure $inner = null) {
51 2
            return new Join($field, $this, Join::TYPE_LEFT_JOIN, $inner);
52 2
        }, ...$relations);
53
    }
54
55
    /**
56
     * @param string|array ...$relations
57
     * @return Query|$this|self
58
     */
59
    public function innerJoin(...$relations): self
60
    {
61
        return $this->addRelation(function(string $field, \Closure $inner = null) {
62
            return new Join($field, $this, Join::TYPE_INNER_JOIN, $inner);
63
        }, ...$relations);
64
    }
65
66
    /**
67
     * @param \Closure $onCreate
68
     * @param string|array ...$relations
69
     * @return Query|$this|self
70
     */
71 2
    private function addRelation(\Closure $onCreate, ...$relations): self
72
    {
73 2
        foreach ($relations as $relation) {
74 2
            if (\is_string($relation)) {
75 2
                $this->add($onCreate($relation));
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...
76 2
                continue;
77
            }
78
79
            if (\is_array($relation)) {
80
                foreach ($relation as $rel => $sub) {
81
                    \assert(\is_string($rel) && $sub instanceof \Closure);
82
83
                    $this->add($onCreate($rel, $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...
84
                }
85
                continue;
86
            }
87
88
            $error = 'Relation should be string ("relation_name") '.
89
                'or array (["relation" => function]), ' .
90
                'but %s given';
91
92
            throw new \InvalidArgumentException(\sprintf($error, \gettype($relation)));
93
        }
94
95 2
        return $this;
96
    }
97
}
98