CursorArgs::requestedTooManyItems()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace DutchCodingCompany\CursorPagination\Pagination;
4
5
use GraphQL\Error\Error;
6
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
7
use Illuminate\Support\Arr;
8
use Laravel\Scout\Builder as ScoutBuilder;
9
use DutchCodingCompany\CursorPagination\Cursor;
10
use DutchCodingCompany\CursorPagination\CursorPaginator;
11
use Nuwave\Lighthouse\Pagination\PaginationArgs;
12
use Nuwave\Lighthouse\Pagination\PaginationType;
13
14
class CursorArgs
15
{
16
    /**
17
     * @var Cursor
18
     */
19
    public $cursor;
20
21
    /**
22
     * @var int
23
     */
24
    public $first;
25
26
    /**
27
     * Create a new instance from user given args.
28
     *
29
     * @param  mixed[]  $args
30
     * @param  \Nuwave\Lighthouse\Pagination\PaginationType|null  $paginationType
31
     * @return static
32
     *
33
     * @throws \GraphQL\Error\Error
34
     */
35
    public static function extractArgs(array $args, ?PaginationType $paginationType, ?int $paginateMaxCount): self
36
    {
37
        $instance = new static();
38
39
        if ($paginationType->isConnection()) {
0 ignored issues
show
Bug introduced by
It seems like $paginationType is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
40
            $instance->first = $args['first'];
41
            $instance->cursor = Cursor::decode($args);
42
        } else {
43
            return PaginationArgs::extractArgs($args, $paginationType, $paginateMaxCount);
0 ignored issues
show
Bug introduced by
It seems like $paginationType defined by parameter $paginationType on line 35 can be null; however, Nuwave\Lighthouse\Pagina...tionArgs::extractArgs() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
44
        }
45
46
        if ($instance->first <= 0) {
47
            throw new Error(
48
                self::requestedZeroOrLessItems($instance->first)
49
            );
50
        }
51
52
        // Make sure the maximum pagination count is not exceeded
53
        if (
54
            $paginateMaxCount !== null
55
            && $instance->first > $paginateMaxCount
56
        ) {
57
            throw new Error(
58
                self::requestedTooManyItems($paginateMaxCount, $instance->first)
59
            );
60
        }
61
62
        return $instance;
63
    }
64
65
    public static function requestedZeroOrLessItems(int $amount): string
66
    {
67
        return "Requested pagination amount must be more than 0, got {$amount}.";
68
    }
69
70
    public static function requestedTooManyItems(int $maxCount, int $actualCount): string
71
    {
72
        return "Maximum number of {$maxCount} requested items exceeded, got {$actualCount}. Fetch smaller chunks.";
73
    }
74
75
    public static function scoutBuilderNotSupported(): string
76
    {
77
        return "Laravel Scout Builder is not supported for connection based pagination";
78
    }
79
80
    /**
81
     * Apply the args to a builder, constructing a paginator.
82
     *
83
     * @param \Illuminate\Database\Query\Builder|\Laravel\Scout\Builder|\Illuminate\Database\Eloquent\Relations\Relation $builder
84
     */
85
    public function applyToBuilder($builder): CursorPaginator
86
    {
87
        if ($builder instanceof ScoutBuilder) {
0 ignored issues
show
Bug introduced by
The class Laravel\Scout\Builder does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
88
            throw new Error(
89
                self::scoutBuilderNotSupported()
90
            );
91
        }
92
93
        return $builder->cursorPaginate($this->first, $this->cursor);
94
    }
95
}
96