#!/usr/bin/env php
<?php
// Run all project files through the parallel-linter
$output = shell_exec('parallel-lint --no-colors --no-progress --json ./src ./test');
$output = json_decode($output, true);

if (!count($output["results"]["filesWithSyntaxError"])) {
    echo "\e[0;32mFound no issues with code quality.\e[0m";
    echo "\n";
    exit(0);
} else {
    $totalNumberOfIssues = count($output["results"]["filesWithSyntaxError"]);

    // Render report
    echo "\e[0;31mFound "
        . (($totalNumberOfIssues === 1)
            ? '1 issue'
            : $totalNumberOfIssues . ' issues')
        . " with code quality.\e[0m";
    echo "\n";

    $errors = $output["results"]["errors"];

    unset($file);
    foreach ($output["results"]["filesWithSyntaxError"] as $file) {
        echo "\n";
        echo "\e[1;37m" . str_replace('"', '', $file) . "\e[0m";
        echo "\n\n";

        $errors_in_file = array_filter($errors, fn ($elem) => $elem["file"] === $file);

        foreach ($errors_in_file as $error) {
            echo "\e[2m" . str_pad('  L' . $error['line'], 7) . " | \e[0m";
            echo "\e[0;31mERR:\e[0m  ";
            echo $error['message'];
            echo "\n";
        }
    }

    exit(1);
}
