EscapedStringHandler::handle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 15
ccs 12
cts 12
cp 1
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
/**
3
 * handler for escaped string
4
 */
5
namespace xKerman\Restricted;
6
7
/**
8
 * Handler for escaped string
9
 */
10
class EscapedStringHandler implements HandlerInterface
11
{
12
    /** @var integer */
13
    const CLOSE_STRING_LENGTH = 2;
14
15
    /**
16
     * parse given `$source` as escaped string
17
     *
18
     * @param Source      $source parser input
19
     * @param string|null $args   string length
20
     * @return array
21
     * @throws UnserializeFailedException
22
     */
23 13
    public function handle(Source $source, $args)
24
    {
25 13
        $length = intval($args, 10);
26 13
        $result = array();
27 13
        for ($i = 0; $i < $length; ++$i) {
28 12
            $char = $source->read(1);
29 12
            if ($char !== '\\') {
30 3
                $result[] = $char;
31 3
                continue;
32
            }
33 9
            $hex = $source->match('/\G([0-9a-fA-F]{2})/');
34 6
            $result[] = chr(intval($hex[0], 16));
35
        }
36 10
        $source->consume('";', self::CLOSE_STRING_LENGTH);
37 9
        return array(implode('', $result), $source);
38
    }
39
}
40