Skip to content

Commit 7638922

Browse files
committed
Add check for overwriting files
1 parent 178850c commit 7638922

File tree

7 files changed

+48
-3
lines changed

7 files changed

+48
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/docs/build/
22
/tests/manual/result/
3+
/failed-classes/
34
/vendor/
45
.idea
56
composer.lock

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,4 @@ After installing the dependencies of the library via `composer update` you can e
176176

177177
During the execution the tests will create a directory PHPModelGeneratorTest in tmp where JSON-Schema files and PHP classes will be written to.
178178

179-
If a test which creates a PHP class from a JSON-Schema fails the JSON-Schema and the generated class(es) will be dumped to the directory `./Failed-classes`
179+
If a test which creates a PHP class from a JSON-Schema fails the JSON-Schema and the generated class(es) will be dumped to the directory `./failed-classes`

src/Model/RenderJob.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,13 @@ public function render(string $destination, GeneratorConfiguration $generatorCon
5353

5454
$class = $this->renderClass($generatorConfiguration);
5555

56+
if (file_exists($this->fileName)) {
57+
throw new FileSystemException("File {$this->fileName} already exists. Make sure object IDs are unique.");
58+
}
59+
5660
if (!file_put_contents($this->fileName, $class)) {
5761
// @codeCoverageIgnoreStart
58-
throw new FileSystemException("Can't write class $this->classPath\\$this->className");
62+
throw new FileSystemException("Can't write class $this->classPath\\$this->className.");
5963
// @codeCoverageIgnoreEnd
6064
}
6165

src/PropertyProcessor/ComposedValue/AbstractComposedValueProcessor.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
*/
2727
abstract class AbstractComposedValueProcessor extends AbstractValueProcessor
2828
{
29+
private static $generatedMergedProperties = [];
30+
2931
/**
3032
* @inheritdoc
3133
*/
@@ -115,8 +117,14 @@ private function createMergedProperty(
115117
$propertyData['propertyData']['id'] ?? $compositionProperty->getName()
116118
);
117119

120+
// check if the merged property already has been generated
121+
if (isset(self::$generatedMergedProperties[$mergedClassName])) {
122+
return self::$generatedMergedProperties[$mergedClassName];
123+
}
124+
118125
$mergedPropertySchema = new Schema($mergedClassName);
119126
$mergedProperty = new Property('MergedProperty', $mergedClassName);
127+
self::$generatedMergedProperties[$mergedClassName] = $mergedProperty;
120128

121129
foreach ($properties as $property) {
122130
if ($property->getNestedSchema()) {

tests/Basic/BasicSchemaGenerationTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPModelGenerator\Tests\Basic;
44

5+
use PHPModelGenerator\Exception\FileSystemException;
56
use PHPModelGenerator\Exception\SchemaException;
67
use PHPModelGenerator\ModelGenerator;
78
use PHPModelGenerator\Model\GeneratorConfiguration;
@@ -233,4 +234,12 @@ public function testIdenticalSchemasAreMappedToOneClass(): void
233234
$reflection->getProperty('object2')->getDocComment()
234235
);
235236
}
237+
238+
public function testDuplicateIdThrowsAnException(): void
239+
{
240+
$this->expectException(FileSystemException::class);
241+
$this->expectExceptionMessageMatches('/File (.*) already exists. Make sure object IDs are unique./');
242+
243+
$this->generateClassFromFile('DuplicateId.json');
244+
}
236245
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"property": {
5+
"type": "object",
6+
"id": "Person",
7+
"properties": {
8+
"name": {
9+
"type": "string"
10+
}
11+
}
12+
},
13+
"property2": {
14+
"type": "object",
15+
"id": "Person",
16+
"properties": {
17+
"age": {
18+
"type": "integer"
19+
}
20+
}
21+
}
22+
}
23+
}

tests/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
const FAILED_CLASSES_PATH = __DIR__ . '/../Failed-classes/';
3+
const FAILED_CLASSES_PATH = __DIR__ . '/../failed-classes/';
44

55
if (is_dir(FAILED_CLASSES_PATH)) {
66
$di = new RecursiveDirectoryIterator(FAILED_CLASSES_PATH, FilesystemIterator::SKIP_DOTS);

0 commit comments

Comments
 (0)