#!/usr/bin/env php
<?php

/**
 * This file is part of the PHP CS Fixer utility, based on "FriendsOfPHP/PHP-CS-Fixer" package.
 *
 * Parent package authors:
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
 *
 * This source file is subject to the MIT license that is bundled with this source code in the file LICENSE.
 */

global $argv;

// Try to disable xdebug
try {
    if (\function_exists('xdebug_disable')) {
        xdebug_disable();
    }
} catch (\Exception $e) {
    // Do nothing
} catch (\Throwable $e) {
    // Do nothing
}

// IDK for a what - this is a copy & paste from vendor binary file
\set_error_handler(static function ($severity, $message, $file, $line) {
    if ($severity & error_reporting()) {
        throw new ErrorException($message, 0, $severity, $file, $line);
    }
});

// Require composer autoloader
if (file_exists($autoload = __DIR__ . '/../../autoload.php')) {
    require $autoload;
} else {
    require __DIR__ . '/vendor/autoload.php';
}

// Detect project root directory
$reflector = new ReflectionClass(\Composer\Autoload\ClassLoader::class);
$project_root_dir = dirname(dirname(dirname($reflector->getFileName()))) . '/';

// Try to detect - user pass own config, or not?
$user_config = null;
for ($i = 0; $i <= count($argv) - 1; ++$i) {
    if (\strpos((string)$argv[$i], '--config') !== false) {
        $user_config = isset($argv[$j = $i + 1])
            ? $argv[$j]
            : null;
        break;
    }
}

// Initialize input arguments object
$input = new \Symfony\Component\Console\Input\ArgvInput($user_config === null
    ? array_merge([
        null,
        'fix',
        '--verbose',
        '--config',
        __DIR__ . '/config.php',
    ], array_splice($argv, 1, \count($argv)))
    : null
);

// And start parent app
$application = new \PhpCsFixer\Console\Application;
$application->run($input);

__HALT_COMPILER();
