| Conditions | 3 |
| Paths | 3 |
| Total Lines | 73 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 157 | public function idAction($id = null) |
||
| 158 | {
|
||
| 159 | $this->initialize(); |
||
| 160 | |||
| 161 | $event = $this->events->find($id); |
||
| 162 | |||
| 163 | $form = new \Mos\HTMLForm\CForm(); |
||
| 164 | |||
| 165 | $form = $form->create([], [ |
||
| 166 | 'id' => [ |
||
| 167 | 'type' => 'hidden', |
||
| 168 | 'required' => true, |
||
| 169 | 'validation' => ['not_empty'], |
||
| 170 | 'value' => $id |
||
| 171 | ], |
||
| 172 | 'title' => [ |
||
| 173 | 'class' => 'form-control', |
||
| 174 | 'type' => 'text', |
||
| 175 | 'label' => 'Title', |
||
| 176 | 'value' => $event->title, |
||
| 177 | 'required' => true, |
||
| 178 | 'validation' => ['not_empty'], |
||
| 179 | ], |
||
| 180 | 'time' => [ |
||
| 181 | 'class' => 'form-control', |
||
| 182 | 'type' => 'date', |
||
| 183 | 'label' => 'Date for event', |
||
| 184 | 'value' => $event->showdate, |
||
| 185 | 'required' => true, |
||
| 186 | 'validation' => ['not_empty'], |
||
| 187 | ], |
||
| 188 | 'content' => [ |
||
| 189 | 'class' => 'form-control', |
||
| 190 | 'type' => 'textarea', |
||
| 191 | 'label' => 'Content', |
||
| 192 | 'value' => $event->content, |
||
| 193 | 'required' => true, |
||
| 194 | 'validation' => ['not_empty'], |
||
| 195 | ], |
||
| 196 | 'submit' => [ |
||
| 197 | 'class' => 'btn btn-default', |
||
| 198 | 'type' => 'submit', |
||
| 199 | 'value' => 'Update', |
||
| 200 | 'callback' => function($form) {
|
||
| 201 | $form->saveInSession = true; |
||
| 202 | return true; |
||
| 203 | } |
||
| 204 | ], |
||
| 205 | ]); |
||
| 206 | |||
| 207 | // Check the status of the form |
||
| 208 | $status = $form->check(); |
||
| 209 | |||
| 210 | if ($status === true) {
|
||
| 211 | |||
| 212 | $this->dispatcher->forward([ |
||
| 213 | 'controller' => 'event', |
||
| 214 | 'action' => 'add', |
||
| 215 | 'params' => ['id' => $id], |
||
| 216 | ]); |
||
| 217 | |||
| 218 | } else if ($status === false) {
|
||
| 219 | |||
| 220 | var_dump('Check method returned false');
|
||
| 221 | die; |
||
| 222 | } |
||
| 223 | |||
| 224 | $this->theme->setTitle("View event with id");
|
||
| 225 | $this->views->add('calendar/view', [
|
||
| 226 | 'event' => $event, |
||
| 227 | 'form' => $form->getHTML() |
||
| 228 | ]); |
||
| 229 | } |
||
| 230 | } |
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: