Completed
Push — master ( 62a574...40c09d )
by
unknown
01:52
created

AppendsAttributesToResults   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 34
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A allowedAppends() 0 10 2
A addAppendsToResults() 0 6 1
A guardAgainstUnknownAppends() 0 10 2
1
<?php
2
3
namespace Spatie\QueryBuilder\Concerns;
4
5
use Illuminate\Support\Collection;
6
use Spatie\QueryBuilder\Exceptions\InvalidAppendQuery;
7
8
trait AppendsAttributesToResults
9
{
10
    /** @var \Illuminate\Support\Collection */
11
    protected $allowedAppends;
12
13
    public function allowedAppends($appends): self
14
    {
15
        $appends = is_array($appends) ? $appends : func_get_args();
16
17
        $this->allowedAppends = collect($appends);
18
19
        $this->guardAgainstUnknownAppends();
20
21
        return $this;
22
    }
23
24
    protected function addAppendsToResults(Collection $results)
25
    {
26
        $appends = $this->request->appends();
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
28
        return $results->each->append($appends->toArray());
29
    }
30
31
    protected function guardAgainstUnknownAppends()
32
    {
33
        $appends = $this->request->appends();
34
35
        $diff = $appends->diff($this->allowedAppends);
36
37
        if ($diff->count()) {
38
            throw InvalidAppendQuery::appendsNotAllowed($diff, $this->allowedAppends);
39
        }
40
    }
41
}
42