Skip to content

Commit b395fcf

Browse files
committed
Implement a custom JSON serializer on top of the existing one to remove static properties from data files
Signed-off-by: William Desportes <williamdes@wdes.fr>
1 parent 27c7201 commit b395fcf

File tree

5 files changed

+95
-4
lines changed

5 files changed

+95
-4
lines changed

phpstan-baseline.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,11 @@ parameters:
850850
count: 1
851851
path: src/Tools/ContextGenerator.php
852852

853+
-
854+
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Tools\\\\CustomJsonSerializer\\:\\:extractObjectData\\(\\) has parameter \\$ref with generic class ReflectionClass but does not specify its types\\: T$#"
855+
count: 1
856+
path: src/Tools/CustomJsonSerializer.php
857+
853858
-
854859
message: "#^Argument of an invalid type array\\<int, string\\>\\|false supplied for foreach, only iterables are supported\\.$#"
855860
count: 1

psalm-baseline.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,18 @@
11481148
<code>++$i !== $count</code>
11491149
</RedundantCondition>
11501150
</file>
1151+
<file src="src/Tools/CustomJsonSerializer.php">
1152+
<MixedAssignment occurrences="2">
1153+
<code>$data[$property]</code>
1154+
<code>$data[$property]</code>
1155+
</MixedAssignment>
1156+
<MoreSpecificImplementedParamType occurrences="1">
1157+
<code>$properties</code>
1158+
</MoreSpecificImplementedParamType>
1159+
<PropertyNotSetInConstructor occurrences="1">
1160+
<code>CustomJsonSerializer</code>
1161+
</PropertyNotSetInConstructor>
1162+
</file>
11511163
<file src="src/Tools/TestGenerator.php">
11521164
<MixedOperand occurrences="1">
11531165
<code>$debug</code>

src/Tools/CustomJsonSerializer.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\SqlParser\Tools;
6+
7+
use ReflectionClass;
8+
use ReflectionException;
9+
use Zumba\JsonSerializer\JsonSerializer;
10+
11+
use function in_array;
12+
13+
/**
14+
* Used for .out files generation
15+
*/
16+
class CustomJsonSerializer extends JsonSerializer
17+
{
18+
public const SKIP_PROPERTIES = [
19+
'ALLOWED_KEYWORDS',
20+
'GROUP_OPTIONS',
21+
'END_OPTIONS',
22+
'KEYWORD_PARSERS',
23+
'STATEMENT_PARSERS',
24+
'KEYWORD_NAME_INDICATORS',
25+
'OPERATOR_NAME_INDICATORS',
26+
'DEFAULT_DELIMITER',
27+
'PARSER_METHODS',
28+
'OPTIONS',
29+
'CLAUSES',
30+
'DB_OPTIONS',
31+
'DELIMITERS',
32+
'JOINS',
33+
'FIELDS_OPTIONS',
34+
'LINES_OPTIONS',
35+
'TRIGGER_OPTIONS',
36+
'FUNC_OPTIONS',
37+
'TABLE_OPTIONS',
38+
'FIELD_OPTIONS',
39+
'DATA_TYPE_OPTIONS',
40+
'REFERENCES_OPTIONS',
41+
'KEY_OPTIONS',
42+
'VIEW_OPTIONS',
43+
'EVENT_OPTIONS',
44+
'USER_OPTIONS',
45+
];
46+
47+
/**
48+
* Extract the object data
49+
*
50+
* @param object $value
51+
* @param ReflectionClass $ref
52+
* @param string[] $properties
53+
*
54+
* @return array<string,mixed>
55+
*/
56+
protected function extractObjectData($value, $ref, $properties)
57+
{
58+
$data = [];
59+
foreach ($properties as $property) {
60+
if (in_array($property, self::SKIP_PROPERTIES, true)) {
61+
continue;
62+
}
63+
64+
try {
65+
$propRef = $ref->getProperty($property);
66+
$propRef->setAccessible(true);
67+
$data[$property] = $propRef->getValue($value);
68+
} catch (ReflectionException $e) {
69+
$data[$property] = $value->$property;
70+
}
71+
}
72+
73+
return $data;
74+
}
75+
}

src/Tools/TestGenerator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use PhpMyAdmin\SqlParser\Parser;
1313
use PhpMyAdmin\SqlParser\Token;
1414
use PhpMyAdmin\SqlParser\UtfString;
15-
use Zumba\JsonSerializer\JsonSerializer;
1615

1716
use function file_exists;
1817
use function file_get_contents;
@@ -170,7 +169,7 @@ public static function build($type, $input, $output, $debug = null, $ansi = fals
170169

171170
// unset mode, reset to default every time, to be sure
172171
Context::setMode();
173-
$serializer = new JsonSerializer();
172+
$serializer = new CustomJsonSerializer();
174173
// Writing test's data.
175174
$encoded = $serializer->serialize($test);
176175

tests/TestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
use PhpMyAdmin\SqlParser\Parser;
1212
use PhpMyAdmin\SqlParser\Token;
1313
use PhpMyAdmin\SqlParser\TokensList;
14+
use PhpMyAdmin\SqlParser\Tools\CustomJsonSerializer;
1415
use PHPUnit\Framework\TestCase as BaseTestCase;
15-
use Zumba\JsonSerializer\JsonSerializer;
1616

1717
use function file_get_contents;
1818
use function str_contains;
@@ -96,7 +96,7 @@ public function getData(string $name): array
9696
$serializedData = file_get_contents('tests/data/' . $name . '.out');
9797
$this->assertIsString($serializedData);
9898

99-
$serializer = new JsonSerializer();
99+
$serializer = new CustomJsonSerializer();
100100
$data = $serializer->unserialize($serializedData);
101101

102102
$this->assertIsArray($data);

0 commit comments

Comments
 (0)