Completed
Push — master ( 4ebd3a...ac7094 )
by Nicolas
03:03
created

Scroll   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 165
Duplicated Lines 3.64 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 3
dl 6
loc 165
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A current() 0 4 1
A next() 0 18 2
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 15 1
B _setScrollId() 0 10 5
A _saveOptions() 6 10 3
A _revertOptions() 0 5 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 Elastica;
3
4
/**
5
 * Scroll Iterator.
6
 *
7
 * @author Manuel Andreo Garcia <[email protected]>
8
 *
9
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html
10
 */
11
class Scroll implements \Iterator
12
{
13
    /**
14
     * @var string
15
     */
16
    public $expiryTime;
17
18
    /**
19
     * @var Search
20
     */
21
    protected $_search;
22
23
    /**
24
     * @var null|string
25
     */
26
    protected $_nextScrollId;
27
28
    /**
29
     * @var null|ResultSet
30
     */
31
    protected $_currentResultSet;
32
33
    /**
34
     * 0: scroll<br>
35
     * 1: scroll id.
36
     *
37
     * @var array
38
     */
39
    protected $_options = [null, null];
40
41
    private $totalPages = 0;
42
    private $currentPage = 0;
43
44
    /**
45
     * Constructor.
46
     *
47
     * @param Search $search
48
     * @param string $expiryTime
49
     */
50
    public function __construct(Search $search, $expiryTime = '1m')
51
    {
52
        $this->_search = $search;
53
        $this->expiryTime = $expiryTime;
54
    }
55
56
    /**
57
     * Returns current result set.
58
     *
59
     * @link http://php.net/manual/en/iterator.current.php
60
     *
61
     * @return ResultSet
62
     */
63
    public function current()
64
    {
65
        return $this->_currentResultSet;
66
    }
67
68
    /**
69
     * Next scroll search.
70
     *
71
     * @link http://php.net/manual/en/iterator.next.php
72
     */
73
    public function next()
74
    {
75
        if ($this->currentPage < $this->totalPages) {
76
            $this->_saveOptions();
77
78
            $this->_search->setOption(Search::OPTION_SCROLL, $this->expiryTime);
79
            $this->_search->setOption(Search::OPTION_SCROLL_ID, $this->_nextScrollId);
80
81
            $this->_setScrollId($this->_search->search());
82
83
            $this->_revertOptions();
84
        } else {
85
            // If there are no pages left, we do not need to query ES.
86
            // Reset scroll ID so valid() returns false.
87
            $this->_nextScrollId = null;
88
            $this->_currentResultSet = null;
89
        }
90
    }
91
92
    /**
93
     * Returns scroll id.
94
     *
95
     * @link http://php.net/manual/en/iterator.key.php
96
     *
97
     * @return string
98
     */
99
    public function key()
100
    {
101
        return $this->_nextScrollId;
102
    }
103
104
    /**
105
     * Returns true if current result set contains at least one hit.
106
     *
107
     * @link http://php.net/manual/en/iterator.valid.php
108
     *
109
     * @return bool
110
     */
111
    public function valid()
112
    {
113
        return $this->_nextScrollId !== null;
114
    }
115
116
    /**
117
     * Initial scroll search.
118
     *
119
     * @link http://php.net/manual/en/iterator.rewind.php
120
     */
121
    public function rewind()
122
    {
123
        // reset state
124
        $this->_options = [null, null];
125
        $this->currentPage = 0;
126
127
        // initial search
128
        $this->_saveOptions();
129
130
        $this->_search->setOption(Search::OPTION_SCROLL, $this->expiryTime);
131
        $this->_search->setOption(Search::OPTION_SCROLL_ID, null);
132
        $this->_setScrollId($this->_search->search());
133
134
        $this->_revertOptions();
135
    }
136
137
    /**
138
     * Prepares Scroll for next request.
139
     *
140
     * @param ResultSet $resultSet
141
     */
142
    protected function _setScrollId(ResultSet $resultSet)
143
    {
144
        if ($this->currentPage === 0) {
145
            $this->totalPages = $resultSet->count() > 0 ? ceil($resultSet->getTotalHits() / $resultSet->count()) : 0;
0 ignored issues
show
Documentation Bug introduced by
It seems like $resultSet->count() > 0 ...resultSet->count()) : 0 can also be of type double. However, the property $totalPages is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
146
        }
147
148
        $this->_currentResultSet = $resultSet;
149
        ++$this->currentPage;
150
        $this->_nextScrollId = $resultSet->getResponse()->isOk() && $resultSet->count() > 0 ? $resultSet->getResponse()->getScrollId() : null;
151
    }
152
153
    /**
154
     * Save all search options manipulated by Scroll.
155
     */
156
    protected function _saveOptions()
157
    {
158 View Code Duplication
        if ($this->_search->hasOption(Search::OPTION_SCROLL)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
159
            $this->_options[0] = $this->_search->getOption(Search::OPTION_SCROLL);
160
        }
161
162 View Code Duplication
        if ($this->_search->hasOption(Search::OPTION_SCROLL_ID)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
163
            $this->_options[1] = $this->_search->getOption(Search::OPTION_SCROLL_ID);
164
        }
165
    }
166
167
    /**
168
     * Revert search options to previously saved state.
169
     */
170
    protected function _revertOptions()
171
    {
172
        $this->_search->setOption(Search::OPTION_SCROLL, $this->_options[0]);
173
        $this->_search->setOption(Search::OPTION_SCROLL_ID, $this->_options[1]);
174
    }
175
}
176