Conditions | 18 |
Paths | 12 |
Total Lines | 103 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
204 | public function transform($input, & $output = '') |
||
205 | { |
||
206 | // Get all defined handlers |
||
207 | $handlers = \samsonphp\event\Event::listeners(); |
||
208 | |||
209 | // Iterate all event fire calls |
||
210 | foreach ($this->fires as $id => $data) { |
||
211 | |||
212 | // Collection of actual event handler call for replacement |
||
213 | $code = array(); |
||
214 | |||
215 | // Set pointer to event subscriptions collection |
||
216 | if (isset($this->subscriptions[$id])) { |
||
217 | // Iterate event subscriptions |
||
218 | foreach ($this->subscriptions[$id] as &$event) { |
||
219 | $this->log('Analyzing event subscription[##]', $id); |
||
220 | // If subscriber callback is object method |
||
221 | if (isset($event['object'])) { |
||
222 | $eventHandlers = & $handlers[$id]; |
||
223 | if (isset($eventHandlers)) { |
||
224 | // Iterate all handlers |
||
225 | foreach ($eventHandlers as $handler) { |
||
226 | |||
227 | //trace($handler); |
||
228 | $call = ''; |
||
229 | |||
230 | // Get pointer to object |
||
231 | if (is_scalar($handler[0][0])) { |
||
232 | $object = $handler[0][0]; |
||
233 | } else { |
||
234 | $object = & $handler[0][0]; |
||
235 | } |
||
236 | |||
237 | // TODO: Not existing dynamic handlers what was excluded from compressed code |
||
238 | |||
239 | if(is_object($object) && $object instanceof \samsonframework\core\ViewInterface && $object instanceof \samsonframework\core\CompressInterface) { |
||
240 | // Build object method call |
||
241 | $call = 'm("' . $object->id() . '")->' . $event['method'] . '('; |
||
242 | $this->log(' - Replacing event fire[##] with object function call [##]', $id, $call); |
||
243 | } elseif (strpos($event['object'], '(') !== false) { // Function |
||
244 | // Build object method call |
||
245 | $call = $event['object'].'->' . $event['method'] . '('; |
||
246 | } elseif (is_string($object) && class_exists($object, false)) { // Static class |
||
247 | //trace($event['object'].'-'.$object); |
||
248 | |||
249 | // Build object method call |
||
250 | $call = $event['object'].'::' . $event['method'] . '('; |
||
251 | } |
||
252 | |||
253 | // TODO: Define what to do with other classes, only functions supported |
||
254 | // If we have found correct object |
||
255 | if (isset($call{0})) { |
||
256 | // Event fire passes parameters |
||
257 | if (is_array($data['params'])) { |
||
258 | $call .= implode(', ', $data['params']); |
||
259 | } |
||
260 | |||
261 | // Gather object calls |
||
262 | $code[] = $call .');'; |
||
263 | } else { |
||
264 | $this->log(' - Cannot replace event fire[##] with [##] - [##]', $id, $event['object'], $event['method']); |
||
265 | } |
||
266 | |||
267 | } |
||
268 | } |
||
269 | } else { // Global function |
||
270 | if (strpos($event['method'], '$') === false) { |
||
271 | $call = $event['method'] . '(' . implode(', ', $data['params']) . ');'; |
||
272 | $code[] = $call; |
||
273 | $this->log(' - Replacing event fire[##] with function call [##]', $id, $call); |
||
274 | } else { |
||
275 | $this->log('Cannot replace event fire method with [##] - variables not supported', $event['method']); |
||
276 | } |
||
277 | } |
||
278 | } |
||
279 | |||
280 | // Remove duplicates |
||
281 | $code = array_unique($code); |
||
282 | |||
283 | // Replace Event::fire call with actual handlers |
||
284 | $input = str_replace($data['source'], implode("\n", $code), $input); |
||
285 | |||
286 | // Logging changes in code |
||
287 | foreach ($code as $replace) { |
||
288 | $this->log('Replacing [##] with [##]', $data['source'], $replace); |
||
289 | } |
||
290 | |||
291 | } else { // There is no subscriptions to this event fire |
||
292 | // Remove Event::fire call without subscriptions |
||
293 | $input = str_replace($data['source'], '', $input); |
||
294 | |||
295 | $this->log('Removing event firing [##] as it has no subscriptions', $data['source']); |
||
296 | } |
||
297 | } |
||
298 | |||
299 | // Remove all subscriptions from code |
||
300 | $input = $this->removeSubscriptionCalls($this->subscriptions, $input); |
||
301 | |||
302 | // Copy output |
||
303 | $output = $input; |
||
304 | |||
305 | return true; |
||
306 | } |
||
307 | |||
320 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.