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

AddsFieldsToQuery   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 73
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A allowedFields() 0 21 4
A parseFields() 0 4 1
A addFieldsToQuery() 0 8 2
A prependFieldsWithTableName() 0 6 1
A getFieldsForIncludedTable() 0 4 1
A guardAgainstUnknownFields() 0 19 2
1
<?php
2
3
namespace Spatie\QueryBuilder\Concerns;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use Spatie\QueryBuilder\Exceptions\InvalidFieldQuery;
8
9
trait AddsFieldsToQuery
10
{
11
    /** @var \Illuminate\Support\Collection */
12
    protected $allowedFields;
13
14
    public function allowedFields($fields): self
15
    {
16
        $fields = is_array($fields) ? $fields : func_get_args();
17
18
        $this->allowedFields = collect($fields)
19
            ->map(function (string $fieldName) {
20
                if (! Str::contains($fieldName, '.')) {
21
                    $modelTableName = $this->getModel()->getTable();
0 ignored issues
show
Bug introduced by
It seems like getModel() 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...
22
23
                    return "{$modelTableName}.{$fieldName}";
24
                }
25
26
                return $fieldName;
27
            });
28
29
        if (! $this->allowedFields->contains('*')) {
30
            $this->guardAgainstUnknownFields();
31
        }
32
33
        return $this;
34
    }
35
36
    public function parseFields()
37
    {
38
        $this->addFieldsToQuery($this->request->fields());
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...
39
    }
40
41
    protected function addFieldsToQuery(Collection $fields)
42
    {
43
        $modelTableName = $this->getModel()->getTable();
0 ignored issues
show
Bug introduced by
It seems like getModel() 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...
44
45
        if ($modelFields = $fields->get($modelTableName)) {
46
            $this->select($this->prependFieldsWithTableName($modelFields, $modelTableName));
0 ignored issues
show
Bug introduced by
It seems like select() 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...
47
        }
48
    }
49
50
    protected function prependFieldsWithTableName(array $fields, string $tableName): array
51
    {
52
        return array_map(function ($field) use ($tableName) {
53
            return "{$tableName}.{$field}";
54
        }, $fields);
55
    }
56
57
    protected function getFieldsForIncludedTable(string $relation): array
58
    {
59
        return $this->request->fields()->get($relation, []);
60
    }
61
62
    protected function guardAgainstUnknownFields()
63
    {
64
        $fields = $this->request->fields()
65
            ->map(function ($fields, $model) {
66
                $tableName = Str::snake(preg_replace('/-/', '_', $model));
67
68
                $fields = array_map([Str::class, 'snake'], $fields);
69
70
                return $this->prependFieldsWithTableName($fields, $tableName);
71
            })
72
            ->flatten()
73
            ->unique();
74
75
        $diff = $fields->diff($this->allowedFields);
76
77
        if ($diff->count()) {
78
            throw InvalidFieldQuery::fieldsNotAllowed($diff, $this->allowedFields);
79
        }
80
    }
81
}
82