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

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

18 Sep 2025 01:40PM UTC coverage: 98.434% (-0.1%) from 98.564%
17830597583

Pull #92

github

web-flow
Merge 59383d515 into 0e2c43036
Pull Request #92: BuilderClassPostProcessor

135 of 142 new or added lines in 10 files covered. (95.07%)

2 existing lines in 1 file now uncovered.

3394 of 3448 relevant lines covered (98.43%)

564.49 hits per line

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

90.0
/src/Model/RenderJob.php
1
<?php
2

3
declare(strict_types = 1);
4

5
namespace PHPModelGenerator\Model;
6

7
use PHPMicroTemplate\Exception\PHPMicroTemplateException;
8
use PHPMicroTemplate\Render;
9
use PHPModelGenerator\Exception\FileSystemException;
10
use PHPModelGenerator\Exception\RenderException;
11
use PHPModelGenerator\Exception\ValidationException;
12
use PHPModelGenerator\Model\Validator\AbstractComposedPropertyValidator;
13
use PHPModelGenerator\SchemaProcessor\Hook\SchemaHookResolver;
14
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
15
use PHPModelGenerator\Utils\RenderHelper;
16

17
/**
18
 * Class RenderJob
19
 *
20
 * @package PHPModelGenerator\Model
21
 */
22
class RenderJob
23
{
24
    /**
25
     * Create a new class render job
26
     *
27
     * @param Schema $schema The Schema object which holds properties and validators
28
     */
29
    public function __construct(
1,909✔
30
        protected Schema $schema,
31
    ) {}
1,909✔
32

33
    /**
34
     * @param PostProcessor[] $postProcessors
35
     */
36
    public function executePostProcessors(array $postProcessors, GeneratorConfiguration $generatorConfiguration): void
1,909✔
37
    {
38
        foreach ($postProcessors as $postProcessor) {
1,909✔
39
            $postProcessor->process($this->schema, $generatorConfiguration);
1,909✔
40
        }
41
    }
42

43
    /**
44
     * Execute the render job and render the class
45
     *
46
     * @throws FileSystemException
47
     * @throws RenderException
48
     */
49
    public function render(GeneratorConfiguration $generatorConfiguration): void
1,894✔
50
    {
51
        $this->generateModelDirectory();
1,894✔
52

53
        $class = $this->renderClass($generatorConfiguration);
1,894✔
54

55
        if (file_exists($this->schema->getTargetFileName())) {
1,894✔
56
            throw new FileSystemException(
1✔
57
                "File {$this->schema->getTargetFileName()} already exists. Make sure object IDs are unique.",
1✔
58
            );
1✔
59
        }
60

61
        if (!file_put_contents($this->schema->getTargetFileName(), $class)) {
1,894✔
62
            // @codeCoverageIgnoreStart
63
            throw new FileSystemException(
64
                "Can't write class {$this->schema->getClassPath()}\\{$this->schema->getClassName()}.",
65
            );
66
            // @codeCoverageIgnoreEnd
67
        }
68

69
        require $this->schema->getTargetFileName();
1,894✔
70

71
        if ($generatorConfiguration->isOutputEnabled()) {
1,894✔
72
            echo sprintf(
1✔
73
                "Rendered class %s\n",
1✔
74
                join(
1✔
75
                    '\\',
1✔
76
                    array_filter([
1✔
77
                        $generatorConfiguration->getNamespacePrefix(),
1✔
78
                        $this->schema->getClassPath(),
1✔
79
                        $this->schema->getClassName(),
1✔
80
                    ]),
1✔
81
                ),
1✔
82
            );
1✔
83
        }
84
    }
85

86
    /**
87
     * Generate the directory structure for saving a generated class
88
     *
89
     * @throws FileSystemException
90
     */
91
    protected function generateModelDirectory(): void
1,894✔
92
    {
93
        $destination = dirname($this->schema->getTargetFileName());
1,894✔
94
        if (!is_dir($destination) && !mkdir($destination, 0777, true)) {
1,894✔
95
            throw new FileSystemException("Can't create path $destination");
×
96
        }
97
    }
98

99
    /**
100
     * Render a class. Returns the php code of the class
101
     *
102
     * @throws RenderException
103
     */
104
    protected function renderClass(GeneratorConfiguration $generatorConfiguration): string
1,894✔
105
    {
106
        $namespace = trim(
1,894✔
107
            join('\\', [$generatorConfiguration->getNamespacePrefix(), $this->schema->getClassPath()]),
1,894✔
108
            '\\',
1,894✔
109
        );
1,894✔
110

111
        try {
112
            $class = (new Render(__DIR__ . '/../Templates/'))->renderTemplate(
1,894✔
113
                'Model.phptpl',
1,894✔
114
                [
1,894✔
115
                    'namespace'                         => $namespace,
1,894✔
116
                    'use'                               => $this->getUseForSchema($generatorConfiguration, $namespace),
1,894✔
117
                    'schema'                            => $this->schema,
1,894✔
118
                    'schemaHookResolver'                => new SchemaHookResolver($this->schema),
1,894✔
119
                    'generatorConfiguration'            => $generatorConfiguration,
1,894✔
120
                    'viewHelper'                        => new RenderHelper($generatorConfiguration),
1,894✔
121
                    // one hack a day keeps the problems away. Make true literal available for the templating. Easy fix
122
                    'true'                              => true,
1,894✔
123
                    'baseValidatorsWithoutCompositions' => array_filter(
1,894✔
124
                        $this->schema->getBaseValidators(),
1,894✔
125
                        static fn($validator): bool => !is_a($validator, AbstractComposedPropertyValidator::class),
1,894✔
126
                    ),
1,894✔
127
                ],
1,894✔
128
            );
1,894✔
UNCOV
129
        } catch (PHPMicroTemplateException $exception) {
×
NEW
UNCOV
130
            throw new RenderException(
×
NEW
131
                "Can't render class {$this->schema->getClassPath()}\\{$this->schema->getClassName()}",
×
NEW
132
                0,
×
NEW
133
                $exception,
×
NEW
134
            );
×
135
        }
136

137
        return $class;
1,894✔
138
    }
139

140
    /**
141
     * @return string[]
142
     */
143
    protected function getUseForSchema(GeneratorConfiguration $generatorConfiguration, string $namespace): array
1,894✔
144
    {
145
        return RenderHelper::filterClassImports(
1,894✔
146
            array_unique(
1,894✔
147
                array_merge(
1,894✔
148
                    $this->schema->getUsedClasses(),
1,894✔
149
                    $generatorConfiguration->collectErrors()
1,894✔
150
                        ? [$generatorConfiguration->getErrorRegistryClass()]
493✔
151
                        : [ValidationException::class],
1,894✔
152
                ),
1,894✔
153
            ),
1,894✔
154
            $namespace,
1,894✔
155
        );
1,894✔
156
    }
157
}
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