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 |
||
55 | class ResourceCollectionIterator implements RecursiveResourceIterator |
||
56 | { |
||
57 | /** |
||
58 | * Return {@link PuliResource} instances as values. |
||
59 | */ |
||
60 | const CURRENT_AS_RESOURCE = 1; |
||
61 | |||
62 | /** |
||
63 | * Return the paths of the resources as values. |
||
64 | */ |
||
65 | const CURRENT_AS_PATH = 2; |
||
66 | |||
67 | /** |
||
68 | * Return the names of the resources as values. |
||
69 | */ |
||
70 | const CURRENT_AS_NAME = 4; |
||
71 | |||
72 | /** |
||
73 | * Return the paths of the resources as keys. |
||
74 | */ |
||
75 | const KEY_AS_PATH = 64; |
||
76 | |||
77 | /** |
||
78 | * Return the collection keys as keys. |
||
79 | * |
||
80 | * Attention: Don't use this mode when iterating recursively, as PHP's |
||
81 | * {@link RecursiveIteratorIterator} skips inner nodes then. |
||
82 | */ |
||
83 | const KEY_AS_CURSOR = 128; |
||
84 | |||
85 | /** |
||
86 | * @var PuliResource[] |
||
87 | */ |
||
88 | protected $resources; |
||
89 | |||
90 | /** |
||
91 | * @var int |
||
92 | */ |
||
93 | protected $mode; |
||
94 | |||
95 | /** |
||
96 | * Creates a new iterator. |
||
97 | * |
||
98 | * The following constants can be used to configure the values returned by |
||
99 | * the iterator: |
||
100 | * |
||
101 | * * {@link CURRENT_AS_RESOURCE}: The {@link PuliResource} objects are |
||
102 | * returned as values; |
||
103 | * * {@link CURRENT_AS_PATH}: The resource paths are returned as values; |
||
104 | * * {@link CURRENT_AS_NAME}: The resource names are returned as values. |
||
105 | * |
||
106 | * The following constants can be used to configure the keys returned by |
||
107 | * the iterator: |
||
108 | * |
||
109 | * * {@link KEY_AS_CURSOR}: The collection keys are returned as keys; |
||
110 | * * {@link KEY_AS_PATH}: The resource paths are returned as keys. |
||
111 | * |
||
112 | * By default, the mode `KEY_AS_PATH | CURRENT_AS_RESOURCE` is used. |
||
113 | * |
||
114 | * @param ResourceCollection $resources The resources to iterate. |
||
115 | * @param int|null $mode A bitwise combination of the mode |
||
116 | * constants. |
||
117 | */ |
||
118 | 643 | public function __construct(ResourceCollection $resources, $mode = null) |
|
131 | |||
132 | /** |
||
133 | * Returns the current value of the iterator. |
||
134 | * |
||
135 | * @return PuliResource|string The current value as configured in |
||
136 | * {@link __construct}. |
||
137 | */ |
||
138 | 360 | public function current() |
|
150 | |||
151 | /** |
||
152 | * Advances the iterator to the next position. |
||
153 | */ |
||
154 | 360 | public function next() |
|
158 | |||
159 | /** |
||
160 | * Returns the current key of the iterator. |
||
161 | * |
||
162 | * @return int|string|null The current key as configured in |
||
163 | * {@link __construct} or `null` if the cursor |
||
164 | * is behind the last element. |
||
165 | */ |
||
166 | 118 | public function key() |
|
178 | |||
179 | /** |
||
180 | * Returns whether the iterator points to a valid key. |
||
181 | * |
||
182 | * @return bool Whether the iterator position is valid. |
||
183 | */ |
||
184 | 642 | public function valid() |
|
188 | |||
189 | /** |
||
190 | * Rewinds the iterator to the first entry. |
||
191 | */ |
||
192 | 641 | public function rewind() |
|
196 | |||
197 | /** |
||
198 | * Returns whether the iterator can be applied recursively over the |
||
199 | * current element. |
||
200 | * |
||
201 | * @return bool Whether the current element can be iterated recursively. |
||
202 | */ |
||
203 | 9 | public function hasChildren() |
|
207 | |||
208 | /** |
||
209 | * Returns the iterator for the children of the current element. |
||
210 | * |
||
211 | * @return static Returns an instance of this class for the children of |
||
212 | * the current element. |
||
213 | */ |
||
214 | 9 | public function getChildren() |
|
218 | |||
219 | /** |
||
220 | * {@inheritdoc} |
||
221 | */ |
||
222 | 5 | public function getCurrentResource() |
|
226 | } |
||
227 |