Skip to content

Commit a761a3f

Browse files
committed
Improved codebase quality through static analysis (#1723)
1 parent 9b35f69 commit a761a3f

File tree

10 files changed

+104
-16
lines changed

10 files changed

+104
-16
lines changed

src/Flow/ETL/Adapter/JSON/JSONMachine/JsonExtractor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function extract(FlowContext $context) : \Generator
3636
foreach ($context->streams()->list($this->path, $this->filter()) as $stream) {
3737

3838
/**
39-
* @var array|object $rowData
39+
* @var array<string, mixed>|object $rowData
4040
*/
4141
foreach ((new Items($stream->iterate(8 * 1024), $this->readerOptions()))->getIterator() as $rowData) {
4242
$row = (array) $rowData;

src/Flow/ETL/Adapter/JSON/JSONMachine/JsonLinesExtractor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function extract(FlowContext $context) : \Generator
4949

5050
foreach ($stream->readLines() as $jsonLine) {
5151
/**
52-
* @var array|object $rowData
52+
* @var array<string, mixed>|object $rowData
5353
*/
5454
foreach ($lineIterator($jsonLine) as $rowData) {
5555

src/Flow/ETL/Adapter/JSON/RowsNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function __construct(private EntryNormalizer $normalizer)
1515
}
1616

1717
/**
18-
* @return \Generator<array<string, null|array|bool|float|int|string>>
18+
* @return \Generator<array<string, null|array<string, mixed>|bool|float|int|string>>
1919
*/
2020
public function normalize(Rows $rows) : \Generator
2121
{

src/Flow/ETL/Adapter/JSON/RowsNormalizer/EntryNormalizer.php

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public function __construct(
1717
}
1818

1919
/**
20-
* @param Entry<mixed, mixed> $entry
20+
* @param Entry<mixed> $entry
21+
*
22+
* @return null|array<string, mixed>|bool|float|int|string
2123
*/
2224
public function normalize(Entry $entry) : string|float|int|bool|array|null
2325
{
@@ -27,13 +29,56 @@ public function normalize(Entry $entry) : string|float|int|bool|array|null
2729
DateEntry::class => $entry->value()?->format($this->dateFormat),
2830
TimeEntry::class => $entry->value() ? date_interval_to_microseconds($entry->value()) : null,
2931
EnumEntry::class => $entry->value()?->name,
30-
JsonEntry::class => $entry->value(),
32+
JsonEntry::class => $this->normalizeJsonValue($entry->value()),
3133
ListEntry::class,
3234
MapEntry::class,
3335
StructureEntry::class,
3436
XMLElementEntry::class => $entry->toString(),
3537
XMLEntry::class => $entry->toString(),
36-
default => $entry->value(),
38+
default => $this->normalizeValue($entry->value()),
3739
};
3840
}
41+
42+
/**
43+
* @return null|array<string, mixed>|bool|float|int|string
44+
*/
45+
private function normalizeJsonValue(mixed $value) : string|float|int|bool|array|null
46+
{
47+
if (\is_array($value)) {
48+
/** @var array<string, mixed> $normalizedArray */
49+
$normalizedArray = [];
50+
51+
foreach ($value as $key => $val) {
52+
$normalizedArray[\is_string($key) ? $key : (string) $key] = $val;
53+
}
54+
55+
return $normalizedArray;
56+
}
57+
58+
return $this->normalizeValue($value);
59+
}
60+
61+
/**
62+
* @return null|array<string, mixed>|bool|float|int|string
63+
*/
64+
private function normalizeValue(mixed $value) : string|float|int|bool|array|null
65+
{
66+
if (\is_string($value) || \is_float($value) || \is_int($value) || \is_bool($value) || $value === null) {
67+
return $value;
68+
}
69+
70+
if (\is_array($value)) {
71+
/** @var array<string, mixed> $normalizedArray */
72+
$normalizedArray = [];
73+
74+
foreach ($value as $key => $val) {
75+
$normalizedArray[\is_string($key) ? $key : (string) $key] = $val;
76+
}
77+
78+
return $normalizedArray;
79+
}
80+
81+
// Fallback for unexpected types - convert to string
82+
return '';
83+
}
3984
}

src/Flow/ETL/Adapter/JSON/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* @param Path|string $path - string is internally turned into stream
1414
* @param ?string $pointer - if you want to iterate only results of a subtree, use a pointer, read more at https://github.com/halaxa/json-machine#parsing-a-subtree - @deprecate use withPointer method instead
15-
* @param ?Schema $schema - enforce schema on the extracted data - @deprecate use withSchema method instead
15+
* @param null|Schema $schema - enforce schema on the extracted data - @deprecate use withSchema method instead
1616
*/
1717
#[DocumentationDSL(module: Module::JSON, type: Type::EXTRACTOR)]
1818
#[DocumentationExample(topic: 'data_reading', example: 'json')]

tests/Flow/ETL/Adapter/JSON/Tests/Integration/JSONMachine/JsonExtractorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function test_extracting_json_from_local_file_stream_using_pointer() : vo
4444
->fetch();
4545

4646
foreach ($rows as $row) {
47+
$value = $row->get('/timezones')->value();
4748
self::assertSame(
4849
[
4950
'timezones',
@@ -53,7 +54,7 @@ public function test_extracting_json_from_local_file_stream_using_pointer() : vo
5354
'capital',
5455

5556
],
56-
\array_keys($row->get('/timezones')->value())
57+
\is_array($value) ? \array_keys($value) : []
5758
);
5859
}
5960

tests/Flow/ETL/Adapter/JSON/Tests/Integration/JSONMachine/JsonLinesExtractorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function test_extracting_jsonl_from_local_file_stream_using_pointer() : v
4444
->fetch();
4545

4646
foreach ($rows as $row) {
47+
$value = $row->get('/timezones')->value();
4748
self::assertSame(
4849
[
4950
'timezones',
@@ -53,7 +54,7 @@ public function test_extracting_jsonl_from_local_file_stream_using_pointer() : v
5354
'capital',
5455

5556
],
56-
\array_keys($row->get('/timezones')->value())
57+
\is_array($value) ? \array_keys($value) : []
5758
);
5859
}
5960

tests/Flow/ETL/Adapter/JSON/Tests/Integration/JsonLinesTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,20 @@ public function test_ignores_pretty() : void
2727
->write(to_json_lines($path = __DIR__ . '/var/test_jsonl_ignore_pretty.jsonl')->withFlags(JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES))
2828
->run();
2929

30+
$content = \file_get_contents($path);
31+
32+
if ($content === false) {
33+
self::fail('Failed to read file content');
34+
}
35+
3036
self::assertStringContainsString(
3137
<<<'JSON'
3238
{"name":"John","age":30}
3339
{"name":"Jane","age":25}
3440
{"name":"Jake","age":30}
3541
{"name":"Joe","age":30}
3642
JSON,
37-
\file_get_contents($path)
43+
$content
3844
);
3945
}
4046

tests/Flow/ETL/Adapter/JSON/Tests/Integration/JsonTest.php

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,17 @@ public function test_domdocument_json_file() : void
2828
->write(to_json($path = __DIR__ . '/var/test_domdocument.json'))
2929
->run();
3030

31+
$content = \file_get_contents($path);
32+
33+
if ($content === false) {
34+
self::fail('Failed to read file content');
35+
}
36+
3137
self::assertStringContainsString(
3238
<<<'JSON'
3339
[{"id":1,"descriptionHtml":"<b>red<\/b>","size":"small"}]
3440
JSON,
35-
\file_get_contents($path)
41+
$content
3642
);
3743
}
3844

@@ -67,12 +73,18 @@ public function test_json_loader_loading_empty_string() : void
6773

6874
$loader->closure($context);
6975

76+
$content = \file_get_contents($path);
77+
78+
if ($content === false) {
79+
self::fail('Failed to read file content');
80+
}
81+
7082
self::assertJsonStringEqualsJsonString(
7183
<<<'JSON'
7284
[
7385
]
7486
JSON,
75-
\file_get_contents($path)
87+
$content
7688
);
7789

7890
if (\file_exists($path)) {
@@ -100,6 +112,11 @@ public function test_json_loader_overwrite_mode() : void
100112
->run();
101113

102114
$content = \file_get_contents($path);
115+
116+
if ($content === false) {
117+
self::fail('Failed to read file content');
118+
}
119+
103120
self::assertStringEndsWith(']', $content);
104121

105122
self::assertEquals(
@@ -126,11 +143,17 @@ public function test_jsonentry_json_file() : void
126143
->write(to_json($path = __DIR__ . '/var/test_jsonentry.json'))
127144
->run();
128145

146+
$content = \file_get_contents($path);
147+
148+
if ($content === false) {
149+
self::fail('Failed to read file content');
150+
}
151+
129152
self::assertStringContainsString(
130153
<<<'JSON'
131154
[{"id":1,"nested":{"short":"short_description","long":"long_description"}}]
132155
JSON,
133-
\file_get_contents($path)
156+
$content
134157
);
135158
}
136159

@@ -179,6 +202,12 @@ public function test_putting_each_row_in_a_new_line() : void
179202
->write(to_json($path = __DIR__ . '/var/test_putting_each_row_in_a_new_line.json', put_rows_in_new_lines: true))
180203
->run();
181204

205+
$content = \file_get_contents($path);
206+
207+
if ($content === false) {
208+
self::fail('Failed to read file content');
209+
}
210+
182211
self::assertStringContainsString(
183212
<<<'JSON'
184213
[
@@ -188,7 +217,7 @@ public function test_putting_each_row_in_a_new_line() : void
188217
{"name":"Joe","age":30}
189218
]
190219
JSON,
191-
\file_get_contents($path)
220+
$content
192221
);
193222
}
194223

@@ -207,6 +236,12 @@ public function test_putting_each_row_in_a_new_line_with_json_pretty_print_flag(
207236
->write(to_json($path = __DIR__ . '/var/test_putting_each_row_in_a_new_line.json', flags: JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT, put_rows_in_new_lines: true))
208237
->run();
209238

239+
$content = \file_get_contents($path);
240+
241+
if ($content === false) {
242+
self::fail('Failed to read file content');
243+
}
244+
210245
self::assertStringContainsString(
211246
<<<'JSON'
212247
[
@@ -220,7 +255,7 @@ public function test_putting_each_row_in_a_new_line_with_json_pretty_print_flag(
220255
}
221256
]
222257
JSON,
223-
\file_get_contents($path)
258+
$content
224259
);
225260
}
226261
}

tests/Flow/ETL/Adapter/JSON/Tests/Unit/EntryNormalizerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static function entries_provider() : \Generator
4242
}
4343

4444
/**
45-
* @param Entry<mixed, mixed> $entry
45+
* @param Entry<mixed> $entry
4646
*/
4747
#[DataProvider('entries_provider')]
4848
public function test_normalizing_entries(Entry $entry, mixed $expected) : void

0 commit comments

Comments
 (0)