DocsQuery::type()   A
last analyzed

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 0
dl 0
loc 4
rs 10
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;
10
11
use App\Models\Docs;
12
use App\GraphQL\Types\DocsType;
13
use GraphQL\Type\Definition\Type;
14
use Illuminate\Database\Eloquent\Builder;
15
use Illuminate\Support\Collection;
16
use GraphQL\Type\Definition\ListOfType;
17
use App\GraphQL\Serializers\DocsSerializer;
18
19
/**
20
 * Class DocsQuery.
21
 */
22
class DocsQuery extends AbstractQuery
23
{
24
    /**
25
     * @var array
26
     */
27
    protected $attributes = [
28
        'name'        => 'Docs list query',
29
        'description' => 'Returns a list of available documentation repositories',
30
    ];
31
32
    /**
33
     * @return ListOfType
34
     */
35
    public function type(): ListOfType
36
    {
37
        return Type::listOf(\GraphQL::type('Docs'));
38
    }
39
40
    /**
41
     * @param        $root
42
     * @param  array $args
43
     * @return Collection
44
     */
45
    public function resolve($root, array $args = []): Collection
46
    {
47
        $query = $this->queryFor(Docs::class, $args);
48
49
        $this->checkVersion($query, $args);
50
        $this->checkProject($query, $args);
51
52
        return DocsSerializer::collection($query->get());
0 ignored issues
show
Bug introduced by
The method get does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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...
53
    }
54
55
    /**
56
     * @param Builder $query
57
     * @param array $args
58
     * @return Builder|null
59
     */
60
    private function checkProject(Builder $query, array $args): ?Builder
61
    {
62
        return $this->whenExists($args, 'project', function(string $project) use ($query) {
63
            return $query->where('slug', $project);
64
        });
65
    }
66
67
    /**
68
     * @param Builder $query
69
     * @param array $args
70
     * @return Builder
71
     */
72
    private function checkVersion(Builder $query, array $args): ?Builder
73
    {
74
        return $this->whenExists($args, 'version', function (string $version) use ($query) {
75
            if ($version === 'latest') {
76
                return $query->orderBy('version', 'desc')
0 ignored issues
show
Bug introduced by
The method orderBy() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean enforceOrderBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
77
                    ->take(1);
78
            }
79
80
            return $query->where('version', $version);
81
        });
82
    }
83
84
    /**
85
     * @return array
86
     */
87
    protected function queryArguments(): array
88
    {
89
        return [
90
            'version' => [
91
                'name'        => 'version',
92
                'type'        => Type::string(),
93
            ],
94
            'project' => [
95
                'name'        => 'project',
96
                'type'        => Type::string(),
97
            ],
98
        ];
99
    }
100
}
101