| Conditions | 8 |
| Paths | 14 |
| Total Lines | 57 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 36 |
| CRAP Score | 8 |
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 |
||
| 28 | 80 | public function tokenize($httpField, $fromField) |
|
| 29 | { |
||
| 30 | 80 | $quoteSeparators = array('"', "'"); |
|
| 31 | 80 | $tokenList = array(); |
|
| 32 | 80 | $stringAccumulator = ''; |
|
| 33 | 80 | $lastQuote = ''; |
|
| 34 | |||
| 35 | // Iterate through field, character-by-character |
||
| 36 | 80 | for ($i = 0; $i < strlen($httpField); $i++) { |
|
| 37 | 72 | $chr = substr($httpField, $i, 1); |
|
| 38 | |||
| 39 | 72 | switch (true) { |
|
| 40 | |||
| 41 | // We are at the end of a quoted string |
||
| 42 | 72 | case $chr === $lastQuote: |
|
| 43 | 5 | $tokenList[] = $stringAccumulator; |
|
| 44 | 5 | $stringAccumulator = ''; |
|
| 45 | 5 | $lastQuote = ''; |
|
| 46 | 5 | break; |
|
| 47 | |||
| 48 | // We have found the beginning of a quoted string |
||
| 49 | 72 | case in_array($chr, $quoteSeparators): |
|
| 50 | 5 | $lastQuote = $chr; |
|
| 51 | 5 | break; |
|
| 52 | |||
| 53 | // We are already within a quoted string, but not yet at the end |
||
| 54 | 72 | case strlen($lastQuote): |
|
| 55 | 5 | $stringAccumulator .= $chr; |
|
| 56 | 5 | break; |
|
| 57 | |||
| 58 | // Separators found, add previously accumulated string & separator to token list |
||
| 59 | 72 | case Tokens::isSeparator($chr, Preference::MIME === $fromField): |
|
| 60 | 72 | if (strlen($stringAccumulator)) { |
|
| 61 | 72 | $tokenList[] = $stringAccumulator; |
|
| 62 | 72 | $stringAccumulator = ''; |
|
| 63 | 72 | } |
|
| 64 | |||
| 65 | 72 | $tokenList[] = $chr; |
|
| 66 | 72 | break; |
|
| 67 | |||
| 68 | // Simply accumulate characters |
||
| 69 | 72 | default: |
|
| 70 | 72 | $stringAccumulator .= $chr; |
|
| 71 | 72 | break; |
|
| 72 | 72 | } |
|
| 73 | 72 | } |
|
| 74 | |||
| 75 | // Handle final component |
||
| 76 | 80 | if (strlen($stringAccumulator)) { |
|
| 77 | 72 | $tokenList[] = $stringAccumulator; |
|
| 78 | 72 | } |
|
| 79 | |||
| 80 | // Remove any padding whitespace from token list |
||
| 81 | 80 | $tokenList = array_map('trim', $tokenList); |
|
| 82 | |||
| 83 | 80 | return $tokenList; |
|
| 84 | } |
||
| 85 | } |
||
| 86 |