Completed
Pull Request — master (#451)
by
unknown
01:32
created

QueryBuilderRequest::includes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Spatie\QueryBuilder;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Str;
8
9
class QueryBuilderRequest extends Request
10
{
11
    private static $includesArrayValueDelimiter = ',';
12
    private static $appendsArrayValueDelimiter = ',';
13
    private static $fieldsArrayValueDelimiter = ',';
14
    private static $sortsArrayValueDelimiter = ',';
15
    private static $filterArrayValueDelimiter = ',';
16
17
    public static function setArrayValueDelimiter(string $delimiter): void
18
    {
19
        static::$filterArrayValueDelimiter = $delimiter;
0 ignored issues
show
Bug introduced by
Since $filterArrayValueDelimiter is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $filterArrayValueDelimiter to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
20
        static::$includesArrayValueDelimiter = $delimiter;
0 ignored issues
show
Bug introduced by
Since $includesArrayValueDelimiter is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $includesArrayValueDelimiter to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
21
        static::$appendsArrayValueDelimiter = $delimiter;
0 ignored issues
show
Bug introduced by
Since $appendsArrayValueDelimiter is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $appendsArrayValueDelimiter to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
22
        static::$fieldsArrayValueDelimiter = $delimiter;
0 ignored issues
show
Bug introduced by
Since $fieldsArrayValueDelimiter is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $fieldsArrayValueDelimiter to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
23
        static::$sortsArrayValueDelimiter = $delimiter;
0 ignored issues
show
Bug introduced by
Since $sortsArrayValueDelimiter is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $sortsArrayValueDelimiter to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
24
    }
25
26
    public static function fromRequest(Request $request): self
27
    {
28
        return static::createFrom($request, new self());
0 ignored issues
show
Documentation introduced by
$request is of type object<Illuminate\Http\Request>, but the function expects a object<self>.

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...
29
    }
30
31
    public function includes(): Collection
32
    {
33
        $includeParameterName = config('query-builder.parameters.include');
34
35
        $includeParts = $this->query($includeParameterName);
36
37
        if (!is_array($includeParts)) {
38
            $includeParts = explode(static::getIncludesArrayValueDelimiter(), $this->query($includeParameterName));
39
        }
40
41
        return collect($includeParts)
42
            ->filter()
43
            ->map([Str::class, 'camel']);
44
    }
45
46
    public function appends(): Collection
47
    {
48
        $appendParameterName = config('query-builder.parameters.append');
49
50
        $appendParts = $this->query($appendParameterName);
51
52
        if (!is_array($appendParts)) {
53
            $appendParts = explode(static::getAppendsArrayValueDelimiter(), strtolower($appendParts));
54
        }
55
56
        return collect($appendParts)->filter();
57
    }
58
59 View Code Duplication
    public function fields(): Collection
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
60
    {
61
        $fieldsParameterName = config('query-builder.parameters.fields');
62
63
        $fieldsPerTable = collect($this->query($fieldsParameterName));
64
65
        if ($fieldsPerTable->isEmpty()) {
66
            return collect();
67
        }
68
69
        return $fieldsPerTable->map(function ($fields) {
70
            return explode(static::getFieldsArrayValueDelimiter(), $fields);
71
        });
72
    }
73
74 View Code Duplication
    public function sorts(): Collection
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
75
    {
76
        $sortParameterName = config('query-builder.parameters.sort');
77
78
        $sortParts = $this->query($sortParameterName);
79
80
        if (is_string($sortParts)) {
81
            $sortParts = explode(static::getSortsArrayValueDelimiter(), $sortParts);
82
        }
83
84
        return collect($sortParts)->filter();
85
    }
86
87
    public function filters(): Collection
88
    {
89
        $filterParameterName = config('query-builder.parameters.filter');
90
91
        $filterParts = $this->query($filterParameterName, []);
92
93
        if (is_string($filterParts)) {
94
            return collect();
95
        }
96
97
        $filters = collect($filterParts);
98
99
        return $filters->map(function ($value) {
100
            return $this->getFilterValue($value);
101
        });
102
    }
103
104
    /**
105
     * @param $value
106
     *
107
     * @return array|bool
108
     */
109
    protected function getFilterValue($value)
110
    {
111
        if (is_array($value)) {
112
            return collect($value)->map(function ($valueValue) {
113
                return $this->getFilterValue($valueValue);
114
            })->all();
115
        }
116
117
        if (Str::contains($value, static::getFilterArrayValueDelimiter())) {
118
            return explode(static::getFilterArrayValueDelimiter(), $value);
119
        }
120
121
        if ($value === 'true') {
122
            return true;
123
        }
124
125
        if ($value === 'false') {
126
            return false;
127
        }
128
129
        return $value;
130
    }
131
132
    public static function setIncludesArrayValueDelimiter(string $includesArrayValueDelimiter): void
133
    {
134
        static::$includesArrayValueDelimiter = $includesArrayValueDelimiter;
0 ignored issues
show
Bug introduced by
Since $includesArrayValueDelimiter is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $includesArrayValueDelimiter to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
135
    }
136
137
    public static function setAppendsArrayValueDelimiter(string $appendsArrayValueDelimiter): void
138
    {
139
        static::$appendsArrayValueDelimiter = $appendsArrayValueDelimiter;
0 ignored issues
show
Bug introduced by
Since $appendsArrayValueDelimiter is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $appendsArrayValueDelimiter to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
140
    }
141
142
    public static function setFieldsArrayValueDelimiter(string $fieldsArrayValueDelimiter): void
143
    {
144
        static::$fieldsArrayValueDelimiter = $fieldsArrayValueDelimiter;
0 ignored issues
show
Bug introduced by
Since $fieldsArrayValueDelimiter is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $fieldsArrayValueDelimiter to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
145
    }
146
147
    public static function setSortsArrayValueDelimiter(string $sortsArrayValueDelimiter): void
148
    {
149
        static::$sortsArrayValueDelimiter = $sortsArrayValueDelimiter;
0 ignored issues
show
Bug introduced by
Since $sortsArrayValueDelimiter is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $sortsArrayValueDelimiter to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
150
    }
151
152
    public static function setFilterArrayValueDelimiter(string $filterArrayValueDelimiter): void
153
    {
154
        static::$filterArrayValueDelimiter = $filterArrayValueDelimiter;
0 ignored issues
show
Bug introduced by
Since $filterArrayValueDelimiter is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $filterArrayValueDelimiter to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
155
    }
156
157
    public static function getIncludesArrayValueDelimiter(): string
158
    {
159
        return static::$includesArrayValueDelimiter;
0 ignored issues
show
Bug introduced by
Since $includesArrayValueDelimiter is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $includesArrayValueDelimiter to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
160
    }
161
162
    public static function getAppendsArrayValueDelimiter(): string
163
    {
164
        return static::$appendsArrayValueDelimiter;
0 ignored issues
show
Bug introduced by
Since $appendsArrayValueDelimiter is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $appendsArrayValueDelimiter to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
165
    }
166
167
    public static function getFieldsArrayValueDelimiter(): string
168
    {
169
        return static::$fieldsArrayValueDelimiter;
0 ignored issues
show
Bug introduced by
Since $fieldsArrayValueDelimiter is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $fieldsArrayValueDelimiter to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
170
    }
171
172
    public static function getSortsArrayValueDelimiter(): string
173
    {
174
        return static::$sortsArrayValueDelimiter;
0 ignored issues
show
Bug introduced by
Since $sortsArrayValueDelimiter is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $sortsArrayValueDelimiter to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
175
    }
176
177
    public static function getFilterArrayValueDelimiter(): string
178
    {
179
        return static::$filterArrayValueDelimiter;
0 ignored issues
show
Bug introduced by
Since $filterArrayValueDelimiter is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $filterArrayValueDelimiter to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
180
    }
181
}
182