Total Complexity | 49 |
Total Lines | 262 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like IteratorCollection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use IteratorCollection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class IteratorCollection extends Variable implements \ArrayAccess |
||
29 | { |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | private $elements; |
||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | |||
39 | //put your code here |
||
40 | |||
41 | /** |
||
42 | * IteratorCollection constructor. |
||
43 | * @param array $elements |
||
44 | */ |
||
45 | public function __construct(array $elements = array()) |
||
46 | { |
||
47 | |||
48 | parent::__construct($elements); |
||
49 | |||
50 | $this->elements = $elements; |
||
51 | } |
||
52 | |||
53 | protected function setElementsFromTrustedSource(array $elements) |
||
56 | } |
||
57 | |||
58 | |||
59 | /** |
||
60 | * @return array |
||
61 | */ |
||
62 | public function toArray(): array |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @return \ArrayObject |
||
69 | */ |
||
70 | public function getIterator() |
||
71 | { |
||
72 | |||
73 | return new \ArrayObject($this->elements); |
||
74 | } |
||
75 | |||
76 | public function count() |
||
79 | } |
||
80 | |||
81 | public function current() |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param $element |
||
88 | * Checks if a value exists in an array |
||
89 | * @see in_array |
||
90 | * @return bool |
||
91 | */ |
||
92 | public function contains($element) |
||
93 | { |
||
94 | return in_array($element, $this->elements, true); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @return mixed |
||
99 | */ |
||
100 | public function next() |
||
103 | } |
||
104 | |||
105 | public function last() |
||
106 | { |
||
107 | return end($this->elements); |
||
108 | } |
||
109 | |||
110 | public function first() |
||
111 | { |
||
112 | return reset($this->elements); |
||
113 | } |
||
114 | |||
115 | public function key() |
||
116 | { |
||
117 | return key($this->elements); |
||
118 | } |
||
119 | |||
120 | public function valid() |
||
121 | { |
||
122 | return $this->offsetExists($this->elements); |
||
123 | |||
124 | } |
||
125 | |||
126 | public function offsetGet($offset) |
||
127 | { |
||
128 | return $this->get($offset); |
||
129 | } |
||
130 | |||
131 | public function offsetSet($offset, $value) |
||
132 | { |
||
133 | if (!isset($offset)) { |
||
134 | $this->set($offset, $value); |
||
135 | } |
||
136 | $this->set($offset, $value); |
||
137 | } |
||
138 | |||
139 | public function offsetUnset($offset) |
||
140 | { |
||
141 | $this->remove($offset); |
||
142 | } |
||
143 | |||
144 | public function containsKey($key) |
||
145 | { |
||
146 | return isset($this->elements[$key]) || array_key_exists($key, $this->elements); |
||
147 | } |
||
148 | |||
149 | public function offsetExists($offset) |
||
150 | { |
||
151 | return $this->containsKey($offset); |
||
152 | } |
||
153 | |||
154 | public function remove($key) |
||
155 | { |
||
156 | if (!isset($this->elements[$key]) && !array_key_exists($key, $this->elements)) { |
||
157 | return false; |
||
158 | } else { |
||
159 | $removed = $this->elements[$key]; |
||
160 | unset($this->elements[$key]); |
||
161 | |||
162 | return $removed; |
||
163 | } |
||
164 | } |
||
165 | |||
166 | public function removeElement($element) |
||
167 | { |
||
168 | $key = array_search($element, $this->elements, true); |
||
169 | if (false === $key) { |
||
170 | return false; |
||
171 | } |
||
172 | unset($this->elements[$key]); |
||
173 | return true; |
||
174 | } |
||
175 | |||
176 | public function add($value) |
||
177 | { |
||
178 | $this->elements[] = $value; |
||
179 | |||
180 | return $this; |
||
181 | } |
||
182 | |||
183 | public function set($key, $value) |
||
184 | { |
||
185 | $this->elements[$key] = $value; |
||
186 | |||
187 | return true; |
||
188 | } |
||
189 | |||
190 | public function ksort() |
||
191 | { |
||
192 | |||
193 | return ksort($this->elements); |
||
194 | } |
||
195 | |||
196 | public function natSort() |
||
197 | { |
||
198 | return natsort($this->elements); |
||
199 | } |
||
200 | |||
201 | public function natcasesort() |
||
202 | { |
||
203 | return natcasesort($this->elements); |
||
204 | } |
||
205 | |||
206 | public function exists(Closure $p) |
||
207 | { |
||
208 | foreach ($this->elements as $key => $element) { |
||
209 | if ($p($key, $element)) { |
||
210 | return true; |
||
211 | } |
||
212 | } |
||
213 | return false; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @return int|string |
||
218 | * return the position of the element |
||
219 | */ |
||
220 | public function indexOf($element) |
||
221 | { |
||
222 | return array_search($element, $this->elements); |
||
223 | } |
||
224 | |||
225 | public function isEmpty() |
||
226 | { |
||
227 | return empty($this->elements); |
||
228 | } |
||
229 | |||
230 | public function getValues() |
||
234 | } |
||
235 | |||
236 | public function getKeys() |
||
237 | { |
||
238 | return array_keys($this->elements); |
||
239 | } |
||
240 | |||
241 | public function get($key) |
||
242 | { |
||
243 | return isset($this->elements[$key]) ? $this->elements[$key] : null; |
||
244 | } |
||
245 | |||
246 | |||
247 | public function slice($start, $end) |
||
273 | |||
274 | } |
||
275 | |||
276 | public function reverse() |
||
284 | |||
285 | } |
||
286 | |||
287 | public function find($value) |
||
288 | { |
||
289 | $this->get($value); |
||
290 | } |
||
291 | |||
292 | |||
293 | } |
||
294 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.