Skip to content

Commit 7119373

Browse files
authored
refactor: Handle more cases and update tests (#1)
* wip * refactor: Various improvements * improve bench * add logging support * update readme
1 parent e2c2acc commit 7119373

11 files changed

Lines changed: 2009 additions & 762 deletions

File tree

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
/ecs.php export-ignore
1212
/rector.php export-ignore
1313
/tests export-ignore
14+
/benchmarks export-ignore

.github/workflows/run-tests.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,5 @@ jobs:
3838
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
3939
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
4040
41-
- name: Execute tests with mutation
42-
if: ${{ matrix.os == 'ubuntu-latest' && matrix.php == '8.5' }}
43-
run: vendor/bin/pest --colors=always --mutate --parallel --min=50
44-
4541
- name: Execute tests
46-
if: ${{ matrix.os != 'ubuntu-latest' || matrix.php != '8.5' }}
47-
run: vendor/bin/pest --colors=always
42+
run: vendor/bin/pest --colors=always --parallel

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,28 @@ $data = json_repair_decode(
107107
);
108108
```
109109

110+
## Logging
111+
112+
The library supports PSR-3 logging for debugging repair operations. Pass any PSR-3 compatible logger to see what repairs are being made:
113+
114+
```php
115+
use Psr\Log\LoggerInterface;
116+
117+
// Using the helper function
118+
$repaired = json_repair($broken, logger: $logger);
119+
120+
// Using the class (implements LoggerAwareInterface)
121+
$repairer = new JsonRepairer($broken);
122+
$repairer->setLogger($logger);
123+
$repaired = $repairer->repair();
124+
```
125+
126+
Log messages include the position in the JSON string and a context snippet showing where the repair occurred. This is useful for:
127+
128+
- Debugging why certain repairs are being made
129+
- Understanding how malformed JSON is being interpreted
130+
- Tracking repair operations in production environments
131+
110132
## Credits
111133

112134
- [Sean Tymon](https://github.com/tymondesigns)

benchmarks/JsonRepairerBench.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
/**
1313
* @Revs(100)
14-
* @Iterations(20)
14+
* @Iterations(10)
1515
* @Warmup(2)
1616
*/
1717
class JsonRepairerBench
@@ -41,7 +41,15 @@ public function benchRepairValidJson(array $params): void
4141
}
4242

4343
/**
44+
* Benchmarks large JSON repair.
45+
*
46+
* Note: This benchmark uses fewer revs/iterations since it processes
47+
* 1000 items and takes ~44ms per run. Use --filter=benchRepairLargeJson
48+
* to run it separately.
49+
*
4450
* @ParamProviders({"provideLargeJson"})
51+
* @Revs(10)
52+
* @Iterations(5)
4553
*/
4654
public function benchRepairLargeJson(array $params): void
4755
{
@@ -81,6 +89,16 @@ public function benchRepairStreamingJson(array $params): void
8189
json_repair($params['json']);
8290
}
8391

92+
/**
93+
* Baseline: compare repair overhead against native json_decode on valid JSON.
94+
*
95+
* @ParamProviders({"provideValidJson"})
96+
*/
97+
public function benchNativeJsonDecodeBaseline(array $params): void
98+
{
99+
json_decode($params['json']);
100+
}
101+
84102
/**
85103
* @return array<string, array<string, string>>
86104
*/
@@ -141,7 +159,7 @@ public function provideLargeJson(): array
141159
$brokenJson = rtrim($brokenJson, '}') . ',}';
142160

143161
return [
144-
'large_array' => ['json' => $brokenJson],
162+
'large_array_broken' => ['json' => $brokenJson],
145163
];
146164
}
147165

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
],
1919
"require": {
2020
"php": "^8.3",
21-
"ext-json": "*"
21+
"ext-json": "*",
22+
"psr/log": "^3.0"
2223
},
2324
"require-dev": {
25+
"colinodell/psr-testlogger": "^1.3",
2426
"pestphp/pest": "^4.1.4",
2527
"pestphp/pest-plugin-type-coverage": "^4.0.3",
2628
"phpbench/phpbench": "^1.4",

phpbench.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
"$schema": "https://raw.githubusercontent.com/phpbench/phpbench/master/lib/phpbench.schema.json",
33
"runner.bootstrap": "vendor/autoload.php",
44
"runner.path": "benchmarks",
5-
"runner.iterations": 20,
5+
"runner.iterations": 10,
66
"runner.revs": 100,
77
"runner.warmup": 2,
8-
"runner.time_unit": "microseconds"
8+
"runner.time_unit": "microseconds",
9+
"report.generators": {
10+
"compare": {
11+
"generator": "table",
12+
"cols": ["benchmark", "subject", "set", "revs", "its", "mem_peak", "mode", "rstdev"]
13+
}
14+
}
915
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cortex\JsonRepair\Exceptions;
6+
7+
use RuntimeException;
8+
9+
class JsonRepairException extends RuntimeException
10+
{
11+
/**
12+
* Create a new exception for invalid JSON after repair.
13+
*
14+
* This exception is thrown when the repair process completes but the
15+
* resulting output is still not valid JSON.
16+
*
17+
* @param string $json The repaired JSON that is still invalid
18+
*
19+
* @return self A new JsonRepairException instance
20+
*/
21+
public static function invalidJsonAfterRepair(string $json): self
22+
{
23+
return new self(
24+
sprintf(
25+
'JSON repair completed but the result is still invalid JSON: %s',
26+
$json,
27+
),
28+
);
29+
}
30+
}

0 commit comments

Comments
 (0)