Conditions | 32 |
Paths | > 20000 |
Total Lines | 261 |
Code Lines | 116 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 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 |
||
409 | public function __formAction() |
||
410 | { |
||
411 | $fields = $_POST['fields']; |
||
412 | $this->_errors = array(); |
||
413 | $providers = Symphony::ExtensionManager()->getProvidersOf(iProvider::EVENT); |
||
414 | $providerClass = null; |
||
415 | |||
416 | if (trim($fields['name']) == '') { |
||
417 | $this->_errors['name'] = __('This is a required field'); |
||
418 | } elseif (strpos($fields['name'], '\\') !== false) { |
||
419 | $this->_errors['name'] = __('This field contains invalid characters') . ' (\\)'; |
||
420 | } elseif (!preg_match('/^[a-z]/i', $fields['name'])) { |
||
421 | $this->_errors['name'] = __('The name of the event must begin with a letter.'); |
||
422 | } |
||
423 | |||
424 | if (trim($fields['source']) == '') { |
||
425 | $this->_errors['source'] = __('This is a required field'); |
||
426 | } |
||
427 | |||
428 | $filters = isset($fields['filters']) ? $fields['filters'] : array(); |
||
429 | |||
430 | // See if a Provided Datasource is saved |
||
431 | if (!empty($providers)) { |
||
432 | foreach ($providers as $providerClass => $provider) { |
||
433 | if ($fields['source'] == call_user_func(array($providerClass, 'getSource'))) { |
||
434 | call_user_func_array(array($providerClass, 'validate'), array(&$fields, &$this->_errors)); |
||
435 | break; |
||
436 | } |
||
437 | |||
438 | unset($providerClass); |
||
439 | } |
||
440 | } |
||
441 | |||
442 | $classname = Lang::createHandle($fields['name'], 255, '_', false, true, array('@^[^a-z\d]+@i' => '', '/[^\w\-\.]/i' => '')); |
||
443 | $rootelement = str_replace('_', '-', $classname); |
||
444 | $extends = 'SectionEvent'; |
||
445 | |||
446 | // Check to make sure the classname is not empty after handlisation. |
||
447 | if (empty($classname) && !isset($this->_errors['name'])) { |
||
448 | $this->_errors['name'] = __('Please ensure name contains at least one Latin-based character.', array($classname)); |
||
449 | } |
||
450 | |||
451 | $file = EVENTS . '/event.' . $classname . '.php'; |
||
452 | $isDuplicate = false; |
||
453 | $queueForDeletion = null; |
||
454 | |||
455 | if ($this->_context[0] == 'new' && is_file($file)) { |
||
456 | $isDuplicate = true; |
||
457 | } elseif ($this->_context[0] == 'edit') { |
||
458 | $existing_handle = $this->_context[1]; |
||
459 | |||
460 | if ($classname != $existing_handle && is_file($file)) { |
||
461 | $isDuplicate = true; |
||
462 | } elseif ($classname != $existing_handle) { |
||
463 | $queueForDeletion = EVENTS . '/event.' . $existing_handle . '.php'; |
||
464 | } |
||
465 | } |
||
466 | |||
467 | // Duplicate |
||
468 | if ($isDuplicate) { |
||
469 | $this->_errors['name'] = __('An Event with the name %s already exists', array('<code>' . $classname . '</code>')); |
||
470 | } |
||
471 | |||
472 | if (empty($this->_errors)) { |
||
473 | $source = $fields['source']; |
||
474 | $params = array( |
||
475 | 'rootelement' => $rootelement, |
||
476 | ); |
||
477 | |||
478 | $about = array( |
||
479 | 'name' => $fields['name'], |
||
480 | 'version' => 'Symphony ' . Symphony::Configuration()->get('version', 'symphony'), |
||
481 | 'release date' => DateTimeObj::getGMT('c'), |
||
482 | 'author name' => Symphony::Author()->getFullName(), |
||
483 | 'author website' => URL, |
||
484 | 'author email' => Symphony::Author()->get('email') |
||
485 | ); |
||
486 | |||
487 | // If there is a provider, get their template |
||
488 | if ($providerClass) { |
||
489 | $eventShell = file_get_contents(call_user_func(array($providerClass, 'getTemplate'))); |
||
490 | } else { |
||
491 | $eventShell = file_get_contents($this->getTemplate('blueprints.event')); |
||
492 | $about['trigger condition'] = $rootelement; |
||
493 | } |
||
494 | |||
495 | $this->__injectAboutInformation($eventShell, $about); |
||
496 | |||
497 | // Replace the name |
||
498 | $eventShell = str_replace('<!-- CLASS NAME -->', $classname, $eventShell); |
||
499 | |||
500 | // Build the templates |
||
501 | if ($providerClass) { |
||
502 | $eventShell = call_user_func(array($providerClass, 'prepare'), $fields, $params, $eventShell); |
||
503 | } else { |
||
504 | $this->__injectFilters($eventShell, $filters); |
||
505 | |||
506 | // Add Documentation |
||
507 | $ajaxEventDoc = new contentAjaxEventDocumentation(); |
||
508 | $doc_parts = array(); |
||
509 | |||
510 | // Add Documentation (Success/Failure) |
||
511 | $ajaxEventDoc->addEntrySuccessDoc($doc_parts, $rootelement, $filters); |
||
512 | $ajaxEventDoc->addEntryFailureDoc($doc_parts, $rootelement, $filters); |
||
513 | |||
514 | // Filters |
||
515 | $ajaxEventDoc->addDefaultFiltersDoc($doc_parts, $rootelement, $filters); |
||
516 | |||
517 | // Frontend Markup |
||
518 | $ajaxEventDoc->addFrontendMarkupDoc($doc_parts, $rootelement, $fields['source'], $filters); |
||
519 | $ajaxEventDoc->addSendMailFilterDoc($doc_parts, $filters); |
||
520 | |||
521 | /** |
||
522 | * Allows adding documentation for new filters. A reference to the $documentation |
||
523 | * array is provided, along with selected filters |
||
524 | * @delegate AppendEventFilterDocumentation |
||
525 | * @param string $context |
||
526 | * '/blueprints/events/(edit|new|info)/' |
||
527 | * @param array $selected |
||
528 | * An array of all the selected filters for this Event |
||
529 | * @param array $documentation |
||
530 | * An array of all the documentation XMLElements, passed by reference |
||
531 | * @param string $rootelment |
||
532 | * The name of this event, as a handle. |
||
533 | */ |
||
534 | Symphony::ExtensionManager()->notifyMembers('AppendEventFilterDocumentation', '/blueprints/events/', array( |
||
535 | 'selected' => $filters, |
||
536 | 'documentation' => &$doc_parts, |
||
537 | 'rootelement' => $rootelement |
||
538 | )); |
||
539 | |||
540 | $documentation = join(PHP_EOL, array_map(function($part) { |
||
541 | return rtrim($part->generate(true, 4)); |
||
542 | }, $doc_parts)); |
||
543 | $documentation = str_replace('\'', '\\\'', $documentation); |
||
544 | |||
545 | $eventShell = str_replace('<!-- CLASS EXTENDS -->', $extends, $eventShell); |
||
546 | $eventShell = str_replace('<!-- DOCUMENTATION -->', General::tabsToSpaces($documentation, 4), $eventShell); |
||
547 | } |
||
548 | |||
549 | $eventShell = str_replace('<!-- ROOT ELEMENT -->', $rootelement, $eventShell); |
||
550 | $eventShell = str_replace('<!-- CLASS NAME -->', $classname, $eventShell); |
||
551 | $eventShell = str_replace('<!-- SOURCE -->', addslashes($source), $eventShell); |
||
552 | |||
553 | // Remove left over placeholders |
||
554 | $eventShell = preg_replace(array('/<!--[\w ]++-->/'), '', $eventShell); |
||
555 | |||
556 | if ($this->_context[0] == 'new') { |
||
557 | /** |
||
558 | * Prior to creating an Event, the file path where it will be written to |
||
559 | * is provided and well as the contents of that file. |
||
560 | * |
||
561 | * @delegate EventsPreCreate |
||
562 | * @since Symphony 2.2 |
||
563 | * @param string $context |
||
564 | * '/blueprints/events/' |
||
565 | * @param string $file |
||
566 | * The path to the Event file |
||
567 | * @param string $contents |
||
568 | * The contents for this Event as a string passed by reference |
||
569 | * @param array $filters |
||
570 | * An array of the filters attached to this event |
||
571 | */ |
||
572 | Symphony::ExtensionManager()->notifyMembers('EventPreCreate', '/blueprints/events/', array( |
||
573 | 'file' => $file, |
||
574 | 'contents' => &$eventShell, |
||
575 | 'filters' => $filters |
||
576 | )); |
||
577 | } else { |
||
578 | /** |
||
579 | * Prior to editing an Event, the file path where it will be written to |
||
580 | * is provided and well as the contents of that file. |
||
581 | * |
||
582 | * @delegate EventPreEdit |
||
583 | * @since Symphony 2.2 |
||
584 | * @param string $context |
||
585 | * '/blueprints/events/' |
||
586 | * @param string $file |
||
587 | * The path to the Event file |
||
588 | * @param string $contents |
||
589 | * The contents for this Event as a string passed by reference |
||
590 | * @param array $filters |
||
591 | * An array of the filters attached to this event |
||
592 | */ |
||
593 | Symphony::ExtensionManager()->notifyMembers('EventPreEdit', '/blueprints/events/', array( |
||
594 | 'file' => $file, |
||
595 | 'contents' => &$eventShell, |
||
596 | 'filters' => $filters |
||
597 | )); |
||
598 | } |
||
599 | |||
600 | // Write the file |
||
601 | if (!General::writeFile($file, $eventShell, Symphony::Configuration()->get('write_mode', 'file'))) { |
||
602 | $this->pageAlert( |
||
603 | __('Failed to write Event to disk.') |
||
604 | . ' ' . __('Please check permissions on %s.', array('<code>/workspace/events</code>')), |
||
605 | Alert::ERROR |
||
606 | ); |
||
607 | |||
608 | // Write successful |
||
609 | } else { |
||
610 | if (function_exists('opcache_invalidate')) { |
||
611 | @opcache_invalidate($file, true); |
||
612 | } |
||
613 | |||
614 | // Attach this event to pages |
||
615 | $connections = $fields['connections']; |
||
616 | ResourceManager::setPages(ResourceManager::RESOURCE_TYPE_EVENT, is_null($existing_handle) ? $classname : $existing_handle, $connections); |
||
617 | |||
618 | if ($queueForDeletion) { |
||
619 | General::deleteFile($queueForDeletion); |
||
620 | |||
621 | $pages = PageManager::fetch(false, array('events', 'id'), array(" |
||
622 | `events` REGEXP '[[:<:]]" . $existing_handle . "[[:>:]]' |
||
623 | ")); |
||
624 | |||
625 | if (is_array($pages) && !empty($pages)) { |
||
626 | foreach ($pages as $page) { |
||
627 | $page['events'] = preg_replace('/\b'.$existing_handle.'\b/i', $classname, $page['events']); |
||
628 | |||
629 | PageManager::edit($page['id'], $page); |
||
630 | } |
||
631 | } |
||
632 | } |
||
633 | |||
634 | if ($this->_context[0] == 'new') { |
||
635 | /** |
||
636 | * After creating the Event, the path to the Event file is provided |
||
637 | * |
||
638 | * @delegate EventPostCreate |
||
639 | * @since Symphony 2.2 |
||
640 | * @param string $context |
||
641 | * '/blueprints/events/' |
||
642 | * @param string $file |
||
643 | * The path to the Event file |
||
644 | */ |
||
645 | Symphony::ExtensionManager()->notifyMembers('EventPostCreate', '/blueprints/events/', array( |
||
646 | 'file' => $file |
||
647 | )); |
||
648 | } else { |
||
649 | /** |
||
650 | * After editing the Event, the path to the Event file is provided |
||
651 | * |
||
652 | * @delegate EventPostEdit |
||
653 | * @since Symphony 2.2 |
||
654 | * @param string $context |
||
655 | * '/blueprints/events/' |
||
656 | * @param string $file |
||
657 | * The path to the Event file |
||
658 | * @param string $previous_file |
||
659 | * The path of the previous Event file in the case where an Event may |
||
660 | * have been renamed. To get the handle from this value, see |
||
661 | * `EventManager::__getHandleFromFilename` |
||
662 | */ |
||
663 | Symphony::ExtensionManager()->notifyMembers('EventPostEdit', '/blueprints/events/', array( |
||
664 | 'file' => $file, |
||
665 | 'previous_file' => ($queueForDeletion) ? $queueForDeletion : null |
||
666 | )); |
||
667 | } |
||
668 | |||
669 | redirect(SYMPHONY_URL . '/blueprints/events/edit/'.$classname.'/'.($this->_context[0] == 'new' ? 'created' : 'saved') . '/'); |
||
670 | } |
||
698 |
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider
.