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 |
||
23 | class DBResult implements Iterator, ArrayAccess, Countable{ |
||
24 | use Enumerable, Magic; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $array = []; |
||
30 | |||
31 | /** |
||
32 | * @var int |
||
33 | */ |
||
34 | protected $offset = 0; |
||
35 | |||
36 | /** |
||
37 | * DBResult constructor. |
||
38 | * |
||
39 | * @param \Traversable|\stdClass|array|null $data |
||
40 | * |
||
41 | * @throws \chillerlan\Database\DBException |
||
42 | */ |
||
43 | public function __construct($data = null){ |
||
61 | |||
62 | /** |
||
63 | * @param \chillerlan\Database\DBResult $DBResult |
||
64 | * |
||
65 | * @return \chillerlan\Database\DBResult |
||
66 | */ |
||
67 | public function __merge(DBResult $DBResult){ |
||
72 | |||
73 | |||
74 | /********* |
||
75 | * magic * |
||
76 | *********/ |
||
77 | |||
78 | /** |
||
79 | * @return int |
||
80 | */ |
||
81 | protected function magic_get_length():int{ |
||
84 | |||
85 | |||
86 | /*************** |
||
87 | * ArrayAccess * |
||
88 | ***************/ |
||
89 | |||
90 | /** |
||
91 | * @param int|string $offset |
||
92 | * |
||
93 | * @return bool |
||
94 | */ |
||
95 | public function offsetExists($offset):bool{ |
||
98 | |||
99 | /** |
||
100 | * @param int|string $offset |
||
101 | * |
||
102 | * @return \chillerlan\Database\DBResultRow|mixed|null |
||
103 | */ |
||
104 | public function offsetGet($offset){ |
||
107 | |||
108 | /** |
||
109 | * @param int|string $offset |
||
110 | * @param array $value |
||
111 | * |
||
112 | * @return void |
||
113 | */ |
||
114 | View Code Duplication | public function offsetSet($offset, $value){ |
|
124 | |||
125 | /** |
||
126 | * @param int|string $offset |
||
127 | * |
||
128 | * @return void |
||
129 | */ |
||
130 | public function offsetUnset($offset){ |
||
133 | |||
134 | |||
135 | /************* |
||
136 | * Countable * |
||
137 | *************/ |
||
138 | |||
139 | /** |
||
140 | * @return int |
||
141 | */ |
||
142 | public function count():int{ |
||
145 | |||
146 | |||
147 | /************ |
||
148 | * Iterator * |
||
149 | ************/ |
||
150 | |||
151 | /** |
||
152 | * @return \chillerlan\Database\DBResultRow|mixed |
||
153 | */ |
||
154 | public function current(){ |
||
157 | |||
158 | /** |
||
159 | * @return int |
||
160 | */ |
||
161 | public function key():int{ |
||
164 | |||
165 | /** |
||
166 | * @return bool |
||
167 | */ |
||
168 | public function valid():bool{ |
||
171 | |||
172 | /** |
||
173 | * @return void |
||
174 | */ |
||
175 | public function next(){ |
||
178 | |||
179 | /** |
||
180 | * @return void |
||
181 | */ |
||
182 | public function rewind(){ |
||
185 | |||
186 | } |
||
187 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.