| Conditions | 4 |
| Paths | 6 |
| Total Lines | 77 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 63 | public function convertToString(AnalysisResults $analysisResults): string |
||
| 64 | { |
||
| 65 | $files = []; |
||
| 66 | foreach ($analysisResults->getAnalysisResults() as $analysisResult) { |
||
| 67 | $fullDetails = JsonUtils::toArray($analysisResult->getFullDetails()); |
||
| 68 | $message = ArrayUtils::getArrayValue($fullDetails, self::MESSAGE); |
||
| 69 | $absoluteFilePath = ArrayUtils::getStringValue($fullDetails, self::ABSOLUTE_FILE_PATH); |
||
| 70 | |||
| 71 | if (!array_key_exists($absoluteFilePath, $files)) { |
||
| 72 | $files[$absoluteFilePath] = []; |
||
| 73 | } |
||
| 74 | |||
| 75 | $files[$absoluteFilePath][] = $message; |
||
| 76 | } |
||
| 77 | |||
| 78 | $asArray = [ |
||
| 79 | 'totals' => [ |
||
| 80 | 'errors' => count( |
||
| 81 | array_filter( |
||
| 82 | $analysisResults->getAnalysisResults(), |
||
| 83 | static function (AnalysisResult $result): bool { |
||
| 84 | $details = JsonUtils::toArray($result->getFullDetails()); |
||
| 85 | ArrayUtils::assertArray($details['message']); |
||
| 86 | |||
| 87 | return 'ERROR' === $details['message']['type']; |
||
| 88 | } |
||
| 89 | ) |
||
| 90 | ), |
||
| 91 | 'warnings' => count( |
||
| 92 | array_filter( |
||
| 93 | $analysisResults->getAnalysisResults(), |
||
| 94 | static function (AnalysisResult $result): bool { |
||
| 95 | $details = JsonUtils::toArray($result->getFullDetails()); |
||
| 96 | ArrayUtils::assertArray($details['message']); |
||
| 97 | |||
| 98 | return 'WARNING' === $details['message']['type']; |
||
| 99 | } |
||
| 100 | ) |
||
| 101 | ), |
||
| 102 | 'fixable' => count( |
||
| 103 | array_filter( |
||
| 104 | $analysisResults->getAnalysisResults(), |
||
| 105 | static function (AnalysisResult $result): bool { |
||
| 106 | $details = JsonUtils::toArray($result->getFullDetails()); |
||
| 107 | ArrayUtils::assertArray($details['message']); |
||
| 108 | |||
| 109 | return (bool) $details['message']['fixable']; |
||
| 110 | } |
||
| 111 | ) |
||
| 112 | ), |
||
| 113 | ], |
||
| 114 | self::FILES => [], |
||
| 115 | ]; |
||
| 116 | |||
| 117 | foreach ($files as $fileName => $messages) { |
||
| 118 | $asArray[self::FILES][$fileName] = [ |
||
| 119 | 'errors' => count( |
||
| 120 | array_filter( |
||
| 121 | $messages, |
||
| 122 | static function (array $message): bool { |
||
| 123 | return 'ERROR' === $message['type']; |
||
| 124 | } |
||
| 125 | ) |
||
| 126 | ), |
||
| 127 | 'warnings' => count( |
||
| 128 | array_filter( |
||
| 129 | $messages, |
||
| 130 | static function (array $message): bool { |
||
| 131 | return 'WARNING' === $message['type']; |
||
| 132 | } |
||
| 133 | ) |
||
| 134 | ), |
||
| 135 | self::MESSAGES => $messages, |
||
| 136 | ]; |
||
| 137 | } |
||
| 138 | |||
| 139 | return JsonUtils::toString($asArray); |
||
| 140 | } |
||
| 239 |