• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wol-soft / php-json-schema-model-generator / 26374741919

24 May 2026 10:39PM UTC coverage: 98.638% (+0.08%) from 98.554%
26374741919

Pull #135

github

web-flow
Merge 95246e9e8 into cf5b7af2c
Pull Request #135: Boolean schemas

385 of 388 new or added lines in 16 files covered. (99.23%)

2 existing lines in 2 files now uncovered.

5141 of 5212 relevant lines covered (98.64%)

633.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.65
/src/SchemaProvider/RefResolverTrait.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace PHPModelGenerator\SchemaProvider;
6

7
use PHPModelGenerator\Exception\SchemaException;
8
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
9

10
trait RefResolverTrait
11
{
12
    public function getRef(string $currentFile, ?string $id, string $ref): JsonSchema
209✔
13
    {
14
        $jsonSchemaFilePath = $this->getFullRefURL($id ?? $currentFile, $ref)
209✔
15
            ?: $this->getLocalRefPath($currentFile, $ref);
203✔
16

17
        if ($jsonSchemaFilePath === null || !($jsonSchema = file_get_contents($jsonSchemaFilePath))) {
209✔
18
            throw new SchemaException("Reference to non existing JSON-Schema file $ref");
7✔
19
        }
20

21
        $decodedJsonSchema = json_decode($jsonSchema, true);
202✔
22

23
        if (json_last_error() !== JSON_ERROR_NONE) {
202✔
UNCOV
24
            throw new SchemaException("Invalid JSON-Schema file $jsonSchemaFilePath");
×
25
        }
26

27
        if (!is_array($decodedJsonSchema)) {
202✔
NEW
28
            throw new SchemaException("Referenced JSON-Schema file $jsonSchemaFilePath must contain a JSON object");
×
29
        }
30

31
        return new JsonSchema($jsonSchemaFilePath, $decodedJsonSchema);
202✔
32
    }
33

34
    /**
35
     * Try to build a full URL to fetch the schema from utilizing the $id field of the schema
36
     */
37
    private function getFullRefURL(string $id, string $ref): ?string
209✔
38
    {
39
        if (filter_var($ref, FILTER_VALIDATE_URL)) {
209✔
40
            return $ref;
2✔
41
        }
42

43
        if (!filter_var($id, FILTER_VALIDATE_URL) || ($idURL = parse_url($id)) === false) {
208✔
44
            return null;
203✔
45
        }
46

47
        $baseURL = $idURL['scheme'] . '://' . $idURL['host'] . (isset($idURL['port']) ? ':' . $idURL['port'] : '');
5✔
48

49
        // root relative $ref
50
        if (str_starts_with($ref, '/')) {
5✔
51
            return $baseURL . $ref;
2✔
52
        }
53

54
        // relative $ref against the path of $id
55
        $segments = explode('/', rtrim(dirname($idURL['path'] ?? '/'), '/') . '/' . $ref);
4✔
56
        $output = [];
4✔
57

58
        foreach ($segments as $seg) {
4✔
59
            if ($seg === '' || $seg === '.') {
4✔
60
                continue;
4✔
61
            }
62
            if ($seg === '..') {
4✔
63
                array_pop($output);
2✔
64
                continue;
2✔
65
            }
66
            $output[] = $seg;
4✔
67
        }
68

69
        return $baseURL . '/' . implode('/', $output);
4✔
70
    }
71

72
    private function getLocalRefPath(string $currentFile, string $ref): ?string
203✔
73
    {
74
        $currentDir = dirname($currentFile);
203✔
75
        // windows compatibility
76
        $jsonSchemaFile = str_replace('\\', '/', $ref);
203✔
77

78
        // relative paths to the current location
79
        if (!str_starts_with($jsonSchemaFile, '/')) {
203✔
80
            $candidate = $currentDir . '/' . $jsonSchemaFile;
202✔
81

82
            return file_exists($candidate) ? realpath($candidate) : null;
202✔
83
        }
84

85
        // absolute paths: traverse up to find the context root directory
86
        $relative = ltrim($jsonSchemaFile, '/');
2✔
87

88
        $dir = $currentDir;
2✔
89
        while (true) {
2✔
90
            $candidate = $dir . '/' . $relative;
2✔
91
            if (file_exists($candidate)) {
2✔
92
                return realpath($candidate);
1✔
93
            }
94

95
            $parent = dirname($dir);
2✔
96
            if ($parent === $dir) {
2✔
97
                break;
1✔
98
            }
99
            $dir = $parent;
2✔
100
        }
101

102
        return null;
1✔
103
    }
104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc