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 |
||
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') |
||
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() |
||
67 | |||
68 | /** |
||
69 | * Next scroll search. |
||
70 | * |
||
71 | * @link http://php.net/manual/en/iterator.next.php |
||
72 | */ |
||
73 | public function next() |
||
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() |
||
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() |
||
115 | |||
116 | /** |
||
117 | * Initial scroll search. |
||
118 | * |
||
119 | * @link http://php.net/manual/en/iterator.rewind.php |
||
120 | */ |
||
121 | public function rewind() |
||
136 | |||
137 | /** |
||
138 | * Prepares Scroll for next request. |
||
139 | * |
||
140 | * @param ResultSet $resultSet |
||
141 | */ |
||
142 | protected function _setScrollId(ResultSet $resultSet) |
||
152 | |||
153 | /** |
||
154 | * Save all search options manipulated by Scroll. |
||
155 | */ |
||
156 | protected function _saveOptions() |
||
166 | |||
167 | /** |
||
168 | * Revert search options to previously saved state. |
||
169 | */ |
||
170 | protected function _revertOptions() |
||
175 | } |
||
176 |
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 theid
property of an instance of theAccount
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.