Skip to content

Commit b0bdc95

Browse files
committed
Refactor Thrift debugger
1 parent cf79841 commit b0bdc95

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

bin/thrift_debug

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ if ($data === false) {
2222
exit(1);
2323
}
2424

25+
$debugger = new \Fbns\Thrift\Utils\Debug();
2526
try {
26-
$debugger = new \Fbns\Thrift\Utils\Debug($data);
27-
$debugger();
27+
$debugger($data);
2828
} catch (\Exception $e) {
2929
fwrite(STDERR, $e->getMessage().PHP_EOL);
3030

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
"email": "[email protected]"
1616
}
1717
],
18+
"bin": [
19+
"bin/thrift_debug"
20+
],
1821
"autoload": {
1922
"psr-4": {
2023
"Fbns\\": "src/"

src/Thrift/Utils/Debug.php

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,9 @@ class Debug
1414
{
1515
private const PADDING = ' ';
1616

17-
/** @var Reader */
18-
private $reader;
19-
20-
public function __construct($buffer)
21-
{
22-
$this->reader = new Reader($buffer);
23-
}
24-
25-
public function __invoke()
17+
public function __invoke(string $buffer)
2618
{
27-
$reader = $this->reader;
19+
$reader = new Reader($buffer);
2820
$this->debugStruct($reader()->value());
2921
}
3022

@@ -37,26 +29,36 @@ private function debugStruct(iterable $struct, string $padding = ''): void
3729
}
3830
}
3931

32+
private function debugSeries(Series $value, string $padding): void
33+
{
34+
printf("%02x [\n", $value->itemType());
35+
foreach ($value->value() as $item) {
36+
$this->debugValue($item, $padding.self::PADDING);
37+
echo ",\n";
38+
}
39+
echo "{$padding}]";
40+
}
41+
42+
private function debugMap(Map $value, string $padding): void
43+
{
44+
printf("%02x => %02x [\n", $value->keyType(), $value->valueType());
45+
foreach ($value->value() as $key => $item) {
46+
$this->debugValue($key, $padding.self::PADDING);
47+
echo ' => ';
48+
$this->debugValue($item, '');
49+
echo ",\n";
50+
}
51+
echo "{$padding}]";
52+
}
53+
4054
private function debugValue($value, string $padding): void
4155
{
4256
switch (true) {
4357
case $value instanceof Series:
44-
printf("%02x [\n", $value->itemType());
45-
foreach ($value->value() as $item) {
46-
$this->debugValue($item, $padding.self::PADDING);
47-
echo ",\n";
48-
}
49-
echo "{$padding}]";
58+
$this->debugSeries($value, $padding);
5059
break;
5160
case $value instanceof Map:
52-
printf("%02x => %02x [\n", $value->keyType(), $value->valueType());
53-
foreach ($value->value() as $key => $item) {
54-
$this->debugValue($key, $padding.self::PADDING);
55-
echo ' => ';
56-
$this->debugValue($item, '');
57-
echo ",\n";
58-
}
59-
echo "{$padding}]";
61+
$this->debugMap($value, $padding);
6062
break;
6163
case $value instanceof Struct:
6264
echo "{\n";

0 commit comments

Comments
 (0)