Code Duplication    Length = 51-57 lines in 2 locations

generated/src/xKerman_Restricted_ArrayHandler.php 1 location

@@ 6-56 (lines=51) @@
3
/**
4
 * Handler for PHP serialiezed array
5
 */
6
class xKerman_Restricted_ArrayHandler implements xKerman_Restricted_HandlerInterface
7
{
8
    /** @var ParserInterface $expressionParser parser for unserialize expression */
9
    private $expressionParser;
10
    /** @var integer */
11
    const CLOSE_BRACE_LENGTH = 1;
12
    /**
13
     * constructor
14
     *
15
     * @param ParserInterface $expressionParser parser for unserialize expression
16
     */
17
    public function __construct(xKerman_Restricted_ParserInterface $expressionParser)
18
    {
19
        $this->expressionParser = $expressionParser;
20
    }
21
    /**
22
     * parse given `$source` as PHP serialized array
23
     *
24
     * @param Source $source parser input
25
     * @param string $args   array length
26
     * @return array
27
     * @throws UnserializeFailedException
28
     */
29
    public function handle(xKerman_Restricted_Source $source, $args)
30
    {
31
        $length = intval($args, 10);
32
        $result = array();
33
        for ($i = 0; $i < $length; ++$i) {
34
            list($key, $source) = $this->parseKey($source);
35
            list($value, $source) = $this->expressionParser->parse($source);
36
            $result[$key] = $value;
37
        }
38
        $source->consume('}', self::CLOSE_BRACE_LENGTH);
39
        return array($result, $source);
40
    }
41
    /**
42
     * parse given `$source` as array key (s.t. integer|string)
43
     *
44
     * @param Source $source input
45
     * @return array
46
     * @throws UnserializeFailedException
47
     */
48
    private function parseKey($source)
49
    {
50
        list($key, $source) = $this->expressionParser->parse($source);
51
        if (!is_integer($key) && !is_string($key)) {
52
            return $source->triggerError();
53
        }
54
        return array($key, $source);
55
    }
56
}

src/ArrayHandler.php 1 location

@@ 10-66 (lines=57) @@
7
/**
8
 * Handler for PHP serialiezed array
9
 */
10
class ArrayHandler implements HandlerInterface
11
{
12
    /** @var ParserInterface $expressionParser parser for unserialize expression */
13
    private $expressionParser;
14
15
    /** @var integer */
16
    const CLOSE_BRACE_LENGTH = 1;
17
18
    /**
19
     * constructor
20
     *
21
     * @param ParserInterface $expressionParser parser for unserialize expression
22
     */
23
    public function __construct(ParserInterface $expressionParser)
24
    {
25
        $this->expressionParser = $expressionParser;
26
    }
27
28
    /**
29
     * parse given `$source` as PHP serialized array
30
     *
31
     * @param Source $source parser input
32
     * @param string $args   array length
33
     * @return array
34
     * @throws UnserializeFailedException
35
     */
36
    public function handle(Source $source, $args)
37
    {
38
        $length = intval($args, 10);
39
40
        $result = array();
41
        for ($i = 0; $i < $length; ++$i) {
42
            list($key, $source) = $this->parseKey($source);
43
            list($value, $source) = $this->expressionParser->parse($source);
44
            $result[$key] = $value;
45
        }
46
47
        $source->consume('}', self::CLOSE_BRACE_LENGTH);
48
        return array($result, $source);
49
    }
50
51
    /**
52
     * parse given `$source` as array key (s.t. integer|string)
53
     *
54
     * @param Source $source input
55
     * @return array
56
     * @throws UnserializeFailedException
57
     */
58
    private function parseKey($source)
59
    {
60
        list($key, $source) = $this->expressionParser->parse($source);
61
        if (!is_integer($key) && !is_string($key)) {
62
            return $source->triggerError();
63
        }
64
        return array($key, $source);
65
    }
66
}
67