Passed
Push — master ( bdfaf8...931c46 )
by smiley
01:33
created

PACKReaderAbstract   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toCSV() 0 5 1
A toDB() 0 5 1
A read() 0 21 3
A toXML() 0 5 1
1
<?php
2
/**
3
 * Class PACKReaderAbstract
4
 *
5
 * @filesource   PACKReaderAbstract.php
6
 * @created      27.04.2019
7
 * @package      codemasher\WildstarDB
8
 * @author       smiley <[email protected]>
9
 * @copyright    2019 smiley
10
 * @license      MIT
11
 */
12
13
namespace codemasher\WildstarDB;
14
15
use chillerlan\Database\Database;
16
17
/**
18
 * @property array $blocktable
19
 */
20
abstract class PACKReaderAbstract extends ReaderAbstract{
21
22
	/**
23
	 * @var string
24
	 */
25
	protected $FORMAT_HEADER = 'a4Signature/LVersion/x512/QFilesize/x8/QBlockTableOffset/LBlockCount/x4/LRootInfoIndex/x8';
26
27
	/**
28
	 * 4+4+512+8+8+8+4+4+4+8 = 564 bytes
29
	 *
30
	 * @var int
31
	 */
32
	protected $headerSize = 564;
33
34
	/**
35
	 * @var array
36
	 */
37
	protected $blocktable = [];
38
39
	abstract protected function readData():void;
40
41
	/**
42
	 * @param string $filename
43
	 *
44
	 * @return \codemasher\WildstarDB\ReaderInterface
45
	 * @throws \codemasher\WildstarDB\WSDBException
46
	 */
47
	public function read(string $filename):ReaderInterface{
48
		$this->loadFile($filename);
49
		$this->blocktable = [];
50
51
		if($this->header['Signature'] !== "\x4b\x43\x41\x50"){ // KCAP
52
			throw new WSDBException('invalid PACK');
53
		}
54
55
		// read the block info table
56
		\fseek($this->fh, $this->header['BlockTableOffset']);
57
58
		for($i = 0; $i < $this->header['BlockCount']; $i++){
59
			$this->blocktable[$i] = \unpack('QOffset/QSize', \fread($this->fh, 16));
60
		}
61
62
		// seek forward to the root index block for convenience
63
		\fseek($this->fh, $this->blocktable[$this->header['RootInfoIndex']]['Offset']);
64
65
		$this->readData();
66
67
		return $this;
68
	}
69
70
	/**
71
	 * @param string|null $file
72
	 * @param string      $delimiter
73
	 * @param string      $enclosure
74
	 * @param string      $escapeChar
75
	 *
76
	 * @return string
77
	 */
78
	public function toCSV(string $file = null, string $delimiter = ',', string $enclosure = '"', string $escapeChar = '\\'):string{
79
		// @todo
80
		throw new WSDBException('not implemented');
81
82
		return '';
0 ignored issues
show
Unused Code introduced by
return '' is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
83
	}
84
85
	/**
86
	 * ugh!
87
	 *
88
	 * @param string|null $file
89
	 *
90
	 * @return string
91
	 */
92
	public function toXML(string $file = null):string{
93
		// @todo
94
		throw new WSDBException('not implemented');
95
96
		return '';
0 ignored issues
show
Unused Code introduced by
return '' is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
97
	}
98
99
	/**
100
	 * @param \chillerlan\Database\Database $db
101
	 *
102
	 * @return \codemasher\WildstarDB\ReaderInterface
103
	 */
104
	public function toDB(Database $db):ReaderInterface{
105
		// @todo
106
		throw new WSDBException('not implemented');
107
108
		return $this;
0 ignored issues
show
Unused Code introduced by
return $this is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
109
	}
110
111
}
112