Conditions | 53 |
Paths | 10584 |
Total Lines | 354 |
Lines | 20 |
Ratio | 5.65 % |
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 |
||
192 | private function generateBehatTest() |
||
193 | { |
||
194 | $configDir = $this->bundle->getPath() . '/Resources/config'; |
||
195 | |||
196 | // Get the context names for each section config file |
||
197 | $sectionInfo = array(); |
||
198 | $dir = $configDir . '/pageparts/'; |
||
199 | foreach ($this->sections as $section) { |
||
200 | $data = Yaml::parse(file_get_contents($dir . $section)); |
||
201 | $sectionInfo[basename($section, '.yml')] = array('context' => $data['context'], 'pagetempates' => array()); |
||
202 | } |
||
203 | |||
204 | /* |
||
205 | Example $sectionInfo contents: |
||
206 | Array |
||
207 | ( |
||
208 | [main] => Array |
||
209 | ( |
||
210 | [context] => main |
||
211 | [pagetempates] => Array |
||
212 | ( |
||
213 | ) |
||
214 | ) |
||
215 | ) |
||
216 | */ |
||
217 | |||
218 | // Get a list of page templates that use this context |
||
219 | $templateFinder = new Finder(); |
||
220 | $templateFinder->files()->in($configDir . '/pagetemplates')->name('*.yml'); |
||
221 | |||
222 | $contextTemplates = array(); |
||
223 | foreach ($templateFinder as $templatePath) { |
||
224 | $parts = explode('/', $templatePath); |
||
225 | $fileName = basename($parts[count($parts) - 1], '.yml'); |
||
226 | |||
227 | $data = Yaml::parse(file_get_contents($templatePath)); |
||
228 | $templateName = $data['name']; |
||
229 | if (array_key_exists('rows', $data) && is_array($data['rows'])) { |
||
230 | foreach ($data['rows'] as $row) { |
||
231 | if (is_array($row) && array_key_exists('regions', $row) && is_array($row['regions'])) { |
||
232 | foreach ($row['regions'] as $region) { |
||
233 | $contextTemplates[$region['name']][$fileName] = $templateName; |
||
234 | } |
||
235 | } |
||
236 | } |
||
237 | } |
||
238 | } |
||
239 | |||
240 | /* |
||
241 | Example $contextTemplates contents: |
||
242 | Array |
||
243 | ( |
||
244 | [main] => Array |
||
245 | ( |
||
246 | [full-width-page] => Full width page |
||
247 | [homepage] => Home page |
||
248 | [sidebar-page] => Page with left sidebar |
||
249 | ) |
||
250 | [top] => Array |
||
251 | ( |
||
252 | [homepage] => Home page |
||
253 | ) |
||
254 | [sidebar] => Array |
||
255 | ( |
||
256 | [homepage] => Home page |
||
257 | [sidebar-page] => Page with left sidebar |
||
258 | ) |
||
259 | ) |
||
260 | */ |
||
261 | |||
262 | // Link the page templates to the sections |
||
263 | foreach ($sectionInfo as $fileName => $info) { |
||
264 | $context = $info['context']; |
||
265 | if (array_key_exists($context, $contextTemplates)) { |
||
266 | $sectionInfo[$fileName]['pagetempates'] = $contextTemplates[$context]; |
||
267 | } |
||
268 | } |
||
269 | |||
270 | /* |
||
271 | Example $sectionInfo contents: |
||
272 | Array |
||
273 | ( |
||
274 | [main] => Array |
||
275 | ( |
||
276 | [context] => main |
||
277 | [pagetempates] => Array |
||
278 | ( |
||
279 | [full-width-page] => Full width page |
||
280 | [homepage] => Home page |
||
281 | [sidebar-page] => Page with left sidebar |
||
282 | ) |
||
283 | |||
284 | ) |
||
285 | |||
286 | ) |
||
287 | */ |
||
288 | |||
289 | $folder = $this->registry->getRepository('KunstmaanMediaBundle:Folder')->findOneBy(array('rel' => 'image')); |
||
290 | $images = $this->registry->getRepository('KunstmaanMediaBundle:Media')->findBy( |
||
291 | array('folder' => $folder, 'deleted' => false), |
||
292 | array(), |
||
293 | 2 |
||
294 | ); |
||
295 | |||
296 | // Get all the available pages |
||
297 | $finder = new Finder(); |
||
298 | $finder->files()->in($this->bundle->getPath() . '/Entity/Pages')->name('*.php'); |
||
299 | |||
300 | $pages = array(); |
||
301 | foreach ($finder as $pageFile) { |
||
302 | $parts = explode('/', $pageFile); |
||
303 | $className = basename($parts[count($parts) - 1], '.php'); |
||
304 | |||
305 | $contents = file_get_contents($pageFile); |
||
306 | if (strpos($contents, 'abstract class') === false && strpos($contents, 'interface ') === false && strpos($contents, 'trait ') === false) { |
||
307 | $classNamespace = '\\' . $this->bundle->getNamespace() . '\Entity\Pages\\' . $className; |
||
308 | $entity = new $classNamespace(); |
||
309 | |||
310 | if (!method_exists($entity, 'getPagePartAdminConfigurations') || !method_exists( |
||
311 | $entity, |
||
312 | 'getPageTemplates' |
||
313 | ) |
||
314 | ) { |
||
315 | continue; |
||
316 | } |
||
317 | |||
318 | $ppConfigs = $entity->getPagePartAdminConfigurations(); |
||
319 | $ptConfigs = $entity->getPageTemplates(); |
||
320 | |||
321 | foreach ($ppConfigs as $ppConfig) { |
||
322 | $parts = explode(':', $ppConfig); |
||
323 | $ppConfigFilename = $parts[count($parts) - 1]; |
||
324 | |||
325 | // Context found in this Page class |
||
326 | if (array_key_exists($ppConfigFilename, $sectionInfo)) { |
||
327 | // Search for templates |
||
328 | foreach ($ptConfigs as $ptConfig) { |
||
329 | $parts = explode(':', $ptConfig); |
||
330 | $ptConfigFilename = $parts[count($parts) - 1]; |
||
331 | |||
332 | // Page template found |
||
333 | if (array_key_exists($ptConfigFilename, $sectionInfo[$ppConfigFilename]['pagetempates'])) { |
||
334 | // Get all page properties |
||
335 | $form = $this->container->get('form.factory')->create($entity->getDefaultAdminType()); |
||
336 | $children = $form->createView()->children; |
||
337 | |||
338 | $pageFields = array(); |
||
339 | foreach ($children as $field) { |
||
340 | $name = $field->vars['name']; |
||
341 | $attr = $field->vars['attr']; |
||
342 | $blocks = $field->vars['block_prefixes']; |
||
343 | |||
344 | if ($name == 'title' || $name == 'pageTitle') { |
||
345 | continue; |
||
346 | } |
||
347 | |||
348 | if ($blocks[1] == 'hidden') { |
||
349 | // do nothing |
||
350 | } elseif ($blocks[1] == 'choice' && $blocks[1] == 'entity') { |
||
351 | // do nothing |
||
352 | } elseif ($blocks[1] == 'datetime') { |
||
353 | $pageFields[]['datetime'] = array( |
||
354 | 'label' => $this->labelCase($name), |
||
355 | 'date_random' => DateTime::date('d/m/Y'), |
||
356 | 'time_random' => DateTime::time('H:i'), |
||
357 | ); |
||
358 | } elseif ($blocks[1] == 'number') { |
||
359 | $pageFields[]['decimal'] = array( |
||
360 | 'label' => $this->labelCase($name), |
||
361 | 'random' => Base::randomFloat(2, 0, 99999), |
||
362 | ); |
||
363 | } elseif ($blocks[1] == 'integer') { |
||
364 | $pageFields[]['integer'] = array( |
||
365 | 'label' => $this->labelCase($name), |
||
366 | 'random' => Base::randomNumber(3000, 99999), |
||
367 | ); |
||
368 | } elseif ($blocks[1] == 'checkbox') { |
||
369 | $pageFields[]['boolean'] = array( |
||
370 | 'label' => $this->labelCase($name), |
||
371 | ); |
||
372 | } elseif ($blocks[1] == 'media') { |
||
373 | $id = (count($images) > 0 ? $images[0]->getId() : 1); |
||
374 | $pageFields[]['media'] = array( |
||
375 | 'label' => $this->labelCase($name), |
||
376 | 'random' => $id, |
||
377 | ); |
||
378 | } elseif ($blocks[2] == 'urlchooser') { |
||
379 | $pageFields[]['link'] = array( |
||
380 | 'label' => $this->labelCase($name), |
||
381 | 'random' => 'http://www.' . strtolower(Lorem::word()) . '.com', |
||
382 | ); |
||
383 | } elseif ($blocks[2] == 'textarea' && array_key_exists( |
||
384 | 'class', |
||
385 | $attr |
||
386 | ) && $attr['class'] == 'js-rich-editor rich-editor' |
||
387 | ) { |
||
388 | $pageFields[]['rich_text'] = array( |
||
389 | 'label' => $this->labelCase($name), |
||
390 | 'random' => Lorem::sentence(), |
||
391 | ); |
||
392 | } elseif ($blocks[2] == 'textarea' || $blocks[1] == 'text') { |
||
393 | $pageFields[]['text'] = array( |
||
394 | 'label' => $this->labelCase($name), |
||
395 | 'random' => Lorem::word(), |
||
396 | ); |
||
397 | } |
||
398 | } |
||
399 | |||
400 | $pages[] = array( |
||
401 | 'name' => $className, |
||
402 | 'section' => $sectionInfo[$ppConfigFilename]['context'], |
||
403 | 'template' => $sectionInfo[$ppConfigFilename]['pagetempates'][$ptConfigFilename], |
||
404 | 'fields' => $pageFields, |
||
405 | ); |
||
406 | } |
||
407 | } |
||
408 | } |
||
409 | } |
||
410 | } |
||
411 | } |
||
412 | |||
413 | /* |
||
414 | Example $pages contents: |
||
415 | Array |
||
416 | ( |
||
417 | [0] => Array |
||
418 | ( |
||
419 | [name] => ContentPage |
||
420 | [section] => main |
||
421 | [template] => Page with left sidebar |
||
422 | [fields] => Array |
||
423 | ( |
||
424 | ... |
||
425 | ) |
||
426 | ) |
||
427 | [1] => Array |
||
428 | ( |
||
429 | [name] => ContentPage |
||
430 | [section] => main |
||
431 | [template] => Full width page |
||
432 | [fields] => Array |
||
433 | ( |
||
434 | ... |
||
435 | ) |
||
436 | ) |
||
437 | [2] => Array |
||
438 | ( |
||
439 | [name] => HomePage |
||
440 | [section] => main |
||
441 | [template] => Home page |
||
442 | [fields] => Array |
||
443 | ( |
||
444 | ... |
||
445 | ) |
||
446 | ) |
||
447 | ) |
||
448 | */ |
||
449 | |||
450 | // Add some random values in the field array, so that this values can be uses as test values |
||
451 | foreach ($this->fields as $fkey => $fieldSet) { |
||
452 | foreach ($fieldSet as $key => $values) { |
||
453 | switch ($key) { |
||
454 | case 'multi_line': |
||
455 | View Code Duplication | case 'single_line': |
|
456 | $values[0]['random1'] = Lorem::word(); |
||
457 | $values[0]['random2'] = Lorem::word(); |
||
458 | $values[0]['lName'] = $this->labelCase($values[0]['fieldName']); |
||
459 | |||
460 | break; |
||
461 | View Code Duplication | case 'rich_text': |
|
462 | $values[0]['random1'] = Lorem::sentence(); |
||
463 | $values[0]['random2'] = Lorem::sentence(); |
||
464 | $values[0]['lName'] = $this->labelCase($values[0]['fieldName']); |
||
465 | |||
466 | break; |
||
467 | case 'link': |
||
468 | $values['url']['random1'] = 'http://www.' . strtolower(Lorem::word()) . '.com'; |
||
469 | $values['url']['random2'] = 'http://www.' . strtolower(Lorem::word()) . '.com'; |
||
470 | $values['url']['lName'] = $this->labelCase($values['url']['fieldName']); |
||
471 | $values['text']['random1'] = Lorem::word(); |
||
472 | $values['text']['random2'] = Lorem::word(); |
||
473 | $values['text']['lName'] = $this->labelCase($values['text']['fieldName']); |
||
474 | $values['new_window']['lName'] = $this->labelCase($values['new_window']['fieldName']); |
||
475 | |||
476 | break; |
||
477 | case 'image': |
||
478 | if (count($images) > 0) { |
||
479 | if (count($images) > 1) { |
||
480 | $values['image']['id_random1'] = $images[0]->getId(); |
||
481 | $values['image']['url_random1'] = $images[0]->getUrl(); |
||
482 | $values['image']['id_random2'] = $images[1]->getId(); |
||
483 | $values['image']['url_random2'] = $images[1]->getUrl(); |
||
484 | } else { |
||
485 | $values['image']['id_random1'] = $values['image']['id_random2'] = $images[0]->getId(); |
||
486 | $values['image']['url_random1'] = $values['image']['url_random2'] = $images[0]->getUrl( |
||
487 | ); |
||
488 | } |
||
489 | } else { |
||
490 | $values['image']['id_random1'] = $values['image']['id_random2'] = '1'; |
||
491 | $values['image']['url_random1'] = $values['image']['url_random2'] = 'XXX'; |
||
492 | } |
||
493 | $values['image']['lName'] = $this->labelCase($values['image']['fieldName']); |
||
494 | $values['alt_text']['random1'] = Lorem::word(); |
||
495 | $values['alt_text']['random2'] = Lorem::word(); |
||
496 | $values['alt_text']['lName'] = $this->labelCase($values['alt_text']['fieldName']); |
||
497 | |||
498 | break; |
||
499 | case 'boolean': |
||
500 | $values[0]['lName'] = $this->labelCase($values[0]['fieldName']); |
||
501 | |||
502 | break; |
||
503 | View Code Duplication | case 'integer': |
|
504 | $values[0]['random1'] = Base::randomNumber(3000, 99999); |
||
505 | $values[0]['random2'] = Base::randomNumber(3000, 99999); |
||
506 | $values[0]['lName'] = $this->labelCase($values[0]['fieldName']); |
||
507 | |||
508 | break; |
||
509 | View Code Duplication | case 'decimal': |
|
510 | $values[0]['random1'] = Base::randomFloat(2, 0, 99999); |
||
511 | $values[0]['random2'] = Base::randomFloat(2, 0, 99999); |
||
512 | $values[0]['lName'] = $this->labelCase($values[0]['fieldName']); |
||
513 | |||
514 | break; |
||
515 | case 'datetime': |
||
516 | $values[0]['date_random1'] = DateTime::date('d/m/Y'); |
||
517 | $values[0]['date_random2'] = DateTime::date('d/m/Y'); |
||
518 | $values[0]['time_random1'] = DateTime::time('H:i'); |
||
519 | $values[0]['time_random2'] = DateTime::time('H:i'); |
||
520 | $dparts = explode('/', $values[0]['date_random1']); |
||
521 | $values[0]['datetime_random1'] = $dparts[2] . '-' . $dparts[1] . '-' . $dparts[0] . ' ' . $values[0]['time_random1'] . ':00'; |
||
522 | $dparts = explode('/', $values[0]['date_random2']); |
||
523 | $values[0]['datetime_random2'] = $dparts[2] . '-' . $dparts[1] . '-' . $dparts[0] . ' ' . $values[0]['time_random2'] . ':00'; |
||
524 | $values[0]['lName'] = $this->labelCase($values[0]['fieldName']); |
||
525 | |||
526 | break; |
||
527 | } |
||
528 | |||
529 | $this->fields[$fkey][$key] = $values; |
||
530 | } |
||
531 | } |
||
532 | |||
533 | $params = array( |
||
534 | 'name' => $this->entity, |
||
535 | 'pages' => $pages, |
||
536 | 'fields' => $this->fields, |
||
537 | ); |
||
538 | $this->renderFile( |
||
539 | '/Features/PagePart.feature', |
||
540 | $this->bundle->getPath() . '/Features/Admin' . $this->entity . '.feature', |
||
541 | $params |
||
542 | ); |
||
543 | |||
544 | $this->assistant->writeLine('Generating behat test : <info>OK</info>'); |
||
545 | } |
||
546 | |||
559 |
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.