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

QueryBuilderServiceProvider   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 111
Duplicated Lines 28.83 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 32
loc 111
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D boot() 32 108 13

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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