Decoder::__invoke()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 17
cts 17
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 17
nc 4
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PSR7SessionEncodeDecode;
6
7
final class Decoder implements DecoderInterface
8
{
9
    /**
10
     * {@inheritDoc}
11
     */
12 4
    public function __invoke(string $encodedSessionData): array
13
    {
14 4
        if ('' === $encodedSessionData) {
15 1
            return [];
16
        }
17
18 3
        preg_match_all('/(^|;|\})(\w+)\|/i', $encodedSessionData, $matchesarray, PREG_OFFSET_CAPTURE);
19
20 3
        $decodedData = [];
21
22 3
        $lastOffset = null;
23 3
        $currentKey = '';
24 3
        foreach ($matchesarray[2] as $value) {
25 3
            $offset = $value[1];
26 3
            if (null !== $lastOffset) {
27 2
                $valueText = substr($encodedSessionData, $lastOffset, $offset - $lastOffset);
28
29
                /** @noinspection UnserializeExploitsInspection */
30 2
                $decodedData[$currentKey] = unserialize($valueText);
31
            }
32 3
            $currentKey = $value[0];
33
34 3
            $lastOffset = $offset + strlen($currentKey) + 1;
35
        }
36
37 3
        $valueText = substr($encodedSessionData, $lastOffset);
38
39
        /** @noinspection UnserializeExploitsInspection */
40 3
        $decodedData[$currentKey] = unserialize($valueText);
41
42 3
        return $decodedData;
43
    }
44
}
45