Complex classes like ElggBatch 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ElggBatch, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
57 | class ElggBatch |
||
58 | implements \Iterator { |
||
59 | |||
60 | /** |
||
61 | * The objects to interator over. |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | private $results = array(); |
||
66 | |||
67 | /** |
||
68 | * The function used to get results. |
||
69 | * |
||
70 | * @var mixed A string, array, or closure, or lamda function |
||
71 | */ |
||
72 | private $getter = null; |
||
73 | |||
74 | /** |
||
75 | * The number of results to grab at a time. |
||
76 | * |
||
77 | * @var int |
||
78 | */ |
||
79 | private $chunkSize = 25; |
||
80 | |||
81 | /** |
||
82 | * A callback function to pass results through. |
||
83 | * |
||
84 | * @var mixed A string, array, or closure, or lamda function |
||
85 | */ |
||
86 | private $callback = null; |
||
87 | |||
88 | /** |
||
89 | * Start after this many results. |
||
90 | * |
||
91 | * @var int |
||
92 | */ |
||
93 | private $offset = 0; |
||
94 | |||
95 | /** |
||
96 | * Stop after this many results. |
||
97 | * |
||
98 | * @var int |
||
99 | */ |
||
100 | private $limit = 0; |
||
101 | |||
102 | /** |
||
103 | * Number of processed results. |
||
104 | * |
||
105 | * @var int |
||
106 | */ |
||
107 | private $retrievedResults = 0; |
||
108 | |||
109 | /** |
||
110 | * The index of the current result within the current chunk |
||
111 | * |
||
112 | * @var int |
||
113 | */ |
||
114 | private $resultIndex = 0; |
||
115 | |||
116 | /** |
||
117 | * The index of the current chunk |
||
118 | * |
||
119 | * @var int |
||
120 | */ |
||
121 | private $chunkIndex = 0; |
||
122 | |||
123 | /** |
||
124 | * The number of results iterated through |
||
125 | * |
||
126 | * @var int |
||
127 | */ |
||
128 | private $processedResults = 0; |
||
129 | |||
130 | /** |
||
131 | * Is the getter a valid callback |
||
132 | * |
||
133 | * @var bool |
||
134 | */ |
||
135 | private $validGetter = null; |
||
136 | |||
137 | /** |
||
138 | * The result of running all entities through the callback function. |
||
139 | * |
||
140 | * @var mixed |
||
141 | */ |
||
142 | public $callbackResult = null; |
||
143 | |||
144 | /** |
||
145 | * If false, offset will not be incremented. This is used for callbacks/loops that delete. |
||
146 | * |
||
147 | * @var bool |
||
148 | */ |
||
149 | private $incrementOffset = true; |
||
150 | |||
151 | /** |
||
152 | * Entities that could not be instantiated during a fetch |
||
153 | * |
||
154 | * @var \stdClass[] |
||
155 | */ |
||
156 | private $incompleteEntities = array(); |
||
157 | |||
158 | /** |
||
159 | * Total number of incomplete entities fetched |
||
160 | * |
||
161 | * @var int |
||
162 | */ |
||
163 | private $totalIncompletes = 0; |
||
164 | |||
165 | /** |
||
166 | * Batches operations on any elgg_get_*() or compatible function that supports |
||
167 | * an options array. |
||
168 | * |
||
169 | * Instead of returning all objects in memory, it goes through $chunk_size |
||
170 | * objects, then requests more from the server. This avoids OOM errors. |
||
171 | * |
||
172 | * @param string $getter The function used to get objects. Usually |
||
173 | * an elgg_get_*() function, but can be any valid PHP callback. |
||
174 | * @param array $options The options array to pass to the getter function. If limit is |
||
175 | * not set, 10 is used as the default. In most cases that is not |
||
176 | * what you want. |
||
177 | * @param mixed $callback An optional callback function that all results will be passed |
||
178 | * to upon load. The callback needs to accept $result, $getter, |
||
179 | * $options. |
||
180 | * @param int $chunk_size The number of entities to pull in before requesting more. |
||
181 | * You have to balance this between running out of memory in PHP |
||
182 | * and hitting the db server too often. |
||
183 | * @param bool $inc_offset Increment the offset on each fetch. This must be false for |
||
184 | * callbacks that delete rows. You can set this after the |
||
185 | * object is created with {@link \ElggBatch::setIncrementOffset()}. |
||
186 | */ |
||
187 | public function __construct($getter, $options, $callback = null, $chunk_size = 25, |
||
232 | |||
233 | /** |
||
234 | * Tell the process that an entity was incomplete during a fetch |
||
235 | * |
||
236 | * @param \stdClass $row |
||
237 | * |
||
238 | * @access private |
||
239 | */ |
||
240 | public function reportIncompleteEntity(\stdClass $row) { |
||
243 | |||
244 | /** |
||
245 | * Fetches the next chunk of results |
||
246 | * |
||
247 | * @return bool |
||
248 | */ |
||
249 | private function getNextResultsChunk() { |
||
334 | |||
335 | /** |
||
336 | * Increment the offset from the original options array? Setting to |
||
337 | * false is required for callbacks that delete rows. |
||
338 | * |
||
339 | * @param bool $increment Set to false when deleting data |
||
340 | * @return void |
||
341 | */ |
||
342 | public function setIncrementOffset($increment = true) { |
||
345 | |||
346 | /** |
||
347 | * Implements Iterator |
||
348 | */ |
||
349 | |||
350 | /** |
||
351 | * PHP Iterator Interface |
||
352 | * |
||
353 | * @see Iterator::rewind() |
||
354 | * @return void |
||
355 | */ |
||
356 | public function rewind() { |
||
367 | |||
368 | /** |
||
369 | * PHP Iterator Interface |
||
370 | * |
||
371 | * @see Iterator::current() |
||
372 | * @return mixed |
||
373 | */ |
||
374 | public function current() { |
||
377 | |||
378 | /** |
||
379 | * PHP Iterator Interface |
||
380 | * |
||
381 | * @see Iterator::key() |
||
382 | * @return int |
||
383 | */ |
||
384 | public function key() { |
||
387 | |||
388 | /** |
||
389 | * PHP Iterator Interface |
||
390 | * |
||
391 | * @see Iterator::next() |
||
392 | * @return mixed |
||
393 | */ |
||
394 | public function next() { |
||
419 | |||
420 | /** |
||
421 | * PHP Iterator Interface |
||
422 | * |
||
423 | * @see Iterator::valid() |
||
424 | * @return bool |
||
425 | */ |
||
426 | public function valid() { |
||
433 | } |
||
434 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: