Passed
Push — master ( f58bd0...03116c )
by Rougin
01:39
created

PaginateTrait::offset()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 8
nop 2
crap 4
1
<?php
2
3
namespace Rougin\Wildfire\Traits;
4
5
/**
6
 * Paginate Trait
7
 *
8
 * @property \CI_Config     $config
9
 * @property \CI_Input      $input
10
 * @property \CI_Loader     $load
11
 * @property \CI_Pagination $pagination
12
 * @property \CI_URI        $uri
13
 *
14
 * @package Wildfire
15
 * @author  Rougin Royce Gutib <[email protected]>
16
 */
17
trait PaginateTrait
18
{
19
    /**
20
     * Limits the data based on given configuration and generates pagination links.
21
     *
22
     * @param  integer $page
23
     * @param  integer $total
24
     * @param  array   $config
25
     * @return array
26
     */
27 3
    public function paginate($page, $total, $config = array())
28
    {
29 3
        $this->load->library('pagination');
30
31 3
        $pagination = $this->pagination;
32
33 3
        $config = $this->prepare((array) $config);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $config is correct as $this->prepare((array)$config) targeting Rougin\Wildfire\Traits\PaginateTrait::prepare() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
34
35 3
        $config['per_page'] = (integer) $page;
36
37 3
        $config['total_rows'] = (integer) $total;
38
39 3
        $offset = $this->offset($page, $config);
0 ignored issues
show
Bug introduced by
$config of type void is incompatible with the type array expected by parameter $config of Rougin\Wildfire\Traits\PaginateTrait::offset(). ( Ignorable by Annotation )

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

39
        $offset = $this->offset($page, /** @scrutinizer ignore-type */ $config);
Loading history...
40
41 3
        $pagination->initialize($config);
0 ignored issues
show
Bug introduced by
$config of type void is incompatible with the type array expected by parameter $params of CI_Pagination::initialize(). ( Ignorable by Annotation )

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

41
        $pagination->initialize(/** @scrutinizer ignore-type */ $config);
Loading history...
42
43 3
        $links = $pagination->create_links();
44
45 3
        return array($offset, $links);
46
    }
47
48
    /**
49
     * Returns the offset from the defined configuration.
50
     *
51
     * @param  integer $page
52
     * @param  array   $config
53
     * @return integer
54
     */
55 3
    protected function offset($page, array $config)
56
    {
57 3
        $offset = $this->uri->segment($config['uri_segment']);
58
59 3
        if ($config['page_query_string'] === true) {
60 3
            $segment = $config['query_string_segment'];
61
62 3
            $offset = $this->input->get($segment);
63 3
        }
64
65 3
        $numbers = $config['use_page_numbers'] && $offset !== 0;
66
67 3
        return $numbers ? ($page * $offset) - $page : $offset;
68
    }
69
70
    /**
71
     * Retrieves configuration from pagination.php.
72
     * If not available, will based on given and default data.
73
     *
74
     * @param  array $config
75
     * @return void
76
     */
77 3
    protected function prepare(array $config)
78
    {
79 3
        $this->load->helper('url');
80
81 3
        $items = array('base_url' => current_url());
82
83 3
        $items['page_query_string'] = false;
84 3
        $items['query_string_segment'] = 'per_page';
85 3
        $items['uri_segment'] = 3;
86 3
        $items['use_page_numbers'] = false;
87
88 3
        foreach ((array) $items as $key => $value) {
89 3
            if ($this->config->item($key) !== null) {
90 3
                $config[$key] = $this->config->item($key);
91 3
            } elseif (! isset($config[$key])) {
92 3
                $config[$key] = $value;
93 3
            }
94 3
        }
95
96 3
        return $config;
97
    }
98
}
99