| Conditions | 13 |
| Paths | 13 |
| Total Lines | 45 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 13 | public static function buildHtmlAttributes(array $htmlAttributes, string $charset = 'UTF-8'): string |
||
| 14 | { |
||
| 15 | $html = ''; |
||
| 16 | |||
| 17 | foreach ($htmlAttributes as $name => $value) { |
||
| 18 | if (is_bool($value)) { |
||
| 19 | if ($value) { |
||
| 20 | $html .= " $name"; |
||
| 21 | } |
||
| 22 | continue; |
||
| 23 | } |
||
| 24 | |||
| 25 | if (is_array($value)) { |
||
| 26 | if (in_array($name, self::DATA_ATTRIBUTES)) { |
||
| 27 | foreach ($value as $n => $v) { |
||
| 28 | if (is_string($v)) { |
||
| 29 | $html .= " $name-$n=\"" . self::encode($v, $charset) . '"'; |
||
| 30 | } |
||
| 31 | } |
||
| 32 | continue; |
||
| 33 | } |
||
| 34 | |||
| 35 | if ($name === 'class') { |
||
| 36 | if (empty($value)) { |
||
| 37 | continue; |
||
| 38 | } |
||
| 39 | $html .= " $name=\"" . self::encode(implode(' ', $value), $charset) . '"'; |
||
| 40 | continue; |
||
| 41 | } |
||
| 42 | |||
| 43 | if ($name === 'style') { |
||
| 44 | if (empty($value)) { |
||
| 45 | continue; |
||
| 46 | } |
||
| 47 | $html .= " $name=\"" . self::encode(self::cssStyleFromArray($value), $charset) . '"'; |
||
| 48 | } |
||
| 49 | continue; |
||
| 50 | } |
||
| 51 | |||
| 52 | if ($value !== null) { |
||
| 53 | $html .= " $name=\"" . self::encode($value, $charset) . '"'; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | return $html; |
||
| 58 | } |
||
| 75 |