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 |
||
33 | class LazyResourceCollection implements IteratorAggregate, ResourceCollection |
||
34 | { |
||
35 | /** |
||
36 | * @var string[]|PuliResource[] |
||
37 | */ |
||
38 | private $resources; |
||
39 | |||
40 | /** |
||
41 | * @var ResourceRepository |
||
42 | */ |
||
43 | private $repo; |
||
44 | |||
45 | /** |
||
46 | * @var bool |
||
47 | */ |
||
48 | private $loaded = false; |
||
49 | |||
50 | /** |
||
51 | * Creates a new collection. |
||
52 | * |
||
53 | * @param ResourceRepository $repo The repository that will be used to load |
||
54 | * the resources. |
||
55 | * @param array $paths The paths of the resources which will be |
||
56 | * loaded into the collection. |
||
57 | */ |
||
58 | public function __construct(ResourceRepository $repo, array $paths) |
||
59 | { |
||
60 | $this->resources = $paths; |
||
61 | $this->repo = $repo; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Not supported. |
||
66 | * |
||
67 | * @param PuliResource $resource The resource to add. |
||
68 | * |
||
69 | * @throws BadMethodCallException The collection is read-only. |
||
70 | */ |
||
71 | public function add(PuliResource $resource) |
||
72 | { |
||
73 | throw new BadMethodCallException( |
||
74 | 'Lazy resource collections cannot be modified.' |
||
75 | ); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Not supported. |
||
80 | * |
||
81 | * @param int $key The collection key. |
||
82 | * @param PuliResource $resource The resource to add. |
||
83 | * |
||
84 | * @throws BadMethodCallException The collection is read-only. |
||
85 | */ |
||
86 | public function set($key, PuliResource $resource) |
||
87 | { |
||
88 | throw new BadMethodCallException( |
||
89 | 'Lazy resource collections cannot be modified.' |
||
90 | ); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | public function get($key) |
||
97 | { |
||
98 | if (!isset($this->resources[$key])) { |
||
99 | throw new OutOfBoundsException(sprintf( |
||
100 | 'The offset "%s" does not exist.', |
||
101 | $key |
||
102 | )); |
||
103 | } |
||
104 | |||
105 | if (!$this->resources[$key] instanceof PuliResource) { |
||
106 | $this->resources[$key] = $this->repo->get($this->resources[$key]); |
||
107 | } |
||
108 | |||
109 | return $this->resources[$key]; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Not supported. |
||
114 | * |
||
115 | * @param string $key The collection key to remove. |
||
116 | * |
||
117 | * @throws BadMethodCallException The collection is read-only. |
||
118 | */ |
||
119 | public function remove($key) |
||
120 | { |
||
121 | throw new BadMethodCallException( |
||
122 | 'Lazy resource collections cannot be modified.' |
||
123 | ); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | public function has($key) |
||
133 | |||
134 | /** |
||
135 | * Not supported. |
||
136 | * |
||
137 | * @throws BadMethodCallException The collection is read-only. |
||
138 | */ |
||
139 | public function clear() |
||
140 | { |
||
141 | throw new BadMethodCallException( |
||
142 | 'Lazy resource collections cannot be modified.' |
||
143 | ); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | public function keys() |
||
150 | { |
||
151 | if (!$this->loaded) { |
||
152 | $this->load(); |
||
153 | } |
||
154 | |||
155 | return array_keys($this->resources); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Not supported. |
||
160 | * |
||
161 | * @param PuliResource[]|Traversable $resources The resources to replace the |
||
162 | * collection contents with. |
||
163 | * |
||
164 | * @throws BadMethodCallException The collection is read-only. |
||
165 | */ |
||
166 | public function replace($resources) |
||
167 | { |
||
168 | throw new BadMethodCallException( |
||
169 | 'Lazy resource collections cannot be modified.' |
||
170 | ); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Not supported. |
||
175 | * |
||
176 | * @param PuliResource[]|Traversable $resources The resources to merge into |
||
177 | * the collection. |
||
178 | * |
||
179 | * @throws BadMethodCallException The collection is read-only. |
||
180 | */ |
||
181 | public function merge($resources) |
||
182 | { |
||
183 | throw new BadMethodCallException( |
||
184 | 'Lazy resource collections cannot be modified.' |
||
185 | ); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * {@inheritdoc} |
||
190 | */ |
||
191 | public function isEmpty() |
||
195 | |||
196 | /** |
||
197 | * {@inheritdoc} |
||
198 | */ |
||
199 | public function offsetExists($key) |
||
203 | |||
204 | /** |
||
205 | * {@inheritdoc} |
||
206 | */ |
||
207 | public function offsetGet($key) |
||
211 | |||
212 | /** |
||
213 | * Not supported. |
||
214 | * |
||
215 | * @param string $key The collection key to set. |
||
216 | * @param PuliResource $resource The resource to set. |
||
217 | * |
||
218 | * @throws BadMethodCallException The collection is read-only. |
||
219 | */ |
||
220 | public function offsetSet($key, $resource) |
||
221 | { |
||
222 | throw new BadMethodCallException( |
||
223 | 'Lazy resource collections cannot be modified.' |
||
224 | ); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Not supported. |
||
229 | * |
||
230 | * @param string $key The collection key to remove. |
||
231 | * |
||
232 | * @throws BadMethodCallException The collection is read-only. |
||
233 | */ |
||
234 | public function offsetUnset($key) |
||
235 | { |
||
236 | throw new BadMethodCallException( |
||
237 | 'Lazy resource collections cannot be modified.' |
||
238 | ); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * {@inheritdoc} |
||
243 | */ |
||
244 | View Code Duplication | public function getPaths() |
|
245 | { |
||
246 | if (!$this->loaded) { |
||
247 | $this->load(); |
||
248 | } |
||
249 | |||
250 | return array_map( |
||
251 | function (PuliResource $resource) { return $resource->getPath(); }, |
||
252 | $this->resources |
||
253 | ); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | View Code Duplication | public function getNames() |
|
260 | { |
||
261 | if (!$this->loaded) { |
||
262 | $this->load(); |
||
263 | } |
||
264 | |||
265 | return array_map( |
||
266 | function (PuliResource $resource) { return $resource->getName(); }, |
||
267 | $this->resources |
||
268 | ); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * {@inheritdoc} |
||
273 | */ |
||
274 | public function getIterator($mode = ResourceCollectionIterator::KEY_AS_CURSOR) |
||
275 | { |
||
276 | if (!$this->loaded) { |
||
277 | $this->load(); |
||
278 | } |
||
279 | |||
280 | return new ResourceCollectionIterator($this, $mode); |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * {@inheritdoc} |
||
285 | */ |
||
286 | public function toArray() |
||
287 | { |
||
288 | if (!$this->loaded) { |
||
289 | $this->load(); |
||
290 | } |
||
291 | |||
292 | return $this->resources; |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * {@inheritdoc} |
||
297 | */ |
||
298 | public function count() |
||
302 | |||
303 | /** |
||
304 | * Loads the complete collection. |
||
305 | */ |
||
306 | private function load() |
||
307 | { |
||
308 | foreach ($this->resources as $key => $resource) { |
||
309 | if (!$resource instanceof PuliResource) { |
||
310 | $this->resources[$key] = $this->repo->get($resource); |
||
311 | } |
||
312 | } |
||
313 | |||
314 | $this->loaded = true; |
||
315 | } |
||
316 | } |
||
317 |