Completed
Push — 23 ( 2cbb94...e876bd )
by Harald
11:26 queued 05:21
created

PaginatedCollection   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 18.6 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 16
loc 86
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 1
A current() 0 4 1
A key() 0 4 1
A next() 0 4 1
A rewind() 8 8 1
A valid() 0 9 4
A _getNextPage() 0 14 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Braintree;
3
4
use Iterator;
5
6
/**
7
 * Braintree PaginatedCollection
8
 * PaginatedCollection is a container object for paginated data
9
 *
10
 * retrieves and pages through large collections of results
11
 *
12
 * example:
13
 * <code>
14
 * $result = MerchantAccount::all();
15
 *
16
 * foreach($result as $merchantAccount) {
17
 *   print_r($merchantAccount->status);
18
 * }
19
 * </code>
20
 *
21
 * @package    Braintree
22
 * @subpackage Utility
23
 * @copyright  2016 Braintree, a division of PayPal, Inc.
24
 */
25
class PaginatedCollection implements Iterator
26
{
27
    private $_pager;
28
    private $_pageSize;
29
    private $_currentPage;
30
    private $_index;
31
    private $_totalItems;
32
    private $_items;
33
34
    /**
35
     * set up the paginated collection
36
     *
37
     * expects an array of an object and method to call on it
38
     *
39
     * @param array $pager
40
     */
41 View Code Duplication
    public function  __construct($pager)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        $this->_pager = $pager;
44
        $this->_pageSize = 0;
45
        $this->_currentPage = 0;
46
        $this->_totalItems = 0;
47
        $this->_index = 0;
48
    }
49
50
    /**
51
     * returns the current item when iterating with foreach
52
     */
53
    public function current()
54
    {
55
        return $this->_items[($this->_index % $this->_pageSize)];
56
    }
57
58
    public function key()
59
    {
60
        return null;
61
    }
62
63
    /**
64
     * advances to the next item in the collection when iterating with foreach
65
     */
66
    public function next()
67
    {
68
        ++$this->_index;
69
    }
70
71
    /**
72
     * rewinds the collection to the first item when iterating with foreach
73
     */
74 View Code Duplication
    public function rewind()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        $this->_index = 0;
77
        $this->_currentPage = 0;
78
        $this->_pageSize = 0;
79
        $this->_totalItems = 0;
80
        $this->_items = [];
81
    }
82
83
    /**
84
     * returns whether the current item is valid when iterating with foreach
85
     */
86
    public function valid()
87
    {
88
        if ($this->_currentPage == 0 || $this->_index % $this->_pageSize == 0 && $this->_index < $this->_totalItems)
89
        {
90
            $this->_getNextPage();
91
        }
92
93
        return $this->_index < $this->_totalItems;
94
    }
95
96
    private function _getNextPage()
97
    {
98
        $this->_currentPage++;
99
        $object = $this->_pager['object'];
100
        $method = $this->_pager['method'];
101
        $result = call_user_func(
102
            [$object, $method],
103
            $this->_currentPage
104
        );
105
106
        $this->_totalItems= $result->getTotalItems();
107
        $this->_pageSize = $result->getPageSize();
108
        $this->_items = $result->getCurrentPage();
109
    }
110
}
111
class_alias('Braintree\PaginatedCollection', 'Braintree_PaginatedCollection');
112