| Conditions | 11 |
| Paths | 48 |
| Total Lines | 39 |
| 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 |
||
| 47 | public function __toString() |
||
| 48 | { |
||
| 49 | $this->addAttr('id', 'name'); |
||
| 50 | |||
| 51 | $attributes = ''; |
||
| 52 | |||
| 53 | if ($this->classes->isNotEmpty()) { |
||
| 54 | $this->parts->put('class', $this->classes->implode(' ')); |
||
| 55 | } |
||
| 56 | |||
| 57 | if ($this->styles->isNotEmpty()) { |
||
| 58 | $this->parts->put('style', $this->styles->pipe(function ($styles) { |
||
| 59 | $styleAttr = ''; |
||
| 60 | foreach ($styles as $key => $value) { |
||
| 61 | $styleAttr .= $key . ':' . $value . ';'; |
||
| 62 | } |
||
| 63 | |||
| 64 | return $styleAttr; |
||
| 65 | })); |
||
| 66 | } |
||
| 67 | |||
| 68 | foreach ($this->parts as $key => $value) { |
||
| 69 | if (is_bool($value) && $value === false) { |
||
| 70 | continue; |
||
| 71 | } |
||
| 72 | $attributes .= ' ' . $key . '="' . $value . '"'; |
||
| 73 | } |
||
| 74 | |||
| 75 | foreach ($this->attributes as $key => $value) { |
||
| 76 | if (is_bool($value) && $value === false) { |
||
| 77 | continue; |
||
| 78 | } else if (is_bool($value)) { |
||
| 79 | $value = $key; |
||
| 80 | } |
||
| 81 | $attributes .= ' ' . $key . '="' . $value . '"'; |
||
| 82 | } |
||
| 83 | |||
| 84 | return '<' . $this->tag . $attributes . '>'; |
||
| 85 | } |
||
| 86 | |||
| 97 | } |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.