| Conditions | 9 |
| Paths | 16 |
| Total Lines | 52 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 27 |
| CRAP Score | 9.0036 |
| 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 |
||
| 32 | 2 | public function serialize(array $links): ?string |
|
| 33 | { |
||
| 34 | 2 | $elements = []; |
|
| 35 | 2 | $result = null; |
|
| 36 | 2 | foreach ($links as $link) { |
|
| 37 | /** |
||
| 38 | * Leave templated links alone |
||
| 39 | */ |
||
| 40 | 1 | if ($link->isTemplated()) { |
|
| 41 | continue; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Split the parts of the attributes so that we can parse them |
||
| 46 | */ |
||
| 47 | 1 | $attributes = $link->getAttributes(); |
|
| 48 | 1 | $rels = $link->getRels(); |
|
| 49 | $parts = [ |
||
| 50 | 1 | "", |
|
| 51 | 1 | "rel=\"" . implode(" ", $rels) . "\"" |
|
| 52 | ]; |
||
| 53 | |||
| 54 | 1 | foreach ($attributes as $key => $value) { |
|
| 55 | 1 | if (is_array($value)) { |
|
| 56 | 1 | foreach ($value as $subValue) { |
|
| 57 | 1 | $parts[] = $key . "=\"" . $subValue . "\""; |
|
| 58 | } |
||
| 59 | 1 | continue; |
|
| 60 | } |
||
| 61 | |||
| 62 | 1 | if (!is_bool($value)) { |
|
| 63 | 1 | $parts[] = $key . "=\"" . $value . "\""; |
|
| 64 | 1 | continue; |
|
| 65 | } |
||
| 66 | |||
| 67 | 1 | if (true === $value) { |
|
| 68 | 1 | $parts[] = $key; |
|
| 69 | 1 | continue; |
|
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | 1 | $elements[] = "<" |
|
| 74 | 1 | . $link->getHref() |
|
| 75 | 1 | . ">" |
|
| 76 | 1 | . implode("; ", $parts); |
|
| 77 | } |
||
| 78 | |||
| 79 | 2 | if (count($elements) > 0) { |
|
| 80 | 1 | $result = implode(",", $elements); |
|
| 81 | } |
||
| 82 | |||
| 83 | 2 | return $result; |
|
| 84 | } |
||
| 86 |