Completed
Push — master ( 5d7463...54a08b )
by Freek
12s queued 10s
created

QueryBuilderServiceProvider::boot()   D

Complexity

Conditions 13
Paths 1

Size

Total Lines 108
Code Lines 56

Duplication

Lines 32
Ratio 29.63 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 32
loc 108
rs 4.9922
c 1
b 0
f 0
cc 13
eloc 56
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
        $this->publishes([
13
            __DIR__.'/../config/query-builder.php' => config_path('query-builder.php'),
14
        ], 'config');
15
16
        $this->mergeConfigFrom(__DIR__.'/../config/query-builder.php', 'query-builder');
17
18 View Code Duplication
        Request::macro('includes', function ($include = null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
            $parameter = config('query-builder.parameters.include');
20
            $includeParts = $this->query($parameter);
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...
21
22
            if (! is_array($includeParts)) {
23
                $includeParts = explode(',', strtolower($this->query($parameter)));
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...
24
            }
25
26
            $includes = collect($includeParts)->filter();
27
28
            if (is_null($include)) {
29
                return $includes;
30
            }
31
32
            return $includes->contains(strtolower($include));
33
        });
34
35 View Code Duplication
        Request::macro('appends', function ($append = null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
            $parameter = config('query-builder.parameters.append');
37
            $appendParts = $this->query($parameter);
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...
38
39
            if (! is_array($appendParts)) {
40
                $appendParts = explode(',', strtolower($this->query($parameter)));
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...
41
            }
42
43
            $appends = collect($appendParts)->filter();
44
45
            if (is_null($append)) {
46
                return $appends;
47
            }
48
49
            return $appends->contains(strtolower($append));
50
        });
51
52
        Request::macro('filters', function ($filter = null) {
53
            $filterParts = $this->query(
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...
54
                config('query-builder.parameters.filter')
55
            );
56
57
            if (! $filterParts) {
58
                return collect();
59
            }
60
61
            $filters = collect($filterParts)->filter(function ($filter) {
62
                return ! is_null($filter);
63
            });
64
65
            $filtersMapper = function ($value) {
66
                if (is_array($value)) {
67
                    return collect($value)->map($this)->all();
68
                }
69
70
                if (str_contains($value, ',')) {
71
                    return explode(',', $value);
72
                }
73
74
                if ($value === 'true') {
75
                    return true;
76
                }
77
78
                if ($value === 'false') {
79
                    return false;
80
                }
81
82
                return $value;
83
            };
84
85
            $filters = $filters->map($filtersMapper->bindTo($filtersMapper));
86
87
            if (is_null($filter)) {
88
                return $filters;
89
            }
90
91
            return $filters->get(strtolower($filter));
92
        });
93
94
        Request::macro('sort', function ($default = null) {
95
            return $this->query(config('query-builder.parameters.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...
96
        });
97
98
        Request::macro('fields', function ($default = null) {
99
            return collect($this->query(config('query-builder.parameters.fields'), $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...
100
        });
101
102
        Request::macro('sorts', function ($default = null) {
103
            $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...
104
105
            if (! is_array($sortParts)) {
106
                $sortParts = explode(',', $sortParts);
107
            }
108
109
            $sorts = collect($sortParts)->filter();
110
111
            if ($sorts->isNotEmpty()) {
112
                return $sorts;
113
            }
114
115
            return collect($default)->filter();
116
        });
117
    }
118
}
119