Passed
Pull Request — master (#32)
by Stephen
14:26
created

HasRelationships::builder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Sfneal\Queries\Traits;
4
5
// todo: add tests with relationship
6
use Illuminate\Database\Eloquent\Builder;
7
8
trait HasRelationships
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $relationships = [];
14
15
    /**
16
     * Scope Plan search results to only plans that are 'public'.
17
     *
18
     * @param array $relationships
19
     * @return $this
20
     */
21
    public function withRelationships(array $relationships = []): self
22
    {
23
        $this->relationships = $relationships;
24
25
        return $this;
26
    }
27
28
    /**
29
     * Retrieve a Query builder.
30
     *
31
     * @return Builder
32
     */
33
    abstract protected function newBuilder(): Builder;
34
35
    /**
36
     * Retrieve a Query builder.
37
     *
38
     * @return Builder
39
     */
40
    protected function builder(): Builder
41
    {
42
        $builder = $this->newBuilder();
43
44
        if ($this->relationships) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->relationships of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
45
            $builder->with($this->relationships);
46
        }
47
48
        return $builder;
49
    }
50
}
51