Completed
Pull Request — master (#4)
by Márk
06:46 queued 04:42
created

XmlReaderParserBitmaskGenerator::generate()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
ccs 18
cts 18
cp 1
rs 8.5806
cc 4
eloc 17
nc 6
nop 0
crap 4
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 2
    public function __construct()
61
    {
62 2
        $this->combinedTypes = [
63 2
            'expectedForMethodResponse' => ['params', 'fault'],
64 2
            'expectedForMember' => ['name', 'value'],
65 2
            'expectedForSimpleType' => ['#text', 'value'],
66 2
            'expectedForNil' => ['nil', 'value'],
67
            'expectedForValue' => [
68 2
                'string',
69 2
                'array',
70 2
                'struct',
71 2
                'int',
72 2
                'biginteger',
73 2
                'i8',
74 2
                'i4',
75 2
                'i2',
76 2
                'i1',
77 2
                'boolean',
78 2
                'double',
79 2
                'float',
80 2
                'bigdecimal',
81 2
                'dateTime.iso8601',
82 2
                'dateTime',
83 2
                'base64',
84 2
                'nil',
85 2
                'dom',
86 2
                '#text',
87 2
                'value',
88 2
            ],
89 2
            'expectedForStruct' => ['member', 'struct', 'value'],
90 2
            'expectedForData' => ['data', 'value', 'array'],
91
            'expectedAfterValue' => [
92 2
                'param',
93 2
                'value',
94 2
                'data',
95 2
                'member',
96 2
                'name',
97 2
                'int',
98 2
                'i4',
99 2
                'i2',
100 2
                'i1',
101 2
                'base64',
102 2
                'fault',
103 2
            ],
104 2
            'expectedAfterParam' => ['param', 'params'],
105 2
            'expectedAfterName' => ['value', 'member'],
106 2
            'expectedAfterMember' => ['struct', 'member'],
107 2
            'allFlags' => $this->basicTypes,
108
        ];
109
110 2
        $this->typeCount = count($this->basicTypes);
111 2
    }
112
113 1
    private function createBitmaskVariable($type, $bitmask, $prefix = '')
114
    {
115 1
        $variableName = preg_match('/^\w+[\d\w_]*$/', $type)
116 1
            ? 'static $'.$prefix.$type
117 1
            : '${\''.$prefix.$type.'\'}';
118 1
        $this->values[$type] = $bitmask;
119
120 1
        return $variableName.' = 0b'.sprintf('%0'.$this->typeCount.'b', $this->values[$type]).';';
121
    }
122
123 1
    public function generate()
124
    {
125 1
        $code = [];
126 1
        $bitmask = 1;
127 1
        foreach ($this->basicTypes as $type) {
128 1
            $code[] = $this->createBitmaskVariable($type, $bitmask, 'flag');
129 1
            $bitmask = $bitmask << 1;
130 1
        }
131
132 1
        foreach ($this->combinedTypes as $type => $combination) {
133 1
            $value = 0;
134 1
            foreach ($combination as $subType) {
135 1
                $value |= $this->values[$subType];
136 1
            }
137 1
            $code[] = $this->createBitmaskVariable($type, $value);
138 1
        }
139
140
        $commentStart = <<<'EOS'
141
// This following assignments are auto-generated using %s
142
// Don’t edit manually
143 1
EOS;
144
145 1
        $commentStart = sprintf($commentStart, __CLASS__);
146
147 1
        $commentEnd = '// End of auto-generated code';
148
149 1
        return $commentStart."\n".implode("\n", $code)."\n".$commentEnd;
150
    }
151
}
152