Completed
Push — master ( 41fc83...fb17b5 )
by Luka
02:57
created

src/Object/TypeDefinition.php (4 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Teto\Object;
3
4
/**
5
 * Type Definition syntax
6
 *
7
 * @package    Teto
8
 * @subpackage Object
9
 * @copyright  2014 USAMI Kenta
10
 * @license    http://www.apache.org/licenses/LICENSE-2.0
11
 * @author     USAMI Kenta <[email protected]>
12
 *
13
 * @property-read string   $expected
14
 * @property-read boolean  $is_nullable
15
 * @property-read boolean  $is_array
16
 * @property-read int|null $len
17
 */
18
final class TypeDefinition
19
{
20
    /** @var string */
21
    private $expected;
1 ignored issue
show
The property $expected is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
22
23
    /** @var boolean */
24
    private $is_nullable;
1 ignored issue
show
The property $is_nullable is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
25
26
    /** @var boolean */
27
    private $is_array;
1 ignored issue
show
The property $is_array is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
28
29
    /** @var int|null */
30
    private $len;
1 ignored issue
show
The property $len is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
31
32
    public function __construct() {}
33
34
    /**
35
     * @param  string $def Type definition
36
     * @return TypeDefinition
37
     */
38 34
    public static function parse($def)
39
    {
40 34
        $type = new TypeDefinition;
41
42 34
        preg_match(self::RE_PROPERTY, $def, $matches);
43
44 34
        if (empty($matches[2])) {
45 5
            throw new \LogicException();
46
        }
47 29
        $type->is_nullable = !empty($matches[1]);
48 29
        $type->expected    = $matches[2];
49 29
        $type->is_array    = !empty($matches[3]);
50 29
        if (isset($matches[4]) && is_numeric($matches[4])) {
51 5
            $type->len = (int)$matches[4];
52
        }
53
54 29
        return $type;
55
    }
56
    const RE_PROPERTY = '/^(\??)([^\s\[\]?]+)((?:\[(\d*)\])?)$/';
57
58
    /**
59
     * @throws \OutOfRangeException
60
     */
61 29
    public function __get($name)
62
    {
63 29
        if (property_exists($this, $name)) {
64 29
            return $this->$name;
65
        }
66
67
        throw new \OutOfRangeException;
68
    }
69
}
70