<commit_before><?php

/*
 * This file is part of the PHP Serialization Helpers package.
 *
 * © <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE.md
 * file that was distributed with this source code.
 */

namespace Cs278\SerializationHelpers;

use Cs278\SerializationHelpers\Exception\SyntaxError;
use Cs278\SerializationHelpers\Exception\SyntaxError\UnknownException;
use Cs278\SerializationHelpers\Exception\SyntaxErrorFactory;

/**
 * Wrapper around unserialize() that converts syntax errors to exceptions.
 *
 * @param string $input Serialized string
 *
 * @return mixed
 *
 * @throws SyntaxError If there was an error unserializing the string
 */
function unserialize($input)
{
    static $errorHandler;
    static $exceptionFactory;

    $currentHandler = null;

    if (!$errorHandler) {
        $exceptionFactory = new SyntaxErrorFactory();
        $errorHandler = function ($code, $message, $file, $line) use ($exceptionFactory, &$currentHandler) {
            if ($code === E_NOTICE) {
                $e = new \ErrorException($message, $code, 0, $file, $line);

                throw $exceptionFactory->createFromErrorException($e);
            }

            if ($currentHandler) {
                return call_user_func_array($currentHandler, func_get_args());
            }

            return false;
        };
    }

    $currentHandler = set_error_handler($errorHandler);

    try {
        $result = \unserialize($input);

        if ($result === false && $input !== 'b:0;') {
            throw new UnknownException($input, 'Unknown error was encountered');
        }
    } catch (\Exception $e) {
        // Ensure the error handler is restored, even if an exception occurs.
        restore_error_handler();

        throw $e;
    }

    restore_error_handler();

    return $result;
}

/**
 * Tests if an input is valid PHP serialized string.
 *
 * Checks if a string is serialized using quick string manipulation
 * to throw out obviously incorrect strings. Unserialize is then run
 * on the string to perform the final verification.
 *
 * Valid serialized forms are the following:
 * <ul>
 * <li>boolean: <code>b:1;</code></li>
 * <li>integer: <code>i:1;</code></li>
 * <li>double: <code>d:0.2;</code></li>
 * <li>string: <code>s:4:"test";</code></li>
 * <li>array: <code>a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}</code></li>
 * <li>object: <code>O:8:"stdClass":0:{}</code></li>
 * <li>object (implementing Serializable): <code>C:8:"stdClass":0:{}</code></li>
 * <li>null: <code>N;</code></li>
 * </ul>
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright © 2009, 2014 <NAME>
 *<commit_msg>Use finally instead of duplicated calls<commit_after><?php

/*
 * This file is part of the PHP Serialization Helpers package.
 *
 * © <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE.md
 * file that was distributed with this source code.
 */

namespace Cs278\SerializationHelpers;

use Cs278\SerializationHelpers\Exception\SyntaxError;
use Cs278\SerializationHelpers\Exception\SyntaxError\UnknownException;
use Cs278\SerializationHelpers\Exception\SyntaxErrorFactory;

/**
 * Wrapper around unserialize() that converts syntax errors to exceptions.
 *
 * @param string $input Serialized string
 *
 * @return mixed
 *
 * @throws SyntaxError If there was an error unserializing the string
 */
function unserialize($input)
{
    static $errorHandler;
    static $exceptionFactory;

    $currentHandler = null;

    if (!$errorHandler) {
        $exceptionFactory = new SyntaxErrorFactory();
        $errorHandler = function ($code, $message, $file, $line) use ($exceptionFactory, &$currentHandler) {
            if ($code === E_NOTICE) {
                $e = new \ErrorException($message, $code, 0, $file, $line);

                throw $exceptionFactory->createFromErrorException($e);
            }

            if ($currentHandler) {
                return call_user_func_array($currentHandler, func_get_args());
            }

            return false;
        };
    }

    $currentHandler = set_error_handler($errorHandler);

    try {
        $result = \unserialize($input);

        if ($result === false && $input !== 'b:0;') {
            throw new UnknownException($input, 'Unknown error was encountered');
        }
    } catch (\Exception $e) {
        throw $e;
    } finally {
        // Ensure the error handler is restored.
        restore_error_handler();
    }

    return $result;
}

/**
 * Tests if an input is valid PHP serialized string.
 *
 * Checks if a string is serialized using quick string manipulation
 * to throw out obviously incorrect strings. Unserialize is then run
 * on the string to perform the final verification.
 *
 * Valid serialized forms are the following:
 * <ul>
 * <li>boolean: <code>b:1;</code></li>
 * <li>integer: <code>i:1;</code></li>
 * <li>double: <code>d:0.2;</code></li>
 * <li>string: <code>s:4:"test";</code></li>
 * <li>array: <code>a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}</code></li>
 * <li>object: <code>O:8:"stdClass":0:{}</code></li>
 * <li>object (implementing Serializable): <code>C:8:"stdClass":0:{}</code></li>
 * <li>null: <code>N;</code></li>
 * </ul>
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright © 2009, 2014 <NAME>
 *
{'fraction_non_alphanumeric': 0.1453444360333081, 'fraction_numerical': 0.014383043149129448, 'mean_word_length': 3.251810136765889, 'pattern_counts': {'":': 4, '<': 87, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2320141', 'n_tokens_mistral': 1624, 'n_tokens_neox': 1570, 'n_words': 434}