| Conditions | 5 |
| Paths | 5 |
| Total Lines | 58 |
| 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 |
||
| 34 | public function parse($line) |
||
| 35 | { |
||
| 36 | //begin of dependencies |
||
| 37 | $stringUtility = $this->stringUtility; |
||
| 38 | //end of dependencies |
||
| 39 | |||
| 40 | //begin of business logic |
||
| 41 | $lineAsArray = explode(' ', $line); |
||
| 42 | |||
| 43 | $arrayIsInvalid = (count($lineAsArray) < 19); |
||
| 44 | |||
| 45 | if ($arrayIsInvalid) { |
||
| 46 | throw new InvalidArgumentException( |
||
| 47 | self::class . ' can not parse given line "' . $line . '"' |
||
| 48 | ); |
||
| 49 | } |
||
| 50 | |||
| 51 | $httpMethod = ( |
||
| 52 | $stringUtility->startsWith($lineAsArray[16], '{') |
||
| 53 | ? substr($lineAsArray[16], 1) |
||
| 54 | : $lineAsArray[16] |
||
| 55 | ); |
||
| 56 | $pid = filter_var( |
||
| 57 | $lineAsArray[2], |
||
| 58 | FILTER_SANITIZE_NUMBER_INT |
||
| 59 | ); |
||
| 60 | $status = str_replace( |
||
| 61 | [ |
||
| 62 | '[', |
||
| 63 | ']' |
||
| 64 | ], |
||
| 65 | '', |
||
| 66 | $lineAsArray[4] |
||
| 67 | ); |
||
| 68 | $uriAuthority = str_replace( |
||
| 69 | [ |
||
| 70 | '[', |
||
| 71 | ']' |
||
| 72 | ], |
||
| 73 | '', |
||
| 74 | (isset($lineAsArray[19]) ? $lineAsArray[19] : $lineAsArray[18]) |
||
| 75 | ); |
||
| 76 | $uriPathWithQuery = ( |
||
| 77 | $stringUtility->endsWith($lineAsArray[17], '}') |
||
| 78 | ? substr($lineAsArray[17], 0, -1) |
||
| 79 | : $lineAsArray[17] |
||
| 80 | ); |
||
| 81 | |||
| 82 | return new Detail( |
||
| 83 | $httpMethod, |
||
| 84 | $lineAsArray[15], |
||
| 85 | $pid, |
||
| 86 | $status, |
||
| 87 | $uriAuthority, |
||
| 88 | $uriPathWithQuery |
||
| 89 | ); |
||
| 90 | //end of business logic |
||
| 91 | } |
||
| 92 | } |
||
| 93 |