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

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

18 Nov 2024 11:05AM UTC coverage: 98.813%. Remained the same
11891115369

push

github

wol-soft
array items fake to be required to ensure null-safe checks to not enforce each place implementing checks to handle array items specifically. Don't pollute the PropertyMetaDataCollection (which is now used to generate a cache key for properties) with this information as the items aren't really required.

5 of 5 new or added lines in 4 files covered. (100.0%)

2 existing lines in 1 file now uncovered.

2914 of 2949 relevant lines covered (98.81%)

517.53 hits per line

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

90.63
/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(
34
        protected JsonSchema $source,
35
        protected SchemaProcessor $schemaProcessor,
36
        protected Schema $schema,
37
    ) {
38
        $this->resolvedPaths = new ResolvedDefinitionsCollection();
1,966✔
39
    }
40

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

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

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

65
            $jsonSchema = $jsonSchema[$segment];
400✔
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)]);
518✔
71

72
        if (!$this->resolvedPaths->offsetExists($key)) {
518✔
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);
518✔
76

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

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

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

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

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

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

109
        return $proxy;
95✔
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