Passed
Push — master ( c48b96...96b3cc )
by smiley
01:24
created
src/DTBLReader.php 1 patch
Spacing   +31 added lines, -31 removed lines patch added patch discarded remove patch
@@ -25,7 +25,7 @@  discard block
 block discarded – undo
25 25
  * @property array  $cols
26 26
  * @property array  $data
27 27
  */
28
-class DTBLReader{
28
+class DTBLReader {
29 29
 	use LoggerAwareTrait;
30 30
 
31 31
 	protected const FORMAT_HEADER = 'a4Signature/LVersion/QTableNameLength/QUnknown1/QRecordSize/QFieldCount/QDescriptionOffset/QRecordCount/QFullRecordSize/QEntryOffset/QNextId/QIDLookupOffset/QUnknown2';
@@ -68,9 +68,9 @@  discard block
 block discarded – undo
68 68
 	 *
69 69
 	 * @throws \codemasher\WildstarDB\WSDBException
70 70
 	 */
71
-	public function __construct(LoggerInterface $logger = null){
71
+	public function __construct(LoggerInterface $logger = null) {
72 72
 
73
-		if(PHP_INT_SIZE < 8){
73
+		if (PHP_INT_SIZE < 8) {
74 74
 			throw new WSDBException('64-bit PHP required');
75 75
 		}
76 76
 
@@ -80,9 +80,9 @@  discard block
 block discarded – undo
80 80
 	/**
81 81
 	 * @return void
82 82
 	 */
83
-	public function __destruct(){
83
+	public function __destruct() {
84 84
 
85
-		if(is_resource($this->fh)){
85
+		if (is_resource($this->fh)) {
86 86
 			fclose($this->fh);
87 87
 		}
88 88
 
@@ -93,7 +93,7 @@  discard block
 block discarded – undo
93 93
 	 *
94 94
 	 * @return mixed|null
95 95
 	 */
96
-	public function __get(string $name){
96
+	public function __get(string $name) {
97 97
 		return property_exists($this, $name) && $name !== 'fh' ? $this->{$name} : null;
98 98
 	}
99 99
 
@@ -118,13 +118,13 @@  discard block
 block discarded – undo
118 118
 		$this->cols = [];
119 119
 		$this->data = [];
120 120
 
121
-		if(strlen($header) !== 0x60){
121
+		if (strlen($header) !== 0x60) {
122 122
 			throw new WSDBException('cannot read DTBL header');
123 123
 		}
124 124
 
125 125
 		$this->header = unpack($this::FORMAT_HEADER, $header);
126 126
 
127
-		if($this->header['Signature'] !== "\x4c\x42\x54\x44"){ // LBTD
127
+		if ($this->header['Signature'] !== "\x4c\x42\x54\x44") { // LBTD
128 128
 			throw new WSDBException('invalid DTBL');
129 129
 		}
130 130
 
@@ -134,17 +134,17 @@  discard block
 block discarded – undo
134 134
 
135 135
 		fseek($this->fh, $this->header['DescriptionOffset'] + 0x60);
136 136
 
137
-		for($i = 0; $i < $this->header['FieldCount']; $i++){
137
+		for ($i = 0; $i < $this->header['FieldCount']; $i++) {
138 138
 			$this->cols[$i]['header'] = unpack($this::FORMAT_COLUMN, fread($this->fh, 0x18));
139 139
 		}
140 140
 
141 141
 		$offset = $this->header['FieldCount'] * 0x18 + $this->header['DescriptionOffset'] + 0x60;
142 142
 
143
-		if($this->header['FieldCount'] % 2){
143
+		if ($this->header['FieldCount'] % 2) {
144 144
 			$offset += 8;
145 145
 		}
146 146
 
147
-		foreach($this->cols as $i => $col){
147
+		foreach ($this->cols as $i => $col) {
148 148
 			fseek($this->fh, $offset + $col['header']['NameOffset']);
149 149
 
150 150
 			$this->cols[$i]['name'] = $this->decodeString(fread($this->fh, $col['header']['NameLength'] * 2));
@@ -160,7 +160,7 @@  discard block
 block discarded – undo
160 160
 	 */
161 161
 	public function read(string $dtbl):DTBLReader{
162 162
 
163
-		if(!is_file($dtbl) || !is_readable($dtbl)){
163
+		if (!is_file($dtbl) || !is_readable($dtbl)) {
164 164
 			throw new WSDBException('DTBL not readable');
165 165
 		}
166 166
 
@@ -170,19 +170,19 @@  discard block
 block discarded – undo
170 170
 
171 171
 		fseek($this->fh, $this->header['EntryOffset'] + 0x60);
172 172
 
173
-		for($i = 0; $i < $this->header['RecordCount']; $i++){
173
+		for ($i = 0; $i < $this->header['RecordCount']; $i++) {
174 174
 			$data = fread($this->fh, $this->header['RecordSize']);
175 175
 			$row  = [];
176 176
 			$j    = 0;
177 177
 			$skip = false;
178 178
 
179
-			foreach($this->cols as $c => $col){
179
+			foreach ($this->cols as $c => $col) {
180 180
 
181
-				if($skip === true && ($c > 0 && $this->cols[$c - 1]['header']['DataType'] === 130) && $col['header']['DataType'] !== 130){
181
+				if ($skip === true && ($c > 0 && $this->cols[$c - 1]['header']['DataType'] === 130) && $col['header']['DataType'] !== 130) {
182 182
 					$j += 4;
183 183
 				}
184 184
 
185
-				switch($col['header']['DataType']){
185
+				switch ($col['header']['DataType']) {
186 186
 					case 3:  // uint32
187 187
 					case 11: // booleans (stored as uint32 0/1)
188 188
 						$v = unpack('L', substr($data, $j, 4))[1]; $j += 4; break;
@@ -199,7 +199,7 @@  discard block
 block discarded – undo
199 199
 				$row[$col['name']] = $v;
200 200
 			}
201 201
 
202
-			if(count($row) !== $this->header['FieldCount']){
202
+			if (count($row) !== $this->header['FieldCount']) {
203 203
 				throw new WSDBException('invalid field count');
204 204
 			}
205 205
 
@@ -208,7 +208,7 @@  discard block
 block discarded – undo
208 208
 
209 209
 		fclose($this->fh);
210 210
 
211
-		if(count($this->data) !== $this->header['RecordCount']){
211
+		if (count($this->data) !== $this->header['RecordCount']) {
212 212
 			throw new WSDBException('invalid row count');
213 213
 		}
214 214
 
@@ -231,12 +231,12 @@  discard block
 block discarded – undo
231 231
 
232 232
 		$v = '';
233 233
 
234
-		do{
234
+		do {
235 235
 			$s = fread($this->fh, 2);
236 236
 
237 237
 			$v .= $s;
238 238
 		}
239
-		while($s !== "\x00\x00" && $s !== '');
239
+		while ($s !== "\x00\x00" && $s !== '');
240 240
 
241 241
 		fseek($this->fh, $p);
242 242
 
@@ -249,7 +249,7 @@  discard block
 block discarded – undo
249 249
 	 */
250 250
 	protected function checkData():void{
251 251
 
252
-		if(empty($this->data)){
252
+		if (empty($this->data)) {
253 253
 			throw new WSDBException('empty data, run DTBLReader::read() first');
254 254
 		}
255 255
 
@@ -264,7 +264,7 @@  discard block
 block discarded – undo
264 264
 	 */
265 265
 	protected function saveToFile(string $data, string $file):bool{
266 266
 
267
-		if(!is_writable(dirname($file))){
267
+		if (!is_writable(dirname($file))) {
268 268
 			throw new WSDBException('cannot write data to file: '.$file.', target directory is not writable');
269 269
 		}
270 270
 
@@ -282,7 +282,7 @@  discard block
 block discarded – undo
282 282
 
283 283
 		$json = json_encode($this->data, $jsonOptions);
284 284
 
285
-		if($file !== null){
285
+		if ($file !== null) {
286 286
 			$this->saveToFile($json, $file);
287 287
 		}
288 288
 
@@ -304,7 +304,7 @@  discard block
 block discarded – undo
304 304
 
305 305
 		fputcsv($mh, array_column($this->cols, 'name'), $delimiter, $enclosure, $escapeChar);
306 306
 
307
-		foreach($this->data as $row){
307
+		foreach ($this->data as $row) {
308 308
 			fputcsv($mh, array_values($row), $delimiter, $enclosure, $escapeChar);
309 309
 		}
310 310
 
@@ -314,7 +314,7 @@  discard block
 block discarded – undo
314 314
 
315 315
 		fclose($mh);
316 316
 
317
-		if($file !== null){
317
+		if ($file !== null) {
318 318
 			$this->saveToFile($csv, $file);
319 319
 		}
320 320
 
@@ -335,10 +335,10 @@  discard block
 block discarded – undo
335 335
 
336 336
 		$types = [3 => 'uint32', 4 => 'float', 11 => 'bool', 20 => 'uint64', 130 => 'string'];
337 337
 
338
-		foreach($this->data as $row){
338
+		foreach ($this->data as $row) {
339 339
 			$item = $sxe->addChild('item');
340 340
 
341
-			foreach(array_values($row) as $i => $value){
341
+			foreach (array_values($row) as $i => $value) {
342 342
 				$item
343 343
 					->addChild($this->cols[$i]['name'], $value)
344 344
 					->addAttribute('dataType', $types[$this->cols[$i]['header']['DataType']]);
@@ -348,7 +348,7 @@  discard block
 block discarded – undo
348 348
 
349 349
 		$xml = $sxe->asXML();
350 350
 
351
-		if($file !== null){
351
+		if ($file !== null) {
352 352
 			$this->saveToFile($xml, $file);
353 353
 		}
354 354
 
@@ -367,9 +367,9 @@  discard block
 block discarded – undo
367 367
 			->ifNotExists()
368 368
 		;
369 369
 
370
-		foreach($this->cols as $i => $col){
370
+		foreach ($this->cols as $i => $col) {
371 371
 
372
-			switch($col['header']['DataType']){
372
+			switch ($col['header']['DataType']) {
373 373
 				case 3:   $createTable->int($col['name'], 10, null, null, 'UNSIGNED'); break;
374 374
 				case 4:   $createTable->decimal($col['name'], '7,3', 0); break;
375 375
 				case 11:  $createTable->field($col['name'], 'BOOLEAN'); break;
@@ -381,7 +381,7 @@  discard block
 block discarded – undo
381 381
 
382 382
 		$createTable->query();
383 383
 
384
-		if(count($this->data) < 1){
384
+		if (count($this->data) < 1) {
385 385
 			$this->logger->notice('no records available for table '.$this->name);
386 386
 			return;
387 387
 		}
Please login to merge, or discard this patch.