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() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Returns scroll id. |
||
| 92 | * |
||
| 93 | * @link http://php.net/manual/en/iterator.key.php |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function key() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Returns true if current result set contains at least one hit. |
||
| 104 | * |
||
| 105 | * @link http://php.net/manual/en/iterator.valid.php |
||
| 106 | * |
||
| 107 | * @return bool |
||
| 108 | */ |
||
| 109 | public function valid() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Initial scroll search. |
||
| 116 | * |
||
| 117 | * @link http://php.net/manual/en/iterator.rewind.php |
||
| 118 | */ |
||
| 119 | public function rewind() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Cleares the search context on ES and marks this Scroll instance as finished. |
||
| 137 | */ |
||
| 138 | public function clear() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Prepares Scroll for next request. |
||
| 155 | * |
||
| 156 | * @param ResultSet $resultSet |
||
| 157 | */ |
||
| 158 | protected function _setScrollId(ResultSet $resultSet) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Save all search options manipulated by Scroll. |
||
| 171 | */ |
||
| 172 | protected function _saveOptions() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Revert search options to previously saved state. |
||
| 185 | */ |
||
| 186 | protected function _revertOptions() |
||
| 191 | } |
||
| 192 |
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
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. 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.