| Conditions | 1 |
| Paths | 1 |
| Total Lines | 94 |
| Code Lines | 66 |
| 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 |
||
| 62 | private function getTestForm() |
||
| 63 | { |
||
| 64 | // Test refactore Form elements |
||
| 65 | $singleCheckbox = new HtmlFormElement( |
||
| 66 | HtmlFormElement::HTML_ELEMENT_INPUT_CHECKBOX, |
||
| 67 | 'accept', |
||
| 68 | 'Accept terms of usage.', |
||
| 69 | [true] |
||
| 70 | ); |
||
| 71 | |||
| 72 | $multiCheckbox = new HtmlFormElement( |
||
| 73 | HtmlFormElement::HTML_ELEMENT_INPUT_CHECKBOX, |
||
| 74 | 'my_locations', |
||
| 75 | 'I have already been...', |
||
| 76 | ['uk', 'eu'], |
||
| 77 | [ |
||
| 78 | 'The United States' => 'us', |
||
| 79 | 'European Union' => 'eu', |
||
| 80 | 'United Kingdom' => 'uk' |
||
| 81 | ] |
||
| 82 | ); |
||
| 83 | |||
| 84 | $radioGroup = new HtmlFormElement( |
||
| 85 | HtmlFormElement::HTML_ELEMENT_INPUT_RADIO, |
||
| 86 | 'curent_lang', |
||
| 87 | 'Current language', |
||
| 88 | ['hu-HU'], |
||
| 89 | [ |
||
| 90 | 'English' => 'en-GB', |
||
| 91 | 'German' => 'de-DE', |
||
| 92 | 'Hungarian' => 'hu-HU' |
||
| 93 | ] |
||
| 94 | ); |
||
| 95 | |||
| 96 | $select = new HtmlMultipleFormElement( |
||
| 97 | HtmlMultipleFormElement::HTML_ELEMENT_SELECT, |
||
| 98 | 'something', |
||
| 99 | null, |
||
| 100 | ['australia', 'india'], |
||
| 101 | [ |
||
| 102 | 'Default' => [ |
||
| 103 | 'Europe' => 'europe', |
||
| 104 | 'America' => 'america', |
||
| 105 | 'Australia' => 'australia' |
||
| 106 | ], |
||
| 107 | 'Africa' => 'africa', |
||
| 108 | 'Asia' => 'asia', |
||
| 109 | 'Antarctica' => 'antarctica', |
||
| 110 | 'India' => 'india' |
||
| 111 | ] |
||
| 112 | ); |
||
| 113 | $select->setMultiple(true); |
||
| 114 | |||
| 115 | $form = new HtmlForm('test', '', 'POST'); |
||
| 116 | $form |
||
| 117 | ->addElement( |
||
| 118 | new HtmlFormElement( |
||
| 119 | HtmlFormElement::HTML_ELEMENT_INPUT_HIDDEN, |
||
| 120 | 'csrf', |
||
| 121 | null, |
||
| 122 | [md5('something')] |
||
| 123 | ) |
||
| 124 | ) |
||
| 125 | ->addElement( |
||
| 126 | new HtmlFormElement( |
||
| 127 | HtmlFormElement::HTML_ELEMENT_INPUT_TEXT, |
||
| 128 | 'name', |
||
| 129 | 'Login name', |
||
| 130 | ['Joker'] |
||
| 131 | ) |
||
| 132 | ) |
||
| 133 | ->addElement( |
||
| 134 | new HtmlFormElement( |
||
| 135 | HtmlFormElement::HTML_ELEMENT_INPUT_PASSWORD, |
||
| 136 | 'password', |
||
| 137 | 'Password' |
||
| 138 | ) |
||
| 139 | ) |
||
| 140 | ->addElement($singleCheckbox) |
||
| 141 | ->addElement($multiCheckbox) |
||
| 142 | ->addElement($radioGroup) |
||
| 143 | ->addElement($select) |
||
| 144 | ->addElement( |
||
| 145 | new Html5FormElement(Html5FormElement::HTML_ELEMENT_INPUT_NUMBER, 'num', 'Num', [4], [1, 16]) |
||
| 146 | ) |
||
| 147 | ->addElement( |
||
| 148 | new Html5FormElement(Html5FormElement::HTML_ELEMENT_INPUT_RANGE, 'range', 'Range', [4], [1, 6, 0.2]) |
||
| 149 | ) |
||
| 150 | ->addElement( |
||
| 151 | new HtmlFormElement(HtmlFormElement::HTML_ELEMENT_INPUT_SUBMIT, 'submit', 'Submit') |
||
| 152 | ); |
||
| 153 | |||
| 154 | return $form; |
||
| 155 | } |
||
| 156 | } |
||
| 157 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.