Test Failed
Push — feature-laravel-5.4 ( 6f3b9a...66d75b )
by Kirill
03:40
created

QueryLimit::queryWithLimit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Unused Code introduced by
$limit is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
59
60
        if (isset($args['_limit'])) {
61
            $limit = max(1, min(1000, (int) $args['_limit']));
62
63
            $builder = $builder->take($limit);
0 ignored issues
show
Bug introduced by
The method take does only exist in Illuminate\Database\Query\Builder, but not in Illuminate\Database\Eloquent\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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