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

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

16 Sep 2025 02:21PM UTC coverage: 98.287% (-0.3%) from 98.564%
17768946986

Pull #92

github

Enno Woortmann
remove unnecessary function
Pull Request #92: BuilderClassPostProcessor

123 of 135 new or added lines in 9 files covered. (91.11%)

4 existing lines in 1 file now uncovered.

3385 of 3444 relevant lines covered (98.29%)

564.05 hits per line

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

89.86
/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,907✔
30
        protected Schema $schema,
31
    ) {}
1,907✔
32

33
    /**
34
     * @param PostProcessor[] $postProcessors
35
     */
36
    public function postProcess(array $postProcessors, GeneratorConfiguration $generatorConfiguration): void
1,907✔
37
    {
38
        foreach ($postProcessors as $postProcessor) {
1,907✔
39
            $postProcessor->process($this->schema, $generatorConfiguration);
1,907✔
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,892✔
50
    {
51
        $this->generateModelDirectory();
1,892✔
52

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

55
        if (file_exists($this->schema->getTargetFileName())) {
1,892✔
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,892✔
62
            // @codeCoverageIgnoreStart
63
            throw new FileSystemException(
64
                "Can't write class {$this->schema->getClassPath()}\\{$this->schema->getClassName()}.",
65
            );
66
            // @codeCoverageIgnoreEnd
67
        }
68

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

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

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

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

135
        return $class;
1,892✔
136
    }
137

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