<?php

declare(strict_types=1);

/**
 * This file is part of CodeIgniter 4 framework.
 *
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

require __DIR__ . '/../system/Test/bootstrap.php';

use CodeIgniter\CLI\CLI;

$rstFilesWithTabs = (string) shell_exec('git grep -EIPn "\t" -- "*.rst"');
$rstFilesWithTabs = explode("\n", $rstFilesWithTabs);
$rstFilesWithTabs = array_map(static function (string $line): array {
    preg_match('/^(?P<file>[^:]+):(?P<line>[0-9]+):(?P<code>.+)$/', $line, $matches);

    return [
        'file' => $matches['file'],
        'line' => $matches['line'],
        'code' => $matches['code'],
    ];
}, array_filter($rstFilesWithTabs));

$normalizedRstFilesWithTabs = [];

foreach ($rstFilesWithTabs as ['file' => $file, 'line' => $line, 'code' => $code]) {
    if (! isset($normalizedRstFilesWithTabs[$file])) {
        $normalizedRstFilesWithTabs[$file] = [];
    }

    $normalizedRstFilesWithTabs[$file][] = ['line' => $line, 'code' => $code];
}

unset($rstFilesWithTabs);

if ($normalizedRstFilesWithTabs !== []) {
    printf(
        "%s\n\n%s\n",
        CLI::color('Tabs in RST files were detected:', 'white', 'red'),
        implode("\n", array_map(
            static function (string $file, array $parts): string {
                return sprintf(
                    "%s%s%s\n",
                    CLI::color('* in ', 'light_red'),
                    CLI::color($file, 'yellow'),
                    implode("\n", array_map(static function (array $line): string {
                        return sprintf(
                            '%s | %s',
                            str_pad($line['line'], 4, ' ', STR_PAD_LEFT),
                            str_replace("\t", CLI::color('    ', 'black', 'red'), $line['code']),
                        );
                    }, $parts)),
                );
            },
            array_keys($normalizedRstFilesWithTabs),
            array_values($normalizedRstFilesWithTabs),
        )),
    );

    exit(1);
}

CLI::write('No tabs in RST files were detected.', 'black', 'green');

exit(0);

Add detector for tabs in RST files
Add detector for tabs in RST files
