Completed
Push — master ( 1cac13...727973 )
by Freek
14s
created

QueryBuilderServiceProvider::boot()   C

Complexity

Conditions 10
Paths 1

Size

Total Lines 72
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 72
rs 5.9036
c 0
b 0
f 0
cc 10
eloc 36
nc 1
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Spatie\QueryBuilder;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\ServiceProvider;
7
8
class QueryBuilderServiceProvider extends ServiceProvider
9
{
10
    public function boot()
11
    {
12
        Request::macro('includes', function ($include = null) {
13
            $includeParts = $this->query('include');
0 ignored issues
show
Bug introduced by
The method query() does not seem to exist on object<Spatie\QueryBuild...BuilderServiceProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
14
15
            if (! is_array($includeParts)) {
16
                $includeParts = explode(',', strtolower($this->query('include')));
0 ignored issues
show
Bug introduced by
The method query() does not seem to exist on object<Spatie\QueryBuild...BuilderServiceProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
17
            }
18
19
            $includes = collect($includeParts)->filter();
20
21
            if (is_null($include)) {
22
                return $includes;
23
            }
24
25
            return $includes->contains(strtolower($include));
26
        });
27
28
        Request::macro('filters', function ($filter = null) {
29
            $filterParts = $this->query('filter');
0 ignored issues
show
Bug introduced by
The method query() does not seem to exist on object<Spatie\QueryBuild...BuilderServiceProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
31
            if (! $filterParts) {
32
                return collect();
33
            }
34
35
            $filters = collect($filterParts)->filter(function ($filter) {
36
                return ! is_null($filter);
37
            });
38
39
            $filters = $filters->map(function ($value) {
40
                if (str_contains($value, ',')) {
41
                    return explode(',', $value);
42
                }
43
44
                if ($value === 'true') {
45
                    return true;
46
                }
47
48
                if ($value === 'false') {
49
                    return false;
50
                }
51
52
                return $value;
53
            });
54
55
            if (is_null($filter)) {
56
                return $filters;
57
            }
58
59
            return $filters->get(strtolower($filter));
60
        });
61
62
        Request::macro('sort', function ($default = null) {
63
            return $this->query('sort', $default);
0 ignored issues
show
Bug introduced by
The method query() does not seem to exist on object<Spatie\QueryBuild...BuilderServiceProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
        });
65
66
        Request::macro('sorts', function ($default = null) {
67
            $sortParts = $this->sort();
0 ignored issues
show
Bug introduced by
The method sort() does not seem to exist on object<Spatie\QueryBuild...BuilderServiceProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
69
            if (! is_array($sortParts)) {
70
                $sortParts = explode(',', $sortParts);
71
            }
72
73
            $sorts = collect($sortParts)->filter();
74
75
            if ($sorts->isNotEmpty()) {
76
                return $sorts;
77
            }
78
79
            return collect($default)->filter();
80
        });
81
    }
82
}
83