HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/work.naniguide.com/vendor/phpro/grumphp/src/Formatter/PhpCsFixerFormatter.php
<?php

declare(strict_types=1);

namespace GrumPHP\Formatter;

use Symfony\Component\Process\Process;

class PhpCsFixerFormatter implements ProcessFormatterInterface
{
    /**
     * @var int
     */
    private $counter = 0;

    /**
     * Resets the internal counter.
     */
    public function resetCounter(): void
    {
        $this->counter = 0;
    }

    public function format(Process $process): string
    {
        $output = $process->getOutput();
        if (!$output) {
            return $process->getErrorOutput();
        }

        if (!$json = json_decode($output, true)) {
            return $output;
        }

        return $this->formatJsonResponse($json);
    }

    private function formatJsonResponse(array $json): string
    {
        $formatted = [];
        foreach ($json['files'] as $file) {
            if (!\is_array($file) || !isset($file['name'])) {
                $formatted[] = 'Invalid file: '.print_r($file, true);
                continue;
            }

            $formatted[] = $this->formatFile($file);
        }

        return implode(PHP_EOL, $formatted);
    }

    private function formatFile(array $file): string
    {
        if (!isset($file['name'])) {
            return 'Invalid file: '.print_r($file, true);
        }

        $hasFixers = isset($file['appliedFixers']);
        $hasDiff = isset($file['diff']);

        return sprintf(
            '%s) %s%s%s',
            ++$this->counter,
            $file['name'],
            $hasFixers ? ' ('.implode(', ', $file['appliedFixers']).')' : '',
            $hasDiff ? PHP_EOL.PHP_EOL.$file['diff'] : ''
        );
    }
}