|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of laravel.su package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace App\GraphQL\Queries\Support; |
|
12
|
|
|
|
|
13
|
|
|
use GraphQL\Type\Definition\Type; |
|
14
|
|
|
use Illuminate\Database\Query\Builder as QBuilder; |
|
15
|
|
|
use Illuminate\Database\Eloquent\Builder as EBuilder; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class QueryLimit. |
|
19
|
|
|
*/ |
|
20
|
|
|
trait QueryLimit |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @param array $args |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected function argumentsWithLimit(array $args): array |
|
27
|
|
|
{ |
|
28
|
|
|
return array_merge($args, [ |
|
29
|
|
|
'_limit' => [ |
|
30
|
|
|
'type' => Type::int(), |
|
31
|
|
|
'description' => 'Items per page: in 1...1000 range', |
|
32
|
|
|
], |
|
33
|
|
|
|
|
34
|
|
|
'_page' => [ |
|
35
|
|
|
'type' => Type::int(), |
|
36
|
|
|
'description' => 'Current page number (Usage without "_limit" argument gives no effect)', |
|
37
|
|
|
], |
|
38
|
|
|
]); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param EBuilder|QBuilder $builder |
|
43
|
|
|
* @param array $args |
|
44
|
|
|
* @return EBuilder |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function queryWithLimit($builder, array &$args = []) |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->paginate($builder, $args); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param EBuilder|QBuilder $builder |
|
53
|
|
|
* @param array $args |
|
54
|
|
|
* @return EBuilder |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function paginate($builder, array &$args = []) |
|
57
|
|
|
{ |
|
58
|
|
|
$limit = null; |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
if (isset($args['_limit'])) { |
|
61
|
|
|
$limit = max(1, min(1000, (int) $args['_limit'])); |
|
62
|
|
|
|
|
63
|
|
|
$builder = $builder->take($limit); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
if (isset($args['_page'])) { |
|
66
|
|
|
$page = max(1, (int) $args['_page']); |
|
67
|
|
|
|
|
68
|
|
|
$builder = $builder->skip(($page - 1) * $limit); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
unset($args['_limit']); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (isset($args['_page'])) { |
|
75
|
|
|
unset($args['_page']); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $builder; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.