Passed
Pull Request — master (#45)
by X
02:29
created

xKerman_Restricted_ArrayHandler::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 2
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Handler for PHP serialiezed array
5
 */
6 View Code Duplication
class xKerman_Restricted_ArrayHandler implements xKerman_Restricted_HandlerInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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
0 ignored issues
show
Documentation introduced by
Should the type for parameter $expressionParser not be xKerman_Restricted_ParserInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
16
     */
17
    public function __construct(xKerman_Restricted_ParserInterface $expressionParser)
18
    {
19
        $this->expressionParser = $expressionParser;
0 ignored issues
show
Documentation Bug introduced by
It seems like $expressionParser of type object<xKerman_Restricted_ParserInterface> is incompatible with the declared type object<ParserInterface> of property $expressionParser.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
20
    }
21
    /**
22
     * parse given `$source` as PHP serialized array
23
     *
24
     * @param Source $source parser input
0 ignored issues
show
Documentation introduced by
Should the type for parameter $source not be xKerman_Restricted_Source?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
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
}