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

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

09 Dec 2025 11:52AM UTC coverage: 98.624% (-0.01%) from 98.636%
20062523725

Pull #95

github

Enno Woortmann
Update docs, add test cases
Pull Request #95: Improve the resolving of references

65 of 66 new or added lines in 7 files covered. (98.48%)

1 existing line in 1 file now uncovered.

3440 of 3488 relevant lines covered (98.62%)

559.67 hits per line

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

88.57
/src/Model/SchemaDefinition/SchemaDefinition.php
1
<?php
2

3
declare(strict_types = 1);
4

5
namespace PHPModelGenerator\Model\SchemaDefinition;
6

7
use PHPModelGenerator\Exception\PHPModelGeneratorException;
8
use PHPModelGenerator\Exception\SchemaException;
9
use PHPModelGenerator\Model\Property\PropertyInterface;
10
use PHPModelGenerator\Model\Property\PropertyProxy;
11
use PHPModelGenerator\Model\Schema;
12
use PHPModelGenerator\PropertyProcessor\PropertyMetaDataCollection;
13
use PHPModelGenerator\PropertyProcessor\PropertyFactory;
14
use PHPModelGenerator\PropertyProcessor\PropertyProcessorFactory;
15
use PHPModelGenerator\SchemaProcessor\SchemaProcessor;
16

17
/**
18
 * Class SchemaDefinition
19
 *
20
 * Hold a definition from a schema
21
 *
22
 * @package PHPModelGenerator\Model
23
 */
24
class SchemaDefinition
25
{
26
    protected ResolvedDefinitionsCollection $resolvedPaths;
27
    /** @var array */
28
    protected $unresolvedProxies = [];
29

30
    /**
31
     * SchemaDefinition constructor.
32
     */
33
    public function __construct(
1,980✔
34
        protected JsonSchema $source,
35
        protected SchemaProcessor $schemaProcessor,
36
        protected Schema $schema,
37
    ) {
38
        $this->resolvedPaths = new ResolvedDefinitionsCollection();
1,980✔
39
    }
40

41
    public function getSchema(): Schema
522✔
42
    {
43
        return $this->schema;
522✔
44
    }
45

46
    /**
47
     * Resolve a reference
48
     *
49
     * @throws PHPModelGeneratorException
50
     * @throws SchemaException
51
     */
52
    public function resolveReference(
522✔
53
        string $propertyName,
54
        array $path,
55
        PropertyMetaDataCollection $propertyMetaDataCollection,
56
    ): PropertyInterface {
57
        $jsonSchema = $this->source->getJson();
522✔
58
        $originalPath = $path;
522✔
59

60
        while ($segment = array_shift($path)) {
522✔
61
            if (!isset($jsonSchema[$segment])) {
403✔
UNCOV
62
                throw new SchemaException("Unresolved path segment $segment in file {$this->source->getFile()}");
×
63
            }
64

65
            $jsonSchema = $jsonSchema[$segment];
403✔
66
        }
67

68
        // if the properties point to the same definition and share identical metadata the generated property can be
69
        // recycled. Otherwise, a new property must be generated as diverging metadata lead to different validators.
70
        $key = implode('-', [...$originalPath, $propertyMetaDataCollection->getHash($propertyName)]);
522✔
71

72
        if (!$this->resolvedPaths->offsetExists($key)) {
522✔
73
            // create a dummy entry for the path first. If the path is used recursive the recursive usages will point
74
            // to the currently created property
75
            $this->resolvedPaths->offsetSet($key, null);
522✔
76

77
            try {
78
                $property =  (new PropertyFactory(new PropertyProcessorFactory()))
522✔
79
                    ->create(
522✔
80
                        $propertyMetaDataCollection,
522✔
81
                        $this->schemaProcessor,
522✔
82
                        $this->schema,
522✔
83
                        $propertyName,
522✔
84
                        $this->source->withJson($jsonSchema),
522✔
85
                    );
522✔
86
                $this->resolvedPaths->offsetSet($key, $property);
522✔
87

88
                /** @var PropertyProxy $proxy */
89
                foreach ($this->unresolvedProxies[$key] ?? [] as $proxy) {
522✔
90
                    $proxy->resolve();
91✔
91
                }
92

93
                unset($this->unresolvedProxies[$key]);
522✔
94

95
                return $property;
522✔
96
            } catch (PHPModelGeneratorException $exception) {
×
97
                $this->resolvedPaths->offsetUnset($key);
×
98
                throw $exception;
×
99
            }
100
        }
101

102
        $proxy = new PropertyProxy($propertyName, $this->source, $this->resolvedPaths, $key);
98✔
103
        $this->unresolvedProxies[$key][] = $proxy;
98✔
104

105
        if ($this->resolvedPaths->offsetGet($key)) {
98✔
106
            $proxy->resolve();
7✔
107
        }
108

109
        return $proxy;
98✔
110
    }
111
}
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

© 2025 Coveralls, Inc