| Conditions | 10 |
| Paths | 28 |
| Total Lines | 45 |
| Code Lines | 22 |
| 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 |
||
| 38 | public function getRecursiveReadableErrors($form, $withChildren = true, $translationDomain = null, $level = 0) |
||
| 39 | { |
||
| 40 | $errors = ''; |
||
| 41 | $translationDomain = $translationDomain ? $translationDomain : $form->getConfig()->getOption('translation_domain'); |
||
| 42 | |||
| 43 | //the errors of the fields |
||
| 44 | foreach ($form->getErrors() as $error) { |
||
| 45 | //the view contains the label identifier |
||
| 46 | $view = $form->createView(); |
||
| 47 | $labelId = $view->vars['label']; |
||
| 48 | |||
| 49 | //get the translated label |
||
| 50 | if ($labelId !== null) { |
||
| 51 | $label = $this->translator->trans(/* @Ignore */$labelId, [], $translationDomain).' : '; |
||
| 52 | } else { |
||
| 53 | $label = ''; |
||
| 54 | } |
||
| 55 | |||
| 56 | //in case of dev mode, we display the item that is a problem |
||
| 57 | //getCause cames in Symfony 2.5 version, this is just a fallback to avoid BC with previous versions |
||
| 58 | if ($this->debug && method_exists($error, 'getCause')) { |
||
| 59 | $cause = $error->getCause(); |
||
| 60 | if ($cause !== null) { |
||
| 61 | $causePropertyPath = $cause->getPropertyPath(); |
||
| 62 | $errors .= ' '.$causePropertyPath; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | //add the error |
||
| 67 | $errors .= $label.$this->translator->trans(/* @Ignore */$error->getMessage(), [], $translationDomain)."\n"; |
||
| 68 | } |
||
| 69 | |||
| 70 | //do we parse the children |
||
| 71 | if ($withChildren) { |
||
| 72 | //we parse the children |
||
| 73 | foreach ($form->getIterator() as $child) { |
||
| 74 | $level++; |
||
| 75 | if ($err = $this->getRecursiveReadableErrors($child, $withChildren, $translationDomain, $level)) { |
||
| 76 | $errors .= $err; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | return $errors; |
||
| 82 | } |
||
| 83 | } |
||
| 84 |
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: