Paged   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A import() 0 13 5
A paged() 0 18 3
1
<?php
2
/**
3
 * Paged builder
4
 * User: moyo
5
 * Date: 2018/4/19
6
 * Time: 2:34 PM
7
 */
8
9
namespace Carno\Database\SQL\Paginator;
10
11
trait Paged
12
{
13
    /**
14
     * default page size
15
     * @var int
16
     */
17
    private $pSize = 20;
18
19
    /**
20
     * @param mixed ...$input
21
     * @return Pagination
22
     */
23
    public function paged(...$input)
24
    {
25
        list($page, $size) =
26
            count($input) === 1 && is_object($input[0])
27
                ? $this->import($input[0])
28
                : [$input[0] ?? 1, $input[1] ?? $this->pSize]
29
        ;
30
31
        $total = yield $this->count();
0 ignored issues
show
Bug Best Practice introduced by
The expression yield $this->count() returns the type Generator which is incompatible with the documented return type Carno\Database\SQL\Paginator\Pagination.
Loading history...
Bug introduced by
It seems like count() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

31
        $total = yield $this->/** @scrutinizer ignore-call */ count();
Loading history...
32
33
        $last = ceil($total / $size);
34
35
        $next = min($page + 1, $last);
36
        $prev = max($page - 1, 1);
37
38
        $this->limit(($page - 1) * $size, $size);
0 ignored issues
show
Bug introduced by
It seems like limit() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

38
        $this->/** @scrutinizer ignore-call */ 
39
               limit(($page - 1) * $size, $size);
Loading history...
39
40
        return new Pagination($total, $size, $prev, $page, $next, $last, yield $this->list());
0 ignored issues
show
Bug introduced by
It seems like list() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

40
        return new Pagination($total, $size, $prev, $page, $next, $last, yield $this->/** @scrutinizer ignore-call */ list());
Loading history...
Bug introduced by
$last of type double is incompatible with the type integer expected by parameter $last of Carno\Database\SQL\Pagin...gination::__construct(). ( Ignorable by Annotation )

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

40
        return new Pagination($total, $size, $prev, $page, $next, /** @scrutinizer ignore-type */ $last, yield $this->list());
Loading history...
41
    }
42
43
    /**
44
     * @param object $source
45
     * @return array
46
     */
47
    private function import(object $source) : array
48
    {
49
        $page = $size = null;
50
51
        if (method_exists($source, 'getPage')) {
52
            $page = (int)$source->getPage();
53
        }
54
55
        if (method_exists($source, 'getSize')) {
56
            $size = (int)$source->getSize();
57
        }
58
59
        return [$page ?: 1, $size ?: $this->pSize];
60
    }
61
}
62