<?php declare(strict_types=1);
/*
 * This file is part of PHPUnit.
 *
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace PHPUnit\Util;

use const PHP_MAJOR_VERSION;
use const PHP_MINOR_VERSION;
use function array_reverse;
use function array_shift;
use function assert;
use function defined;
use function get_defined_constants;
use function get_included_files;
use function in_array;
use function ini_get_all;
use function is_array;
use function is_file;
use function is_scalar;
use function is_string;
use function preg_match;
use function serialize;
use function sprintf;
use function str_ends_with;
use function str_starts_with;
use function strtr;
use function var_export;
use Closure;
use Throwable;

/**
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
 *
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
 */
final readonly class GlobalState
{
    /**
     * @var non-empty-list<non-empty-string>
     */
    private const array SUPER_GLOBAL_ARRAYS = [
        '_ENV',
        '_POST',
        '_GET',
        '_COOKIE',
        '_SERVER',
        '_FILES',
        '_REQUEST',
    ];

    /**
     * Keys in $_SERVER that are populated by the SAPI in the child process
     * and must therefore not be preserved from the parent process.
     *
     * @var non-empty-list<non-empty-string>
     */
    private const array SAPI_SERVER_KEYS = [
        'PHP_SELF',
        'SCRIPT_NAME',
        'SCRIPT_FILENAME',
        'PATH_TRANSLATED',
        'DOCUMENT_ROOT',
        'REQUEST_TIME',
        'REQUEST_TIME_FLOAT',
        'argv',
        'argc',
    ];

    /**
     * @var non-empty-array<non-empty-string, non-empty-array<non-empty-string, true>>
     */
    private const array DEPRECATED_INI_SETTINGS = [
        '7.3' => [
            'iconv.input_encoding'       => true,
            'iconv.output_encoding'      => true,
            'iconv.internal_encoding'    => true,
            'mbstring.func_overload'     => true,
            'mbstring.http_input'        => true,
            'mbstring.http_output'       => true,
            'mbstring.internal_encoding' => true,
            'string.strip_tags'          => true,
        ],

        '7.4' => [
            'iconv.input_encoding'       => true,
            'iconv.output_encoding'      => true,
            'iconv.internal_encoding'    => true,
            'mbstring.func_overload'     => true,
            'mbstring.http_input'        => true,
            'mbstring.http_output'       => true,
            'mbstring.internal_encoding' => true,
            'pdo_odbc.db2_instance_name' => true,
            'string.strip_tags'          => true,
        ],

        '8.0' => [
            'iconv.input_encoding'       => true,
            'iconv.output_encoding'      => true,
            'iconv.internal_encoding'    => true,
            'mbstring.http_input'        => true,
            'mbstring.http_output'       => true,
            'mbstring.internal_encoding' => true,
        ],

        '8.1' => [
            'auto_detect_line_endings'     => true,
            'filter.default'               => true,
            'iconv.input_encoding'         => true,
            'iconv.output_encoding'        => true,
            'iconv.internal_encoding'      => true,
            'mbstring.http_input'          => true,
            'mbstring.http_output'         => true,
            'mbstring.internal_encoding'   => true,
            'oci8.old_oci_close_semantics' => true,
        ],

        '8.2' => [
            'auto_detect_line_endings'     => true,
            'filter.default'               => true,
            'iconv.input_encoding'         => true,
            'iconv.output_encoding'        => true,
            'iconv.internal_encoding'      => true,
            'mbstring.http_input'          => true,
            'mbstring.http_output'         => true,
            'mbstring.internal_encoding'   => true,
            'oci8.old_oci_close_semantics' => true,
        ],

        '8.3' => [
            'auto_detect_line_endings'     => true,
            'filter.default'               => true,
            'iconv.input_encoding'         => true,
            'iconv.output_encoding'        => true,
            'iconv.internal_encoding'      => true,
            'mbstring.http_input'          => true,
            'mbstring.http_output'         => true,
            'mbstring.internal_encoding'   => true,
            'oci8.old_oci_close_semantics' => true,
        ],

        '8.4' => [
            'auto_detect_line_endings'     => true,
            'filter.default'               => true,
            'iconv.input_encoding'         => true,
            'iconv.output_encoding'        => true,
            'iconv.internal_encoding'      => true,
            'mbstring.http_input'          => true,
            'mbstring.http_output'         => true,
            'mbstring.internal_encoding'   => true,
            'oci8.old_oci_close_semantics' => true,
        ],

        '8.5' => [
            'auto_detect_line_endings'     => true,
            'filter.default'               => true,
            'iconv.input_encoding'         => true,
            'iconv.output_encoding'        => true,
            'iconv.internal_encoding'      => true,
            'mbstring.http_input'          => true,
            'mbstring.http_output'         => true,
            'mbstring.internal_encoding'   => true,
            'oci8.old_oci_close_semantics' => true,
        ],

        '8.6' => [
            'auto_detect_line_endings'     => true,
            'filter.default'               => true,
            'iconv.input_encoding'         => true,
            'iconv.output_encoding'        => true,
            'iconv.internal_encoding'      => true,
            'mbstring.http_input'          => true,
            'mbstring.http_output'         => true,
            'mbstring.internal_encoding'   => true,
            'oci8.old_oci_close_semantics' => true,
        ],
    ];

    /**
     * @throws Exception
     */
    public static function getIncludedFilesAsString(): string
    {
        return self::processIncludedFilesAsString(get_included_files());
    }

    /**
     * @param list<string> $files
     *
     * @throws Exception
     */
    public static function processIncludedFilesAsString(array $files): string
    {
        $excludeList = new ExcludeList;
        $prefix      = false;
        $result      = '';

        if (defined('__PHPUNIT_PHAR__') && is_string(__PHPUNIT_PHAR__)) {
            // @codeCoverageIgnoreStart
            $prefix = 'phar://' . __PHPUNIT_PHAR__ . '/';
            // @codeCoverageIgnoreEnd
        }

        // Do not process bootstrap script
        array_shift($files);

        // If bootstrap script was a Composer bin proxy, skip the second entry as well
        if (isset($files[0]) && str_ends_with(strtr($files[0], '\\', '/'), '/phpunit/phpunit/phpunit')) {
            // @codeCoverageIgnoreStart
            array_shift($files);
            // @codeCoverageIgnoreEnd
        }

        foreach (array_reverse($files) as $file) {
            if (isset($GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST']) &&
                is_array($GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST']) &&
                $GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST'] !== [] &&
                in_array($file, $GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST'], true)) {
                continue;
            }

            if ($prefix !== false && str_starts_with($file, $prefix)) {
                continue;
            }

            // Skip virtual file system protocols
            if (preg_match('/^(vfs|phpvfs[a-z0-9]+):/', $file) > 0) {
                continue;
            }

            if (!$excludeList->isExcluded($file) && is_file($file)) {
                $result = 'require_once \'' . $file . "';\n" . $result;
            }
        }

        return $result;
    }

    public static function getIniSettingsAsString(): string
    {
        $result = '';

        $iniSettings = ini_get_all(null, false);

        assert($iniSettings !== false);

        foreach ($iniSettings as $key => $value) {
            if (self::isIniSettingDeprecated($key)) {
                continue;
            }

            if (!is_scalar($value)) {
                continue;
            }

            $result .= sprintf(
                '@ini_set(%s, %s);' . "\n",
                self::exportVariable($key),
                self::exportVariable((string) $value),
            );
        }

        return $result;
    }

    public static function getConstantsAsString(): string
    {
        $constants = get_defined_constants(true);
        $result    = '';

        if (isset($constants['user'])) {
            foreach ($constants['user'] as $name => $value) {
                $result .= sprintf(
                    'if (!defined(\'%s\')) define(\'%s\', %s);' . "\n",
                    $name,
                    $name,
                    self::exportVariable($value),
                );
            }
        }

        return $result;
    }

    public static function exportGlobals(): GlobalStateResult
    {
        $result         = '';
        $skippedGlobals = [];

        foreach (self::SUPER_GLOBAL_ARRAYS as $superGlobalArray) {
            if (isset($GLOBALS[$superGlobalArray]) && is_array($GLOBALS[$superGlobalArray])) {
                foreach ($GLOBALS[$superGlobalArray] as $key => $value) {
                    if ($superGlobalArray === '_SERVER' && in_array($key, self::SAPI_SERVER_KEYS, true)) {
                        continue;
                    }

                    $name = sprintf('$GLOBALS[\'%s\'][\'%s\']', $superGlobalArray, $key);

                    if ($value instanceof Closure) {
                        $skippedGlobals[] = ['name' => $name, 'reason' => 'is a Closure'];

                        continue;
                    }

                    try {
                        $result .= sprintf(
                            '$GLOBALS[\'%s\'][\'%s\'] = %s;' . "\n",
                            $superGlobalArray,
                            $key,
                            self::exportVariable($GLOBALS[$superGlobalArray][$key]),
                        );
                    } catch (Throwable) {
                        $skippedGlobals[] = ['name' => $name, 'reason' => 'is not serializable'];
                    }
                }
            }
        }

        $excludeList   = self::SUPER_GLOBAL_ARRAYS;
        $excludeList[] = 'GLOBALS';

        foreach ($GLOBALS as $key => $value) {
            if (in_array($key, $excludeList, true)) {
                continue;
            }

            $name = sprintf('$GLOBALS[\'%s\']', $key);

            if ($value instanceof Closure) {
                $skippedGlobals[] = ['name' => $name, 'reason' => 'is a Closure'];

                continue;
            }

            try {
                $result .= sprintf(
                    '$GLOBALS[\'%s\'] = %s;' . "\n",
                    $key,
                    self::exportVariable($value),
                );
            } catch (Throwable) {
                $skippedGlobals[] = ['name' => $name, 'reason' => 'is not serializable'];
            }
        }

        return new GlobalStateResult($result, $skippedGlobals);
    }

    private static function exportVariable(mixed $variable): string
    {
        if (is_scalar($variable) || $variable === null ||
            (is_array($variable) && self::arrayOnlyContainsScalars($variable))) {
            return var_export($variable, true);
        }

        return 'unserialize(' . var_export(serialize($variable), true) . ')';
    }

    /**
     * @param array<mixed> $array
     */
    private static function arrayOnlyContainsScalars(array $array): bool
    {
        $result = true;

        foreach ($array as $element) {
            if (is_array($element)) {
                $result = self::arrayOnlyContainsScalars($element);
            } elseif (!is_scalar($element) && $element !== null) {
                $result = false;
            }

            if (!$result) {
                break;
            }
        }

        return $result;
    }

    private static function isIniSettingDeprecated(string $iniSetting): bool
    {
        return isset(self::DEPRECATED_INI_SETTINGS[PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION][$iniSetting]);
    }
}
