Passed
Pull Request — master (#29)
by Sébastien
08:57
created

PaginationWalkStrategy::initialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 13
ccs 7
cts 8
cp 0.875
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 3
crap 2.0078
1
<?php
2
3
namespace Bdf\Prime\Query\Pagination\WalkStrategy;
4
5
use Bdf\Prime\Collection\CollectionInterface;
6
use Bdf\Prime\Connection\ConnectionInterface;
7
use Bdf\Prime\Query\Contract\Limitable;
8
use Bdf\Prime\Query\Contract\ReadOperation;
9
use Bdf\Prime\Query\ReadCommandInterface;
10
use InvalidArgumentException;
11
12
/**
13
 * Simple walk strategy using pagination
14
 * This strategy do not handle write on entities during the walk (like delete entities)
15
 *
16
 * @template R as array|object
17
 * @implements WalkStrategyInterface<R>
18
 */
19
final class PaginationWalkStrategy implements WalkStrategyInterface
20
{
21
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $startPage should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $query should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $chunkSize should have a doc-comment as per coding-style.
Loading history...
22
     * {@inheritdoc}
23
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
24 10
    public function initialize(ReadCommandInterface $query, int $chunkSize, int $startPage): WalkCursor
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 0 found
Loading history...
25
    {
26 10
        if (!$query instanceof Limitable) {
27
            throw new InvalidArgumentException('The query must be an instance of '.Limitable::class);
28
        }
29
30 10
        $query = clone $query;
31 10
        $cursor = new WalkCursor($query);
32
33 10
        $cursor->cursor = ($startPage - 1) * $chunkSize;
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
34 10
        $query->limit($chunkSize);
35
36 10
        return $cursor;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    #[ReadOperation]
43 10
    public function next(WalkCursor $cursor): WalkCursor
44
    {
45 10
        $cursor = clone $cursor;
46
        /** @var Limitable&ReadCommandInterface<ConnectionInterface, R> $query */
0 ignored issues
show
Coding Style introduced by
Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
Loading history...
Coding Style introduced by
Block comments must be started with /*
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
47 10
        $query = $cursor->query;
48
49 10
        $query->offset($cursor->cursor);
0 ignored issues
show
Bug introduced by
The method offset() does not exist on Bdf\Prime\Query\ReadCommandInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Bdf\Prime\Query\Contract...\KeyValueQueryInterface or Bdf\Prime\Query\QueryInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        $query->/** @scrutinizer ignore-call */ 
50
                offset($cursor->cursor);
Loading history...
50 10
        $cursor->cursor += $query->getLimit();
0 ignored issues
show
Bug introduced by
The method getLimit() does not exist on Bdf\Prime\Query\ReadCommandInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Bdf\Prime\Query\Contract...\KeyValueQueryInterface or Bdf\Prime\Query\QueryInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        $cursor->cursor += $query->/** @scrutinizer ignore-call */ getLimit();
Loading history...
51 10
        $cursor->entities = $query->all();
0 ignored issues
show
Documentation Bug introduced by
It seems like $query->all() of type Bdf\Prime\Query\R[]&Bdf\...ion\CollectionInterface is incompatible with the declared type Bdf\Prime\Query\Pagination\WalkStrategy\R[]|null of property $entities.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52
53 10
        if ($cursor->entities instanceof CollectionInterface) {
54
            $cursor->entities = $cursor->entities->all();
55
        }
56
57 10
        return $cursor;
58
    }
59
}
60