TypeDefinition::__get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
1
<?php
2
namespace Teto\Object;
3
4
/**
5
 * Type Definition syntax
6
 *
7
 * @author    USAMI Kenta <[email protected]>
8
 * @copyright 2016 Baguette HQ
9
 * @license   http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * @property-read string   $expected
12
 * @property-read bool     $is_nullable
13
 * @property-read bool     $is_array
14
 * @property-read int|null $len
15
 */
16
final class TypeDefinition
17
{
18
    /** @var string */
19
    private $expected;
20
21
    /** @var bool */
22
    private $is_nullable;
23
24
    /** @var bool */
25
    private $is_array;
26
27
    /** @var int|null */
28
    private $len;
29
30
    public function __construct() {}
31
32
    /**
33
     * @param  string $def Type definition
34
     * @return TypeDefinition
35
     */
36 34
    public static function parse($def)
37
    {
38 34
        $type = new TypeDefinition;
39
40 34
        preg_match(self::RE_PROPERTY, $def, $matches);
41
42 34
        if (empty($matches[2])) {
43 5
            throw new \LogicException();
44
        }
45 29
        $type->is_nullable = !empty($matches[1]);
0 ignored issues
show
Bug introduced by
The property is_nullable is declared read-only in Teto\Object\TypeDefinition.
Loading history...
46 29
        $type->expected    = $matches[2];
0 ignored issues
show
Bug introduced by
The property expected is declared read-only in Teto\Object\TypeDefinition.
Loading history...
47 29
        $type->is_array    = !empty($matches[3]);
0 ignored issues
show
Bug introduced by
The property is_array is declared read-only in Teto\Object\TypeDefinition.
Loading history...
48 29
        if (isset($matches[4]) && is_numeric($matches[4])) {
49 5
            $type->len = (int)$matches[4];
0 ignored issues
show
Bug introduced by
The property len is declared read-only in Teto\Object\TypeDefinition.
Loading history...
50
        }
51
52 29
        return $type;
53
    }
54
    const RE_PROPERTY = '/^(\??)([^\s\[\]?]+)((?:\[(\d*)\])?)$/';
55
56
    /**
57
     * @throws \OutOfRangeException
58
     */
59 29
    public function __get($name)
60
    {
61 29
        if (property_exists($this, $name)) {
62 29
            return $this->$name;
63
        }
64
65
        throw new \OutOfRangeException;
66
    }
67
}
68