| Conditions | 6 |
| Paths | 32 |
| Total Lines | 77 |
| Code Lines | 41 |
| 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 |
||
| 84 | public function returnForm($url, $action = 'add') |
||
| 85 | { |
||
| 86 | $form = new FormValidator('template', 'post', $url); |
||
| 87 | // Setting the form elements |
||
| 88 | $header = get_lang('Add'); |
||
| 89 | if ('edit' === $action) { |
||
| 90 | $header = get_lang('Edit'); |
||
| 91 | } |
||
| 92 | $id = isset($_GET['id']) ? (int) $_GET['id'] : ''; |
||
| 93 | |||
| 94 | $form->addElement('header', '', $header); |
||
| 95 | $form->addElement('hidden', 'id', $id); |
||
| 96 | $form->addElement( |
||
| 97 | 'text', |
||
| 98 | 'name', |
||
| 99 | get_lang('Name'), |
||
| 100 | ['size' => '70', 'id' => 'name'] |
||
| 101 | ); |
||
| 102 | |||
| 103 | /*$form->addHtmlEditor( |
||
| 104 | 'email_template', |
||
| 105 | get_lang('Template'), |
||
| 106 | false, |
||
| 107 | false, |
||
| 108 | [ |
||
| 109 | 'ToolbarSet' => 'Careers', |
||
| 110 | 'Width' => '100%', |
||
| 111 | 'Height' => '250', |
||
| 112 | ] |
||
| 113 | );*/ |
||
| 114 | $form->addTextarea( |
||
| 115 | 'email_template', |
||
| 116 | get_lang('Template') |
||
| 117 | ); |
||
| 118 | |||
| 119 | $finder = new Finder(); |
||
| 120 | $files = $finder |
||
| 121 | ->files() |
||
| 122 | ->in(api_get_path(SYS_CODE_PATH).'template/default/mail') |
||
| 123 | ->sort( |
||
| 124 | function ($a, $b) { |
||
| 125 | return strcmp($a->getRealpath(), $b->getRealpath()); |
||
| 126 | } |
||
| 127 | ); |
||
| 128 | |||
| 129 | $options = []; |
||
| 130 | /** @var SplFileInfo $file */ |
||
| 131 | foreach ($files as $file) { |
||
| 132 | $options[$file->getFilename()] = $file->getFilename(); |
||
| 133 | } |
||
| 134 | |||
| 135 | $form->addSelect( |
||
| 136 | 'type', |
||
| 137 | get_lang('Type'), |
||
| 138 | $options |
||
| 139 | ); |
||
| 140 | |||
| 141 | $defaults = $this->get($id); |
||
| 142 | |||
| 143 | if ('edit' === $action) { |
||
| 144 | $form->addLabel(get_lang('Created at'), Display::dateToStringAgoAndLongDate($defaults['created_at'])); |
||
| 145 | $form->addLabel(get_lang('Updated at'), Display::dateToStringAgoAndLongDate($defaults['updated_at'])); |
||
| 146 | $form->addButtonSave(get_lang('Edit'), 'submit'); |
||
| 147 | } else { |
||
| 148 | $form->addButtonCreate(get_lang('Add'), 'submit'); |
||
| 149 | } |
||
| 150 | |||
| 151 | // Setting the defaults |
||
| 152 | if (!empty($defaults)) { |
||
| 153 | $defaults['email_template'] = $defaults['template']; |
||
| 154 | } |
||
| 155 | $form->setDefaults($defaults); |
||
| 156 | |||
| 157 | // Setting the rules |
||
| 158 | $form->addRule('name', get_lang('Required field'), 'required'); |
||
| 159 | |||
| 160 | return $form; |
||
| 161 | } |
||
| 238 |