public static function unserialize($serialized)
    {
        if (!is_string($serialized)) {
            throw UnserializationFailedException::forValue($serialized);
        }

        $errorMessage = null;
        $errorCode = 0;

        set_error_handler(function ($errno, $errstr) use (&$errorMessage, &$errorCode) {
            $errorMessage = $errstr;
            $errorCode = $errno;
        });

        $value = unserialize($serialized);

        restore_error_handler();

        if (null !== $errorMessage) {
            if (false !== $pos = strpos($errorMessage, '): ')) {
                // cut "unserialize(%path%):" to make message more readable
                $errorMessage = substr($errorMessage, $pos + 3);
            }

            throw UnserializationFailedException::forValue($serialized, $errorMessage, $errorCode);
        }

        return $value;
    }