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

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

04 Mar 2026 11:34PM UTC coverage: 98.631% (-0.06%) from 98.693%
22694612910

Pull #115

github

web-flow
Merge c82ead07c into d14ae3d85
Pull Request #115: Add native PHP union type hints (fixes #110, fixes #114)

178 of 183 new or added lines in 18 files covered. (97.27%)

6 existing lines in 2 files now uncovered.

3601 of 3651 relevant lines covered (98.63%)

555.14 hits per line

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

99.08
/src/PropertyProcessor/ComposedValue/AbstractComposedValueProcessor.php
1
<?php
2

3
declare(strict_types = 1);
4

5
namespace PHPModelGenerator\PropertyProcessor\ComposedValue;
6

7
use PHPModelGenerator\Exception\SchemaException;
8
use PHPModelGenerator\Model\Property\CompositionPropertyDecorator;
9
use PHPModelGenerator\Model\Property\PropertyInterface;
10
use PHPModelGenerator\Model\Property\PropertyType;
11
use PHPModelGenerator\Model\Schema;
12
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
13
use PHPModelGenerator\Model\Validator;
14
use PHPModelGenerator\Model\Validator\ComposedPropertyValidator;
15
use PHPModelGenerator\Model\Validator\RequiredPropertyValidator;
16
use PHPModelGenerator\PropertyProcessor\Decorator\TypeHint\ClearTypeHintDecorator;
17
use PHPModelGenerator\PropertyProcessor\Decorator\TypeHint\CompositionTypeHintDecorator;
18
use PHPModelGenerator\PropertyProcessor\Property\AbstractValueProcessor;
19
use PHPModelGenerator\PropertyProcessor\PropertyMetaDataCollection;
20
use PHPModelGenerator\PropertyProcessor\PropertyFactory;
21
use PHPModelGenerator\PropertyProcessor\PropertyProcessorFactory;
22
use PHPModelGenerator\SchemaProcessor\SchemaProcessor;
23
use PHPModelGenerator\Utils\RenderHelper;
24

25
/**
26
 * Class AbstractComposedValueProcessor
27
 *
28
 * @package PHPModelGenerator\PropertyProcessor\ComposedValue
29
 */
30
abstract class AbstractComposedValueProcessor extends AbstractValueProcessor
31
{
32
    private ?PropertyInterface $mergedProperty = null;
33

34
    /**
35
     * AbstractComposedValueProcessor constructor.
36
     */
37
    public function __construct(
593✔
38
        PropertyMetaDataCollection $propertyMetaDataCollection,
39
        SchemaProcessor $schemaProcessor,
40
        Schema $schema,
41
        private bool $rootLevelComposition,
42
    ) {
43
        parent::__construct($propertyMetaDataCollection, $schemaProcessor, $schema);
593✔
44
    }
45

46
    /**
47
     * @inheritdoc
48
     */
49
    protected function generateValidators(PropertyInterface $property, JsonSchema $propertySchema): void
593✔
50
    {
51
        $json = $propertySchema->getJson()['propertySchema']->getJson();
593✔
52

53
        if (empty($json[$propertySchema->getJson()['type']]) &&
593✔
54
            $this->schemaProcessor->getGeneratorConfiguration()->isOutputEnabled()
593✔
55
        ) {
56
            // @codeCoverageIgnoreStart
57
            echo "Warning: empty composition for {$property->getName()} may lead to unexpected results\n";
58
            // @codeCoverageIgnoreEnd
59
        }
60

61
        $compositionProperties = $this->getCompositionProperties($property, $propertySchema);
593✔
62

63
        $resolvedCompositions = 0;
593✔
64
        foreach ($compositionProperties as $compositionProperty) {
593✔
65
            $compositionProperty->onResolve(
575✔
66
                function () use (&$resolvedCompositions, $property, $compositionProperties, $propertySchema): void {
575✔
67
                    if (++$resolvedCompositions === count($compositionProperties)) {
575✔
68
                        $this->transferPropertyType($property, $compositionProperties);
575✔
69

70
                        $this->mergedProperty = !$this->rootLevelComposition
575✔
71
                            && $this instanceof MergedComposedPropertiesInterface
575✔
72
                                ? $this->schemaProcessor->createMergedProperty(
184✔
73
                                    $this->schema,
184✔
74
                                    $property,
184✔
75
                                    $compositionProperties,
184✔
76
                                    $propertySchema,
184✔
77
                                  )
184✔
78
                                : null;
407✔
79
                    }
80
                },
575✔
81
            );
575✔
82
        }
83

84
        $availableAmount = count($compositionProperties);
593✔
85

86
        $property->addValidator(
593✔
87
            new ComposedPropertyValidator(
593✔
88
                $this->schemaProcessor->getGeneratorConfiguration(),
593✔
89
                $property,
593✔
90
                $compositionProperties,
593✔
91
                static::class,
593✔
92
                [
593✔
93
                    'compositionProperties' => $compositionProperties,
593✔
94
                    'schema' => $this->schema,
593✔
95
                    'generatorConfiguration' => $this->schemaProcessor->getGeneratorConfiguration(),
593✔
96
                    'viewHelper' => new RenderHelper($this->schemaProcessor->getGeneratorConfiguration()),
593✔
97
                    'availableAmount' => $availableAmount,
593✔
98
                    'composedValueValidation' => $this->getComposedValueValidation($availableAmount),
593✔
99
                    // if the property is a composed property the resulting value of a validation must be proposed
100
                    // to be the final value after the validations (e.g. object instantiations may be performed).
101
                    // Otherwise (eg. a NotProcessor) the value must be proposed before the validation
102
                    'postPropose' => $this instanceof ComposedPropertiesInterface,
593✔
103
                    'mergedProperty' => &$this->mergedProperty,
593✔
104
                    'onlyForDefinedValues' =>
593✔
105
                        $propertySchema->getJson()['onlyForDefinedValues']
593✔
106
                        && $this instanceof ComposedPropertiesInterface,
593✔
107
                ],
593✔
108
            ),
593✔
109
            100,
593✔
110
        );
593✔
111
    }
112

113
    /**
114
     * Set up composition properties for the given property schema
115
     *
116
     * @return CompositionPropertyDecorator[]
117
     *
118
     * @throws SchemaException
119
     */
120
    protected function getCompositionProperties(PropertyInterface $property, JsonSchema $propertySchema): array
593✔
121
    {
122
        $propertyFactory = new PropertyFactory(new PropertyProcessorFactory());
593✔
123
        $compositionProperties = [];
593✔
124
        $json = $propertySchema->getJson()['propertySchema']->getJson();
593✔
125

126
        // clear the base type of the property to keep only the types of the composition.
127
        // This avoids e.g. "array|int[]" for a property which is known to contain always an integer array
128
        $property->addTypeHintDecorator(new ClearTypeHintDecorator());
593✔
129

130
        foreach ($json[$propertySchema->getJson()['type']] as $compositionElement) {
593✔
131
            $compositionSchema = $propertySchema->getJson()['propertySchema']->withJson($compositionElement);
575✔
132

133
            $compositionProperty = new CompositionPropertyDecorator(
575✔
134
                $property->getName(),
575✔
135
                $compositionSchema,
575✔
136
                $propertyFactory
575✔
137
                    ->create(
575✔
138
                        new PropertyMetaDataCollection([$property->getName() => $property->isRequired()]),
575✔
139
                        $this->schemaProcessor,
575✔
140
                        $this->schema,
575✔
141
                        $property->getName(),
575✔
142
                        $compositionSchema,
575✔
143
                    )
575✔
144
            );
575✔
145

146
            $compositionProperty->onResolve(function () use ($compositionProperty, $property): void {
575✔
147
                $compositionProperty->filterValidators(static fn(Validator $validator): bool =>
575✔
148
                    !is_a($validator->getValidator(), RequiredPropertyValidator::class) &&
575✔
149
                    !is_a($validator->getValidator(), ComposedPropertyValidator::class)
575✔
150
                );
575✔
151

152
                // only create a composed type hint if we aren't a AnyOf or an AllOf processor and the
153
                // compositionProperty contains no object. This results in objects being composed each separately for a
154
                // OneOf processor (e.g. string|ObjectA|ObjectB). For a merged composed property the objects are merged
155
                // together, so it results in string|MergedObject
156
                if (!($this instanceof MergedComposedPropertiesInterface && $compositionProperty->getNestedSchema())) {
575✔
157
                    $property->addTypeHintDecorator(new CompositionTypeHintDecorator($compositionProperty));
359✔
158
                }
159
            });
575✔
160

161
            $compositionProperties[] = $compositionProperty;
575✔
162
        }
163

164
        return $compositionProperties;
593✔
165
    }
166

167
    /**
168
     * Check if the provided property can inherit a single type from the composition properties.
169
     *
170
     * @param CompositionPropertyDecorator[] $compositionProperties
171
     */
172
    private function transferPropertyType(PropertyInterface $property, array $compositionProperties): void
575✔
173
    {
174
        if ($this instanceof NotProcessor) {
575✔
175
            return;
92✔
176
        }
177

178
        // Skip widening when any branch has a nested schema (object): the merged-property
179
        // mechanism creates a combined class whose name is not among the per-branch type names.
180
        foreach ($compositionProperties as $p) {
483✔
181
            if ($p->getNestedSchema() !== null) {
483✔
182
                return;
355✔
183
            }
184
        }
185

186
        // Flatten all type names from all branches. Use getNames() to handle branches that
187
        // already carry a union PropertyType (e.g. from Phase 4 or Phase 5).
188
        $allNames = array_merge(...array_map(
129✔
189
            static fn(CompositionPropertyDecorator $p): array => $p->getType() ? $p->getType()->getNames() : [],
129✔
190
            $compositionProperties,
129✔
191
        ));
129✔
192

193
        // A branch with no type contributes nothing but signals that nullable=true is required.
194
        $hasBranchWithNoType = array_filter(
129✔
195
            $compositionProperties,
129✔
196
            static fn(CompositionPropertyDecorator $p): bool => $p->getType() === null,
129✔
197
        ) !== [];
129✔
198

199
        // An optional branch (property not required in that branch) means the property can be
200
        // absent at runtime, causing the root getter to return null. This is a structural
201
        // nullable — independent of the implicit-null configuration setting.
202
        //
203
        // For oneOf/anyOf: any optional branch makes the property nullable (the branch that
204
        // omits the property can match, leaving the value as null).
205
        //
206
        // For allOf: all branches must hold simultaneously. If at least one branch marks the
207
        // property as required, the property is required overall — an optional branch in allOf
208
        // does not by itself make the property nullable. Only if NO branch requires the property
209
        // (i.e. the property is optional across all allOf branches) is it structurally nullable.
210
        $hasBranchWithRequiredProperty = array_filter(
129✔
211
            $compositionProperties,
129✔
212
            static fn(CompositionPropertyDecorator $p): bool => $p->isRequired(),
129✔
213
        ) !== [];
129✔
214
        $hasBranchWithOptionalProperty = $this instanceof AllOfProcessor
129✔
215
            ? !$hasBranchWithRequiredProperty
25✔
216
            : array_filter(
104✔
217
                $compositionProperties,
104✔
218
                static fn(CompositionPropertyDecorator $p): bool => !$p->isRequired(),
104✔
219
            ) !== [];
104✔
220

221
        // Strip 'null' → nullable flag; PropertyType constructor deduplicates the rest.
222
        $hasNull = in_array('null', $allNames, true);
129✔
223
        $nonNullNames = array_values(array_filter(
129✔
224
            array_unique($allNames),
129✔
225
            fn(string $t): bool => $t !== 'null',
129✔
226
        ));
129✔
227

228
        if (!$nonNullNames) {
129✔
NEW
229
            return;
×
230
        }
231

232
        $nullable = ($hasNull || $hasBranchWithNoType || $hasBranchWithOptionalProperty) ? true : null;
129✔
233

234
        $property->setType(new PropertyType($nonNullNames, $nullable));
129✔
235
    }
236

237
    /**
238
     * @param int $composedElements The amount of elements which are composed together
239
     */
240
    abstract protected function getComposedValueValidation(int $composedElements): string;
241
}
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