| Conditions | 14 |
| Paths | 300 |
| Total Lines | 64 |
| Code Lines | 43 |
| 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 |
||
| 42 | public function render(FormInterface $form, $colMap=[]) |
||
| 43 | { |
||
| 44 | if (method_exists($form, 'prepare')) { |
||
| 45 | $form->prepare(); |
||
| 46 | } |
||
| 47 | |||
| 48 | $params = $this->getView()->params()->fromQuery(); |
||
| 49 | $form->setAttribute('data-search-params', \Zend\Json\Json::encode($params)); |
||
| 50 | |||
| 51 | $formContent = '<div class="row" style="padding: 0 15px;">'; $buttonsRendered = false; |
||
| 52 | $buttonsContent = '<input type="submit" class="btn btn-primary" name="submit" value="' . $this->getView()->translate('Search') . '">' |
||
| 53 | . '<input type="reset" class="btn btn-default" name="clear" value="' . $this->getView()->translate('Clear') . '">'; |
||
| 54 | |||
| 55 | |||
| 56 | if (empty($colMap)) { |
||
| 57 | $c = count($form); |
||
| 58 | $r = floor($c / 3); |
||
| 59 | |||
| 60 | if (0 != $r) { |
||
| 61 | for ($i=0; $i<$r; $i+=1) { |
||
| 62 | $colMap[] = 4; |
||
| 63 | $colMap[] = 4; |
||
| 64 | $colMap[] = 4; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | if ($l = $c % 3) { |
||
| 68 | for ($i=0; $i<$l; $i+=1) { |
||
| 69 | $colMap[] = 12 / $l; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | $i = 0; |
||
| 75 | foreach ($form as $element) { |
||
| 76 | if ($element instanceof FieldsetInterface) { |
||
| 77 | trigger_error('Fieldsets are not allowed in a search form.', E_USER_NOTICE); |
||
| 78 | continue; |
||
| 79 | } else { |
||
| 80 | $col = isset($colMap[$element->getName()]) |
||
| 81 | ? $colMap[$element->getName()] |
||
| 82 | : (isset($colMap[$i]) ? $colMap[$i] : 4); |
||
| 83 | $i += 1; |
||
| 84 | |||
| 85 | if ($element->getName() == $form->getOption('button_element')) { |
||
| 86 | $formContent.='<div class="input-group col-md-' . $col . '">' |
||
| 87 | . $this->getView()->formElement($element) |
||
| 88 | . '<div class="input-group-btn search-form-buttons" style="width: 0px;">' . $buttonsContent . '</div>' |
||
| 89 | . '</div>'; |
||
| 90 | $buttonsRendered = true; |
||
| 91 | } else { |
||
| 92 | $formContent .= '<div class="input-group col-md-' . $col . '">' . $this->getView()->formElement($element) . '</div>'; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | if (!$buttonsRendered) { |
||
| 98 | $c = count($form); |
||
| 99 | $col = isset($colMap[$c]) ? $colMap[$c]: 12; |
||
| 100 | $formContent .= '<div class="input-group search-form-buttons col-md-' . $col . ' text-right">' |
||
| 101 | . '<div class="btn-group">' . $buttonsContent .'</div></div>'; |
||
| 102 | } |
||
| 103 | |||
| 104 | return $this->openTag($form) . $formContent . $this->closeTag(); |
||
| 105 | } |
||
| 106 | } |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: