Completed
Pull Request — master (#15)
by
unknown
01:19
created

QueryBuilderServiceProvider::boot()   C

Complexity

Conditions 8
Paths 1

Size

Total Lines 65
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 65
rs 6.7651
cc 8
eloc 33
nc 1
nop 0

How to fix   Long Method   

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->loadTranslationsFrom(
13
            __DIR__.'/../lang',
14
            'query-builder'
15
        );
16
17
        $this->publishes([
18
            __DIR__.'/../lang' => resource_path('lang/vendor/query-builder'),
19
        ]);
20
21
        Request::macro('includes', function ($include = null) {
22
            $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...
23
24
            if (! is_array($includeParts)) {
25
                $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...
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
        Request::macro('filters', function ($filter = null) {
38
            $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...
39
40
            if (! $filterParts) {
41
                return collect();
42
            }
43
44
            $filters = collect($filterParts)->filter(function ($filter) {
45
                return ! is_null($filter);
46
            });
47
48
            $filters = $filters->map(function ($value) {
49
                if (str_contains($value, ',')) {
50
                    return explode(',', $value);
51
                }
52
53
                if ($value === 'true') {
54
                    return true;
55
                }
56
57
                if ($value === 'false') {
58
                    return false;
59
                }
60
61
                return $value;
62
            });
63
64
            if (is_null($filter)) {
65
                return $filters;
66
            }
67
68
            return $filters->get(strtolower($filter));
69
        });
70
71
        Request::macro('sort', function ($default = null) {
72
            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...
73
        });
74
    }
75
}
76