Completed
Push — master ( 6aef24...286712 )
by Márk
06:17
created

XmlReaderParserBitmaskGenerator::generate()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
ccs 0
cts 23
cp 0
rs 8.5806
cc 4
eloc 17
nc 6
nop 0
crap 20
1
<?php
2
3
namespace Fxmlrpc\Serialization\CodeGenerator;
4
5
/**
6
 * Generates bitmasks for XmlReaderParser.
7
 *
8
 * @author Lars Strojny <[email protected]>
9
 */
10
final class XmlReaderParserBitmaskGenerator
11
{
12
    /**
13
     * @var array
14
     */
15
    private $basicTypes = [
16
        'methodResponse',
17
        'params',
18
        'fault',
19
        'param',
20
        'value',
21
        'array',
22
        'member',
23
        'name',
24
        '#text',
25
        'string',
26
        'struct',
27
        'int',
28
        'biginteger',
29
        'i8',
30
        'i4',
31
        'i2',
32
        'i1',
33
        'boolean',
34
        'double',
35
        'float',
36
        'bigdecimal',
37
        'dateTime.iso8601',
38
        'dateTime',
39
        'base64',
40
        'nil',
41
        'dom',
42
        'data',
43
    ];
44
45
    /**
46
     * @var array
47
     */
48
    private $combinedTypes = [];
49
50
    /**
51
     * @var int
52
     */
53
    private $typeCount = 0;
54
55
    /**
56
     * @var array
57
     */
58
    private $values = [];
59
60
    public function __construct()
61
    {
62
        $this->combinedTypes = [
63
            'expectedForMethodResponse' => ['params', 'fault'],
64
            'expectedForMember' => ['name', 'value'],
65
            'expectedForSimpleType' => ['#text', 'value'],
66
            'expectedForNil' => ['nil', 'value'],
67
            'expectedForValue' => [
68
                'string',
69
                'array',
70
                'struct',
71
                'int',
72
                'biginteger',
73
                'i8',
74
                'i4',
75
                'i2',
76
                'i1',
77
                'boolean',
78
                'double',
79
                'float',
80
                'bigdecimal',
81
                'dateTime.iso8601',
82
                'dateTime',
83
                'base64',
84
                'nil',
85
                'dom',
86
                '#text',
87
                'value',
88
            ],
89
            'expectedForStruct' => ['member', 'struct', 'value'],
90
            'expectedForData' => ['data', 'value', 'array'],
91
            'expectedAfterValue' => [
92
                'param',
93
                'value',
94
                'data',
95
                'member',
96
                'name',
97
                'int',
98
                'i4',
99
                'i2',
100
                'i1',
101
                'base64',
102
                'fault',
103
            ],
104
            'expectedAfterParam' => ['param', 'params'],
105
            'expectedAfterName' => ['value', 'member'],
106
            'expectedAfterMember' => ['struct', 'member'],
107
            'allFlags' => $this->basicTypes,
108
        ];
109
110
        $this->typeCount = count($this->basicTypes);
111
    }
112
113
    private function createBitmaskVariable($type, $bitmask, $prefix = '')
114
    {
115
        $variableName = preg_match('/^\w+[\d\w_]*$/', $type)
116
            ? 'static $'.$prefix.$type
117
            : '${\''.$prefix.$type.'\'}';
118
        $this->values[$type] = $bitmask;
119
120
        return $variableName.' = 0b'.sprintf('%0'.$this->typeCount.'b', $this->values[$type]).';';
121
    }
122
123
    public function generate()
124
    {
125
        $code = [];
126
        $bitmask = 1;
127
        foreach ($this->basicTypes as $type) {
128
            $code[] = $this->createBitmaskVariable($type, $bitmask, 'flag');
129
            $bitmask = $bitmask << 1;
130
        }
131
132
        foreach ($this->combinedTypes as $type => $combination) {
133
            $value = 0;
134
            foreach ($combination as $subType) {
135
                $value |= $this->values[$subType];
136
            }
137
            $code[] = $this->createBitmaskVariable($type, $value);
138
        }
139
140
        $commentStart = <<<'EOS'
141
// This following assignments are auto-generated using %s
142
// Don’t edit manually
143
EOS;
144
145
        $commentStart = sprintf($commentStart, __CLASS__);
146
147
        $commentEnd = '// End of auto-generated code';
148
149
        return $commentStart."\n".implode("\n", $code)."\n".$commentEnd;
150
    }
151
}
152