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

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

11 Apr 2025 04:41PM UTC coverage: 98.813%. Remained the same
14407937636

Pull #88

github

web-flow
Merge 7dcf4b3f6 into 8823c7c14
Pull Request #88: Fix code typo in README

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

94.34
/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 string $fileName  The file name
28
     * @param string $classPath The relative path of the class for namespace generation
29
     * @param string $className The class name
30
     * @param Schema $schema    The Schema object which holds properties and validators
31
     */
32
    public function __construct(
33
        protected string $fileName,
34
        protected string $classPath,
35
        protected string $className,
36
        protected Schema $schema,
37
    ) {}
1,906✔
38

39
    /**
40
     * @param PostProcessor[] $postProcessors
41
     */
42
    public function postProcess(array $postProcessors, GeneratorConfiguration $generatorConfiguration): void
43
    {
44
        foreach ($postProcessors as $postProcessor) {
1,906✔
45
            $postProcessor->process($this->schema, $generatorConfiguration);
1,906✔
46
        }
47
    }
48

49
    /**
50
     * Execute the render job and render the class
51
     *
52
     * @throws FileSystemException
53
     * @throws RenderException
54
     */
55
    public function render(GeneratorConfiguration $generatorConfiguration): void
56
    {
57
        $this->generateModelDirectory();
1,891✔
58

59
        $class = $this->renderClass($generatorConfiguration);
1,891✔
60

61
        if (file_exists($this->fileName)) {
1,891✔
62
            throw new FileSystemException("File {$this->fileName} already exists. Make sure object IDs are unique.");
1✔
63
        }
64

65
        if (!file_put_contents($this->fileName, $class)) {
1,891✔
66
            // @codeCoverageIgnoreStart
67
            throw new FileSystemException("Can't write class $this->classPath\\$this->className.");
68
            // @codeCoverageIgnoreEnd
69
        }
70

71
        if ($generatorConfiguration->isOutputEnabled()) {
1,891✔
72
            echo sprintf(
1✔
73
                "Rendered class %s\n",
1✔
74
                join(
1✔
75
                    '\\',
1✔
76
                    array_filter([$generatorConfiguration->getNamespacePrefix(), $this->classPath, $this->className]),
1✔
77
                )
1✔
78
            );
1✔
79
        }
80
    }
81

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

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

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

127
        return $class;
1,891✔
128
    }
129

130
    /**
131
     * @return string[]
132
     */
133
    protected function getUseForSchema(GeneratorConfiguration $generatorConfiguration, string $namespace): array
134
    {
135
        $use = array_unique(
1,891✔
136
            array_merge(
1,891✔
137
                $this->schema->getUsedClasses(),
1,891✔
138
                $generatorConfiguration->collectErrors()
1,891✔
139
                    ? [$generatorConfiguration->getErrorRegistryClass()]
492✔
140
                    : [ValidationException::class],
1,891✔
141
            )
1,891✔
142
        );
1,891✔
143

144
        // filter out non-compound uses and uses which link to the current namespace
145
        $use = array_filter($use, static fn($classPath): bool =>
1,891✔
146
            strstr(trim(str_replace("$namespace", '', $classPath), '\\'), '\\') ||
1,891✔
147
            (!strstr($classPath, '\\') && !empty($namespace)),
1,891✔
148
        );
1,891✔
149

150
        return $use;
1,891✔
151
    }
152
}
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