Skip to content

Commit

Permalink
refactor AnalyzerResultReader
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Dec 23, 2022
1 parent b12e8b7 commit af97c0f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 51 deletions.
120 changes: 70 additions & 50 deletions lib/AnalyzerResultReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,7 @@ final class AnalyzerResultReader {
*/
public function readFile(string $filePath): array
{
\Safe\fwrite(STDERR, 'Reading file '.$filePath.PHP_EOL);

$content = file_get_contents($filePath);
if ($content === '') {
throw new \RuntimeException('File '. $filePath .' is empty');
}
$json = json_decode($content, true);
if (!is_array($json)) {
throw new \RuntimeException('Expecting array, got ' . gettype($json));
}
$json = $this->readResultArray($filePath);

$decoded = [];
foreach ($json as $data) {
Expand All @@ -40,49 +31,78 @@ public function readFile(string $filePath): array
throw new \RuntimeException('Expecting array, got ' . gettype($resultArray));
}

$result = new AnalyzerResult();
if (array_key_exists(ResultPrinter::KEY_REFERENCE_DATE, $resultArray)) {

$dt = \DateTimeImmutable::createFromFormat(
ResultPrinter::DATE_FORMAT,
$resultArray[ResultPrinter::KEY_REFERENCE_DATE]
);
if ($dt !== false) {
$result->referenceDate = $dt;
}
}
if (array_key_exists(ResultPrinter::KEY_OVERALL_ERRORS, $resultArray)) {
$result->overallErrors = $resultArray[ResultPrinter::KEY_OVERALL_ERRORS];
}
if (array_key_exists(ResultPrinter::KEY_CLASSES_COMPLEXITY, $resultArray)) {
$result->classesComplexity = $resultArray[ResultPrinter::KEY_CLASSES_COMPLEXITY];
}
if (array_key_exists(ResultPrinter::KEY_DEPRECATIONS, $resultArray)) {
$result->deprecations = $resultArray[ResultPrinter::KEY_DEPRECATIONS];
}
if (array_key_exists(ResultPrinter::KEY_INVALID_PHPDOCS, $resultArray)) {
$result->invalidPhpdocs = $resultArray[ResultPrinter::KEY_INVALID_PHPDOCS];
}
if (array_key_exists(ResultPrinter::KEY_UNKNOWN_TYPES, $resultArray)) {
$result->unknownTypes = $resultArray[ResultPrinter::KEY_UNKNOWN_TYPES];
}
if (array_key_exists(ResultPrinter::KEY_ANONYMOUS_VARIABLES, $resultArray)) {
$result->anonymousVariables = $resultArray[ResultPrinter::KEY_ANONYMOUS_VARIABLES];
}
if (array_key_exists(ResultPrinter::KEY_PROPERTY_TYPE_COVERAGE, $resultArray)) {
$result->propertyTypeCoverage = $resultArray[ResultPrinter::KEY_PROPERTY_TYPE_COVERAGE];
}
if (array_key_exists(ResultPrinter::KEY_PARAM_TYPE_COVERAGE, $resultArray)) {
$result->paramTypeCoverage = $resultArray[ResultPrinter::KEY_PARAM_TYPE_COVERAGE];
}
if (array_key_exists(ResultPrinter::KEY_RETURN_TYPE_COVERAGE, $resultArray)) {
$result->returnTypeCoverage = $resultArray[ResultPrinter::KEY_RETURN_TYPE_COVERAGE];
}

$decoded[$baselinePath] = $result;
$decoded[$baselinePath] = $this->buildAnalyzerResult($resultArray);
}
}

return $decoded;
}

/**
* @param string $filePath
* @return array<mixed>
* @throws \Safe\Exceptions\FilesystemException
* @throws \Safe\Exceptions\JsonException
*/
private function readResultArray(string $filePath): array
{
\Safe\fwrite(STDERR, 'Reading file ' . $filePath . PHP_EOL);

$content = file_get_contents($filePath);
if ($content === '') {
throw new \RuntimeException('File ' . $filePath . ' is empty');
}
$json = json_decode($content, true);
if (!is_array($json)) {
throw new \RuntimeException('Expecting array, got ' . get_debug_type($json));
}

return $json;
}

/**
* @param array<mixed> $resultArray
*/
private function buildAnalyzerResult(array $resultArray): AnalyzerResult
{
$result = new AnalyzerResult();
if (array_key_exists(ResultPrinter::KEY_REFERENCE_DATE, $resultArray)) {

$dt = \DateTimeImmutable::createFromFormat(
ResultPrinter::DATE_FORMAT,
$resultArray[ResultPrinter::KEY_REFERENCE_DATE]
);
if ($dt !== false) {
$result->referenceDate = $dt;
}
}
if (array_key_exists(ResultPrinter::KEY_OVERALL_ERRORS, $resultArray)) {
$result->overallErrors = $resultArray[ResultPrinter::KEY_OVERALL_ERRORS];
}
if (array_key_exists(ResultPrinter::KEY_CLASSES_COMPLEXITY, $resultArray)) {
$result->classesComplexity = $resultArray[ResultPrinter::KEY_CLASSES_COMPLEXITY];
}
if (array_key_exists(ResultPrinter::KEY_DEPRECATIONS, $resultArray)) {
$result->deprecations = $resultArray[ResultPrinter::KEY_DEPRECATIONS];
}
if (array_key_exists(ResultPrinter::KEY_INVALID_PHPDOCS, $resultArray)) {
$result->invalidPhpdocs = $resultArray[ResultPrinter::KEY_INVALID_PHPDOCS];
}
if (array_key_exists(ResultPrinter::KEY_UNKNOWN_TYPES, $resultArray)) {
$result->unknownTypes = $resultArray[ResultPrinter::KEY_UNKNOWN_TYPES];
}
if (array_key_exists(ResultPrinter::KEY_ANONYMOUS_VARIABLES, $resultArray)) {
$result->anonymousVariables = $resultArray[ResultPrinter::KEY_ANONYMOUS_VARIABLES];
}
if (array_key_exists(ResultPrinter::KEY_PROPERTY_TYPE_COVERAGE, $resultArray)) {
$result->propertyTypeCoverage = $resultArray[ResultPrinter::KEY_PROPERTY_TYPE_COVERAGE];
}
if (array_key_exists(ResultPrinter::KEY_PARAM_TYPE_COVERAGE, $resultArray)) {
$result->paramTypeCoverage = $resultArray[ResultPrinter::KEY_PARAM_TYPE_COVERAGE];
}
if (array_key_exists(ResultPrinter::KEY_RETURN_TYPE_COVERAGE, $resultArray)) {
$result->returnTypeCoverage = $resultArray[ResultPrinter::KEY_RETURN_TYPE_COVERAGE];
}
return $result;
}
}
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ parameters:

cognitive_complexity:
class: 50
function: 10
function: 12

type_coverage:
return_type: 100
Expand Down

0 comments on commit af97c0f

Please sign in to comment.