Skip to content

Released: JsonRecast 0.4.0

Choose a tag to compare

@samsonasik samsonasik released this 23 Jul 00:19
· 273 commits to main since this release
0.4.0
d6deb25

ci build PHPStan

This release introduces NodeJsonFinder, a read-only query helper mirroring PHP-Parser's NodeFinder.

For example, use of the find:

$document = JsonRecast::parse(<<<'JSON'
{
    "repositories": [
        {"type": "composer", "url": "http://repo.internal.example/"},
        {"type": "vcs", "url": "https://github.com/acme/lib"}
    ]
}
JSON);

From the $document object, you can resolve the matching nodes, eg:

$finder = new NodeJsonFinder();
$insecureNodes = $finder->find(
    $document,
    static fn (NodeJson $node, NodeJsonPath $path): bool =>
        $node instanceof StringNode
        && str_starts_with($node->value, 'http://'),
);

// $insecureNodes:
// [
//     StringNode { value: 'http://repo.internal.example/' },
// ]

You can also find with extracted nodes data: found nodes do not record their own paths, so when the report should also say where each problem is, extract it in the filter as it arrives:

$insecure = [];
$finder = new NodeJsonFinder();

$finder->find(
    $document,
    static function (NodeJson $node, NodeJsonPath $path) use (&$insecure): bool {
        if ($node instanceof StringNode && str_starts_with($node->value, 'http://')) {
            $insecure[] = [
                'value'     => $node->value,
                'path_info' => array_map(
                    static fn ($segment) => $segment->value, $path->segments()
                ),
            ];

            return true;
        }

        return false;
    },
);

// $insecure:
// [
//     [
//         'value'     => 'http://repo.internal.example/',
//         'path_info' => ['repositories', 0, 'url'],
//     ],
// ]

What's Changed

Full Changelog: 0.3.44...0.4.0