| Conditions | 9 |
| Paths | 16 |
| Total Lines | 52 |
| Code Lines | 31 |
| 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 |
||
| 53 | public function convertFromString(string $resultsAsString, ProjectRoot $projectRoot): AnalysisResults |
||
| 54 | { |
||
| 55 | $analysisResultsAsArray = JsonUtils::toArray($resultsAsString); |
||
| 56 | |||
| 57 | $analysisResultsBuilder = new AnalysisResultsBuilder(); |
||
| 58 | |||
| 59 | try { |
||
| 60 | $errors = ArrayUtils::getArrayValue($analysisResultsAsArray, self::ERRORS); |
||
| 61 | } catch (ArrayParseException $e) { |
||
| 62 | throw ParseAtLocationException::issueParsing($e, 'Root node'); |
||
| 63 | } |
||
| 64 | |||
| 65 | if (count($errors) > 0) { |
||
| 66 | try { |
||
| 67 | ArrayUtils::assertArrayOfStrings($errors); |
||
| 68 | } catch (ArrayParseException $e) { |
||
| 69 | throw ParseAtLocationException::issueParsing($e, self::ERRORS); |
||
| 70 | } |
||
| 71 | $errorMessage = 'PHPStan failed with errors:'.\PHP_EOL.implode("\n", $errors); |
||
| 72 | throw new ErrorReportedByStaticAnalysisTool($errorMessage); |
||
| 73 | } |
||
| 74 | |||
| 75 | try { |
||
| 76 | $filesErrors = ArrayUtils::getArrayValue($analysisResultsAsArray, self::FILES); |
||
| 77 | } catch (ArrayParseException $e) { |
||
| 78 | throw ParseAtLocationException::issueParsing($e, 'Root node'); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** @psalm-suppress MixedAssignment */ |
||
| 82 | foreach ($filesErrors as $absoluteFileNameAsString => $fileErrors) { |
||
| 83 | try { |
||
| 84 | if (!is_string($absoluteFileNameAsString)) { |
||
| 85 | throw new ArrayParseException('Expected filename to be of type string'); |
||
| 86 | } |
||
| 87 | |||
| 88 | ArrayUtils::assertArray($fileErrors); |
||
| 89 | |||
| 90 | $absoluteFileName = new AbsoluteFileName($absoluteFileNameAsString); |
||
| 91 | |||
| 92 | $messages = ArrayUtils::getArrayValue($fileErrors, self::MESSAGES); |
||
| 93 | |||
| 94 | foreach ($messages as $message) { |
||
| 95 | ArrayUtils::assertArray($message); |
||
| 96 | $analysisResult = $this->convertAnalysisResultFromArray($message, $absoluteFileName, $projectRoot); |
||
| 97 | $analysisResultsBuilder->addAnalysisResult($analysisResult); |
||
| 98 | } |
||
| 99 | } catch (ArrayParseException|InvalidPathException $e) { |
||
| 100 | throw ParseAtLocationException::issueParsing($e, "Result [$absoluteFileNameAsString]"); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | return $analysisResultsBuilder->build(); |
||
| 105 | } |
||
| 153 |