<commit_before><?php

declare(strict_types=1);

namespace PSR7SessionEncodeDecode;

final class Decoder implements DecoderInterface
{
    /**
     * {@inheritDoc}
     */
    public function __invoke(string $encodedSessionData): array
    {
        if('' === $encodedSessionData) {
            return [];
        }

        $arr = [];
        foreach (explode(';', rtrim($encodedSessionData, ';')) as $data) {
            [$key, $value] = explode('|', $data);

            $explodedData = explode(':', $value);

            // @todo refactor this conditional
            if (3 === count($explodedData)) {
                // @todo throw exception in case of wrong length
                [$type, $length, $rawValue] = [$explodedData[0], $explodedData[1], trim($explodedData[2], '"')];
            } else {
                [$type, $rawValue] = [$explodedData[0], $explodedData[1]];
            }

            // integer
            if ('i' === $type) {
                $arr[$key] = (int) $rawValue;
                continue;
            }

            // string
            if ('s' === $type) {
                $arr[$key] = (string) $rawValue;
                continue;
            }

            // bool
            if ('b' === $type) {
                $arr[$key] = (bool) $rawValue;
                continue;
            }

            // object
            if ('O' === $type) {
                $arr[$key] = (bool) $rawValue;
                continue;
            }
        }

        return $arr;
    }<commit_msg>simplify decoder logic<commit_after><?php

declare(strict_types=1);

namespace PSR7SessionEncodeDecode;

final class Decoder implements DecoderInterface
{
    /**
     * {@inheritDoc}
     */
    public function __invoke(string $encodedSessionData): array
    {
        if ('' === $encodedSessionData) {
            return [];
        }

        preg_match_all('/(^|;|\})(\w+)\|/i', $encodedSessionData, $matchesarray, PREG_OFFSET_CAPTURE);

        $decodedData = [];

        $lastOffset = null;
        $currentKey = '';
        foreach ($matchesarray[2] as $value) {
            $offset = $value[1];
            if (null !== $lastOffset) {
                $valueText = substr($encodedSessionData, $lastOffset, $offset - $lastOffset);

                /** @noinspection UnserializeExploitsInspection */
                $decodedData[$currentKey] = unserialize($valueText);
            }
            $currentKey = $value[0];

            $lastOffset = $offset + strlen($currentKey) + 1;
        }

        $valueText = substr($encodedSessionData, $lastOffset);

        /** @noinspection UnserializeExploitsInspection */
        $decodedData[$currentKey] = unserialize($valueText);

        return $decodedData;
    }
{'fraction_non_alphanumeric': 0.14824304538799415, 'fraction_numerical': 0.005124450951683748, 'mean_word_length': 1.7302697302697303, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '2306171', 'n_tokens_mistral': 800, 'n_tokens_neox': 759, 'n_words': 152}