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

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

23 Mar 2026 01:55PM UTC coverage: 98.584% (-0.001%) from 98.585%
23440965067

Pull #118

github

Enno Woortmann
Merge fix/issue-110 into chore/phpunit-13-upgrade

Brings in patternProperties type intersection, PSR-12 code style
enforcement, PropertyMerger API refactor, and all related tests and
documentation. Resolved conflicts by keeping readonly constructors and
PHP 8.4+ style from this branch, applying phpcbf to align opening
braces with the PSR-12 rule. Added squizlabs/php_codesniffer to
require-dev alongside phpunit ^13.0.
Pull Request #118: Upgrade to PHP 8.4 minimum and PHPUnit 13

508 of 517 new or added lines in 54 files covered. (98.26%)

23 existing lines in 9 files now uncovered.

3828 of 3883 relevant lines covered (98.58%)

548.12 hits per line

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

92.19
/src/Model/SchemaDefinition/SchemaDefinitionDictionary.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace PHPModelGenerator\Model\SchemaDefinition;
6

7
use ArrayObject;
8
use PHPModelGenerator\Exception\SchemaException;
9
use PHPModelGenerator\Model\Schema;
10
use PHPModelGenerator\SchemaProcessor\SchemaProcessor;
11

12
/**
13
 * Class SchemaDefinitionDictionary
14
 *
15
 * @package PHPModelGenerator\Model\SchemaDefinition
16
 */
17
class SchemaDefinitionDictionary extends ArrayObject
18
{
19
    /** @var Schema[] */
20
    private array $parsedExternalFileSchemas = [];
21

22
    /**
23
     * SchemaDefinitionDictionary constructor.
24
     */
25
    public function __construct(private readonly JsonSchema $schema)
2,069✔
26
    {
27
        parent::__construct();
2,069✔
28
    }
29

30
    /**
31
     * Set up the definition directory for the schema
32
     */
33
    public function setUpDefinitionDictionary(SchemaProcessor $schemaProcessor, Schema $schema): void
2,061✔
34
    {
35
        // attach the root node to the definition dictionary
36
        $this->addDefinition('#', new SchemaDefinition($schema->getJsonSchema(), $schemaProcessor, $schema));
2,061✔
37

38
        foreach ($schema->getJsonSchema()->getJson() as $key => $propertyEntry) {
2,061✔
39
            if (!is_array($propertyEntry)) {
2,061✔
40
                continue;
2,061✔
41
            }
42

43
            // add the root nodes of the schema to resolve path references
44
            $this->addDefinition(
2,046✔
45
                $key,
2,046✔
46
                new SchemaDefinition($schema->getJsonSchema()->withJson($propertyEntry), $schemaProcessor, $schema),
2,046✔
47
            );
2,046✔
48
        }
49

50
        $this->fetchDefinitionsById($schema->getJsonSchema(), $schemaProcessor, $schema);
2,061✔
51
    }
52

53
    /**
54
     * Fetch all schema definitions with an ID for direct references
55
     */
56
    protected function fetchDefinitionsById(
2,061✔
57
        JsonSchema $jsonSchema,
58
        SchemaProcessor $schemaProcessor,
59
        Schema $schema,
60
    ): void {
61
        $json = $jsonSchema->getJson();
2,061✔
62

63
        if (isset($json['$id'])) {
2,061✔
64
            $this->addDefinition(
315✔
65
                str_starts_with((string) $json['$id'], '#') ? $json['$id'] : "#{$json['$id']}",
315✔
66
                new SchemaDefinition($jsonSchema, $schemaProcessor, $schema),
315✔
67
            );
315✔
68
        }
69

70
        foreach ($json as $item) {
2,061✔
71
            if (!is_array($item)) {
2,061✔
72
                continue;
2,061✔
73
            }
74

75
            $this->fetchDefinitionsById($jsonSchema->withJson($item), $schemaProcessor, $schema);
2,046✔
76
        }
77
    }
78

79
    /**
80
     * Add a partial schema definition to the schema
81
     *
82
     * @return $this
83
     */
84
    public function addDefinition(string $key, SchemaDefinition $definition): self
2,061✔
85
    {
86
        if (isset($this[$key])) {
2,061✔
87
            return $this;
869✔
88
        }
89

90
        $this[$key] = $definition;
2,061✔
91

92
        return $this;
2,061✔
93
    }
94

95
    /**
96
     * @throws SchemaException
97
     */
98
    public function getDefinition(string $key, SchemaProcessor $schemaProcessor, array &$path = []): ?SchemaDefinition
529✔
99
    {
100
        if (str_starts_with($key, '#') && strpos($key, '/')) {
529✔
101
            $path = explode('/', $key);
404✔
102
            array_shift($path);
404✔
103
            $key  = array_shift($path);
404✔
104
        }
105

106
        if (!isset($this[$key])) {
529✔
107
            if (strstr((string) $key, '#', true)) {
192✔
108
                [$jsonSchemaFile, $externalKey] = explode('#', (string) $key);
166✔
109
            } else {
110
                $jsonSchemaFile = $key;
26✔
111
                $externalKey = '';
26✔
112
            }
113

114
            if (array_key_exists($jsonSchemaFile, $this->parsedExternalFileSchemas)) {
192✔
UNCOV
115
                return $this->parsedExternalFileSchemas[$jsonSchemaFile]->getSchemaDictionary()->getDefinition(
×
116
                    "#$externalKey",
×
117
                    $schemaProcessor,
×
118
                    $path,
×
119
                );
×
120
            }
121

122
            return $jsonSchemaFile
192✔
123
                ? $this->parseExternalFile($jsonSchemaFile, "#$externalKey", $schemaProcessor, $path)
192✔
124
                : null;
185✔
125
        }
126

127
        return $this[$key] ?? null;
522✔
128
    }
129

130
    /**
131
     * @throws SchemaException
132
     */
133
    private function parseExternalFile(
192✔
134
        string $jsonSchemaFile,
135
        string $externalKey,
136
        SchemaProcessor $schemaProcessor,
137
        array &$path,
138
    ): ?SchemaDefinition {
139
        $jsonSchema = $schemaProcessor->getSchemaProvider()->getRef(
192✔
140
            $this->schema->getFile(),
192✔
141
            $this->schema->getJson()['$id'] ?? null,
192✔
142
            $jsonSchemaFile,
192✔
143
        );
192✔
144

145
        // set up a dummy schema to fetch the definitions from the external file
146
        $schema = new Schema(
185✔
147
            '',
185✔
148
            $schemaProcessor->getCurrentClassPath(),
185✔
149
            'ExternalSchema',
185✔
150
            $jsonSchema,
185✔
151
            new self($jsonSchema),
185✔
152
        );
185✔
153

154
        $schema->getSchemaDictionary()->setUpDefinitionDictionary($schemaProcessor, $schema);
185✔
155
        $this->parsedExternalFileSchemas[$jsonSchemaFile] = $schema;
185✔
156

157
        return $schema->getSchemaDictionary()->getDefinition($externalKey, $schemaProcessor, $path);
185✔
158
    }
159
}
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