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

WhereInSelection::argumentsWithWhereIn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of laravel.su package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace App\GraphQL\Queries\Support;
10
11
use GraphQL\Type\Definition\Type;
12
use Illuminate\Database\Query\Builder as QBuilder;
13
use Illuminate\Database\Eloquent\Builder as EBuilder;
14
15
/**
16
 * Class WhereInSelection.
17
 */
18
trait WhereInSelection
19
{
20
    /**
21
     * @param array $args
22
     * @return array
23
     */
24
    protected function argumentsWithWhereIn(array $args): array
25
    {
26
        return array_merge($args, [
27
            'id'  => [
28
                'type'        => Type::listOf(Type::id()),
29
                'description' => 'Select ' . $this->getQueryName() . ' by identifier or identifier array',
30
            ],
31
        ]);
32
    }
33
34
    /**
35
     * @return string
36
     */
37
    private function getQueryName(): string
38
    {
39
        $short = (new \ReflectionObject($this))->getShortName();
40
41
        return str_replace('Query', '', $short);
42
    }
43
44
    /**
45
     * @param  EBuilder|QBuilder $builder
46
     * @param  array             $args
47
     * @return EBuilder
48
     */
49
    protected function queryWithWhereIn($builder, array &$args = [])
50
    {
51
        if (isset($args['id'])) {
52
            if (count($args['id']) === 1) {
53
                $builder->where('id', reset($args['id']));
54
            } else {
55
                $builder->whereIn('id', $args['id']);
0 ignored issues
show
Bug introduced by
The method whereIn 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...
56
            }
57
58
            unset($args['id']);
59
        }
60
61
        return $builder;
62
    }
63
}