Completed
Push — 2.0 ( fe8057...a5342d )
by Kirill
06:55
created

WhereInSelection::getQueryName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
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\Kernel;
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
 * @package App\GraphQL\Kernel
18
 */
19
trait WhereInSelection
20
{
21
    /**
22
     * @param array $args
23
     * @return array
24
     */
25
    protected function argumentsWithWhereIn(array $args): array
26
    {
27
        return array_merge($args, [
28
            'id'  => [
29
                'type'        => Type::listOf(Type::id()),
30
                'description' => 'Select ' . $this->getQueryName() . ' by identifier or identifier array',
31
            ],
32
        ]);
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    private function getQueryName(): string
39
    {
40
        $short = (new \ReflectionObject($this))->getShortName();
41
42
        return str_replace('Query', '', $short);
43
    }
44
45
    /**
46
     * @param  EBuilder|QBuilder $builder
47
     * @param  array             $args
48
     * @return EBuilder
49
     */
50
    protected function queryWithWhereIn($builder, array $args = [])
51
    {
52
        if (isset($args['id'])) {
53
            if (count($args['id']) === 1) {
54
                return $builder->where('id', reset($args['id']));
55
            }
56
57
            return $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...
58
        }
59
60
        return $builder;
61
    }
62
}
63