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

HasRelationships   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 8
c 3
b 1
f 0
dl 0
loc 41
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A withRelationships() 0 5 1
A builder() 0 9 2
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