Passed
Push — master ( 5197c9...313b01 )
by Sébastien
04:05 queued 15s
created

PaginationWalkStrategy   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 16
dl 0
loc 38
ccs 14
cts 16
cp 0.875
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A next() 0 16 2
A initialize() 0 12 2
1
<?php
2
3
namespace Bdf\Prime\Query\Pagination\WalkStrategy;
4
5
use Bdf\Prime\Collection\CollectionInterface;
6
use Bdf\Prime\Query\Contract\Limitable;
7
use Bdf\Prime\Query\Contract\ReadOperation;
8
use Bdf\Prime\Query\ReadCommandInterface;
9
use InvalidArgumentException;
10
11
/**
12
 * Simple walk strategy using pagination
13
 * This strategy do not handle write on entities during the walk (like delete entities)
0 ignored issues
show
introduced by
Doc comment short description must be on a single line, further text should be a separate paragraph
Loading history...
14
 */
15
final class PaginationWalkStrategy implements WalkStrategyInterface
16
{
17
    /**
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 $chunkSize 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...
18
     * {@inheritdoc}
19
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
20 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...
21
    {
22 10
        if (!$query instanceof Limitable) {
23
            throw new InvalidArgumentException('The query must be an instance of '.Limitable::class);
24
        }
25
26 10
        $cursor = new WalkCursor(clone $query);
27
28 10
        $cursor->cursor = ($startPage - 1) * $chunkSize;
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
29 10
        $cursor->query->limit($chunkSize);
0 ignored issues
show
Bug introduced by
The method limit() 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

29
        $cursor->query->/** @scrutinizer ignore-call */ 
30
                        limit($chunkSize);
Loading history...
30
31 10
        return $cursor;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    #[ReadOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
38 10
    public function next(WalkCursor $cursor): WalkCursor
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
39
    {
40 10
        $cursor = clone $cursor;
41
        /** @var Limitable&ReadCommandInterface $query */
0 ignored issues
show
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...
Coding Style introduced by
Block comments must be started with /*
Loading history...
Coding Style introduced by
Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
Loading history...
42 10
        $query = $cursor->query;
43
44 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

44
        $query->/** @scrutinizer ignore-call */ 
45
                offset($cursor->cursor);
Loading history...
45 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

45
        $cursor->cursor += $query->/** @scrutinizer ignore-call */ getLimit();
Loading history...
46 10
        $cursor->entities = $query->all();
47
48 10
        if ($cursor->entities instanceof CollectionInterface) {
0 ignored issues
show
introduced by
$cursor->entities is never a sub-type of Bdf\Prime\Collection\CollectionInterface.
Loading history...
49
            $cursor->entities = $cursor->entities->all();
50
        }
51
52 10
        return $cursor;
53
    }
54
}
55