Completed
Push — master ( c06df0...e0073a )
by
unknown
01:25
created

QueryBuilderServiceProvider::boot()   D

Complexity

Conditions 15
Paths 2

Size

Total Lines 116

Duplication

Lines 32
Ratio 27.59 %

Importance

Changes 0
Metric Value
dl 32
loc 116
rs 4.7333
c 0
b 0
f 0
cc 15
nc 2
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
        if ($this->app->runningInConsole()) {
13
            $this->publishes([
14
                __DIR__.'/../config/query-builder.php' => config_path('query-builder.php'),
15
            ], 'config');
16
        }
17
18
        $this->mergeConfigFrom(__DIR__.'/../config/query-builder.php', 'query-builder');
19
20 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...
21
            $parameter = config('query-builder.parameters.include');
22
            $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...
23
24
            if (! is_array($includeParts)) {
25
                $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...
26
            }
27
28
            $includes = collect($includeParts)->filter();
29
30
            if (is_null($include)) {
31
                return $includes;
32
            }
33
34
            return $includes->contains(strtolower($include));
35
        });
36
37 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...
38
            $parameter = config('query-builder.parameters.append');
39
            $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...
40
41
            if (! is_array($appendParts)) {
42
                $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...
43
            }
44
45
            $appends = collect($appendParts)->filter();
46
47
            if (is_null($append)) {
48
                return $appends;
49
            }
50
51
            return $appends->contains(strtolower($append));
52
        });
53
54
        Request::macro('filters', function ($filter = null) {
55
            $filterParts = $this->query(config('query-builder.parameters.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...
56
57
            if (is_string($filterParts)) {
58
                return collect();
59
            }
60
61
            $filters = collect($filterParts);
62
63
            $filtersMapper = function ($value) {
64
                if (is_array($value)) {
65
                    return collect($value)->map($this)->all();
0 ignored issues
show
Documentation introduced by
$this is of type this<Spatie\QueryBuilder...BuilderServiceProvider>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
                }
67
68
                if (str_contains($value, ',')) {
69
                    return explode(',', $value);
70
                }
71
72
                if ($value === 'true') {
73
                    return true;
74
                }
75
76
                if ($value === 'false') {
77
                    return false;
78
                }
79
80
                return $value;
81
            };
82
83
            $filters = $filters->map($filtersMapper->bindTo($filtersMapper));
84
85
            if (is_null($filter)) {
86
                return $filters;
87
            }
88
89
            return $filters->get(strtolower($filter));
90
        });
91
92
        Request::macro('fields', function () {
93
            $fieldsPerTable = collect(
94
                $this->query(config('query-builder.parameters.fields'))
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...
95
            );
96
97
            if ($fieldsPerTable->isEmpty()) {
98
                return null;
99
            }
100
101
            return $fieldsPerTable->map(function ($fields) {
102
                return explode(',', $fields);
103
            });
104
        });
105
106
        Request::macro('sort', function ($default = null) {
107
            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...
108
        });
109
110
        Request::macro('sorts', function ($default = null) {
111
            $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...
112
113
            if (! is_array($sortParts)) {
114
                $sortParts = explode(',', $sortParts);
115
            }
116
117
            $sorts = collect($sortParts)->filter();
118
119
            if ($sorts->isNotEmpty()) {
120
                return $sorts;
121
            }
122
123
            return collect($default)->filter();
124
        });
125
    }
126
}
127