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

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

12 May 2026 08:42AM UTC coverage: 98.554% (+0.003%) from 98.551%
25723523596

push

github

web-flow
Merge pull request #126 from wol-soft/jsonSchemaDraft7

Json schema draft7

244 of 252 new or added lines in 13 files covered. (96.83%)

4 existing lines in 4 files now uncovered.

4770 of 4840 relevant lines covered (98.55%)

643.67 hits per line

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

98.18
/src/SchemaProcessor/PostProcessor/Internal/AdditionalPropertiesPostProcessor.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace PHPModelGenerator\SchemaProcessor\PostProcessor\Internal;
6

7
use PHPModelGenerator\Exception\Object\InvalidAdditionalPropertiesException;
8
use PHPModelGenerator\Exception\SchemaException;
9
use PHPModelGenerator\Model\GeneratorConfiguration;
10
use PHPModelGenerator\Model\Property\Property;
11
use PHPModelGenerator\Model\Property\PropertyType;
12
use PHPModelGenerator\Model\Schema;
13
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
14
use PHPModelGenerator\Model\Validator\AdditionalPropertiesValidator;
15
use PHPModelGenerator\Model\Validator\PropertyTemplateValidator;
16
use PHPModelGenerator\PropertyProcessor\Decorator\TypeHint\ArrayTypeHintDecorator;
17
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
18
use PHPModelGenerator\Utils\RenderHelper;
19

20
/**
21
 * Class AdditionalPropertiesPostProcessor
22
 *
23
 * @package PHPModelGenerator\SchemaProcessor\PostProcessor\Internal
24
 */
25
class AdditionalPropertiesPostProcessor extends PostProcessor
26
{
27
    /**
28
     * @throws SchemaException
29
     */
30
    public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void
2,235✔
31
    {
32
        $json = $schema->getJsonSchema()->getJson();
2,235✔
33

34
        if (isset($json['additionalProperties']) && $json['additionalProperties'] !== false) {
2,235✔
35
            $this->addAdditionalPropertiesCollectionProperty($schema);
92✔
36
        }
37
    }
38

39
    /**
40
     * @throws SchemaException
41
     */
42
    public function addAdditionalPropertiesCollectionProperty(Schema $schema): void
103✔
43
    {
44
        $validationProperty = null;
103✔
45
        foreach ($schema->getBaseValidators() as $validator) {
103✔
46
            if (is_a($validator, AdditionalPropertiesValidator::class)) {
97✔
47
                $validator->setCollectAdditionalProperties(true);
89✔
48
                $validationProperty = $validator->getValidationProperty();
89✔
49
            }
50
        }
51

52
        $additionalPropertiesCollectionProperty = (new Property(
103✔
53
            'additionalProperties',
103✔
54
            new PropertyType('array'),
103✔
55
            new JsonSchema(__FILE__, []),
103✔
56
            'Collect all additional properties provided to the schema',
103✔
57
        ))
103✔
58
            ->setDefaultValue([])
103✔
59
            ->setInternal(true);
103✔
60

61
        if ($validationProperty) {
103✔
62
            $additionalPropertiesCollectionProperty->addTypeHintDecorator(
89✔
63
                new ArrayTypeHintDecorator($validationProperty),
89✔
64
            );
89✔
65
        }
66

67
        $schema->addProperty($additionalPropertiesCollectionProperty);
103✔
68

69
        $json = $schema->getJsonSchema()->getJson();
103✔
70
        if (!isset($json['additionalProperties']) || $json['additionalProperties'] === true) {
103✔
71
            $this->addUpdateAdditionalProperties($schema);
14✔
72
        }
73
    }
74

75
    /**
76
     * Usually the AdditionalPropertiesValidator validates all additional properties against the constraints and updates
77
     * the internal storage of the additional properties. If no additional property constraints are defined for the
78
     * schema the provided additional properties must be updated separately as no AdditionalPropertiesValidator is added
79
     * to the generated class.
80
     */
81
    private function addUpdateAdditionalProperties(Schema $schema): void
14✔
82
    {
83
        $schema->addBaseValidator(
14✔
84
            new class ($schema) extends PropertyTemplateValidator {
14✔
85
                public function __construct(Schema $schema)
86
                {
87
                    $patternProperties = array_keys($schema->getJsonSchema()->getJson()['patternProperties'] ?? []);
14✔
88

89
                    parent::__construct(
14✔
90
                        new Property($schema->getClassName(), null, $schema->getJsonSchema()),
14✔
91
                        join(
14✔
92
                            DIRECTORY_SEPARATOR,
14✔
93
                            [
14✔
94
                                '..',
14✔
95
                                'SchemaProcessor',
14✔
96
                                'PostProcessor',
14✔
97
                                'Templates',
14✔
98
                                'AdditionalProperties',
14✔
99
                                'UpdateAdditionalProperties.phptpl',
14✔
100
                            ],
14✔
101
                        ),
14✔
102
                        [
14✔
103
                            'patternProperties' => $patternProperties
14✔
104
                                ? RenderHelper::varExportArray($patternProperties)
8✔
UNCOV
105
                                : null,
×
106
                            'additionalProperties' => RenderHelper::varExportArray(
14✔
107
                                array_keys($schema->getJsonSchema()->getJson()['properties'] ?? []),
14✔
108
                            ),
14✔
109
                        ],
14✔
110
                        InvalidAdditionalPropertiesException::class,
14✔
111
                    );
14✔
112
                }
113
            },
14✔
114
        );
14✔
115
    }
116
}
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