Completed
Push — master ( 5875b6...d65a71 )
by brian
01:43
created

Token::__toString()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 13

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 40
cts 40
cp 1
rs 6.6166
c 0
b 0
f 0
cc 13
nc 13
nop 0
crap 13

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
/**
4
 * @copyright   (c) 2017-present brian ridley
5
 * @author      brian ridley <[email protected]>
6
 * @license     http://opensource.org/licenses/MIT MIT
7
 */
8
9
namespace ptlis\SerializedDataEditor\Parser;
10
11
/**
12
 * A token from a serialized PHP value.
13
 */
14
final class Token
15
{
16
    const NULL = 'null';
17
    const BOOL = 'bool';
18
    const INTEGER = 'integer';
19
    const FLOAT = 'float';
20
    const STRING = 'string';
21
    const REFERENCE = 'reference';
22
    const ARRAY_START = 'array_start';
23
    const OBJECT_DEFAULT_NAME = 'object_default_name';
24
    const OBJECT_CUSTOM_NAME = 'object_custom_name';
25
    const OBJECT_CUSTOM_DATA = 'object_custom_data';
26
    const OBJECT_MEMBER_COUNT = 'object_member_count';
27
    const COMPOUND_END = 'compound_end'; // array or object close
28
29
    const PREFIX_NULL = 'N';
30
    const PREFIX_BOOL = 'b';
31
    const PREFIX_INTEGER = 'i';
32
    const PREFIX_FLOAT = 'd';
33
    const PREFIX_STRING = 's';
34
    const PREFIX_REFERENCE = 'R';
35
    const PREFIX_ARRAY_START = 'a';
36
    const PREFIX_OBJECT_DEFAULT_NAME = 'O';
37
    const PREFIX_OBJECT_CUSTOM_NAME = 'C';
38
    const PREFIX_COMPOUND_END = '}';
39
40
    /** @var string */
41
    private $type;
42
43
    /** @var string|null */
44
    private $value;
45
46 45
    public function __construct(
47
        string $type,
48
        ?string $value = null
49
    ) {
50 45
        $this->type = $type;
51 45
        $this->value = $value;
52 45
    }
53
54 36
    public function getType(): string
55
    {
56 36
        return $this->type;
57
    }
58
59 35
    public function getValue(): ?string
60
    {
61 35
        return $this->value;
62
    }
63
64 10
    public function __toString(): string
65
    {
66 10
        $str = '';
67 10
        switch ($this->type) {
68 10
            case self::NULL:
69 1
                $str = self::PREFIX_NULL . ';';
70 1
                break;
71
72 9
            case self::BOOL:
73 2
                $str = self::PREFIX_BOOL . ':' . $this->value . ';';
74 2
                break;
75
76 8
            case self::INTEGER:
77 4
                $str = self::PREFIX_INTEGER . ':' . $this->value . ';';
78 4
                break;
79
80 7
            case self::FLOAT:
81 1
                $str = self::PREFIX_FLOAT . ':' . $this->value . ';';
82 1
                break;
83
84 6
            case self::STRING:
85 5
                $str = self::PREFIX_STRING . ':' . strlen($this->value) . ':"' . $this->value . '";';
86 5
                break;
87
88 5
            case self::REFERENCE:
89 1
                $str = self::PREFIX_REFERENCE . ':' . $this->value . ';';
90 1
                break;
91
92 5
            case self::ARRAY_START:
93 3
                $str = self::PREFIX_ARRAY_START . ':' . $this->value . ':{';
94 3
                break;
95
96 5
            case self::OBJECT_DEFAULT_NAME:
97 2
                $str = self::PREFIX_OBJECT_DEFAULT_NAME . ':' . strlen($this->value) . ':"' . $this->value . '":';
98 2
                break;
99
100 5
            case self::OBJECT_MEMBER_COUNT:
101 2
                $str = $this->value . ':{';
102 2
                break;
103
104 5
            case self::OBJECT_CUSTOM_NAME:
105 1
                $str = self::PREFIX_OBJECT_CUSTOM_NAME . ':' . strlen($this->value) . ':"' . $this->value . '":';
106 1
                break;
107
108 5
            case self::OBJECT_CUSTOM_DATA:
109 1
                $str = strlen($this->value) . ':{' . $this->value;
110 1
                break;
111
112 5
            case self::COMPOUND_END:
113 5
                $str = '}';
114 5
                break;
115
        }
116
117 10
        return $str;
118
    }
119
}