Skip to content

Commit 8d1fdda

Browse files
author
Enno Woortmann
committed
Add ObjectProcessor test cases
Optimize ObjectInstantiationDecorator to catch invalid constructor calls
1 parent 6742880 commit 8d1fdda

19 files changed

+309
-31
lines changed

src/Generator.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,9 @@ public function generateModels(string $source, string $destination): array
6161

6262
$renderProxy = new RenderQueue();
6363
$schemaProcessor = new SchemaProcessor($source, $destination, $this->generatorConfiguration, $renderProxy);
64-
$generatedFiles = [];
6564

6665
foreach ($this->getSchemaFiles($source) as $jsonSchemaFile) {
67-
$generatedFiles[] = $schemaProcessor->process($jsonSchemaFile);
66+
$schemaProcessor->process($jsonSchemaFile);
6867
}
6968

7069
// render all collected classes
@@ -77,7 +76,7 @@ public function generateModels(string $source, string $destination): array
7776
// @codeCoverageIgnoreEnd
7877
}
7978

80-
return $generatedFiles;
79+
return $schemaProcessor->getGeneratedFiles();
8180
}
8281

8382
/**

src/Model/Property/Property.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public function hasDecorators(): bool
170170
/**
171171
* @inheritdoc
172172
*/
173-
public function getClasses(): array
173+
public function getExceptionClasses(): array
174174
{
175175
$use = [];
176176

@@ -179,7 +179,11 @@ public function getClasses(): array
179179
}
180180

181181
foreach ($this->getNestedProperties() as $property) {
182-
$use = array_merge($use, $property->getClasses());
182+
$use = array_merge($use, $property->getExceptionClasses());
183+
}
184+
185+
foreach ($this->decorators as $decorator) {
186+
$use = array_merge($use, $decorator->getExceptionClasses());
183187
}
184188

185189
return $use;

src/Model/Property/PropertyInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function hasDecorators(): bool;
9999
*
100100
* @return array
101101
*/
102-
public function getClasses(): array;
102+
public function getExceptionClasses(): array;
103103

104104
/**
105105
* @param bool $isPropertyRequired

src/Model/Property/PropertyProxy.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ public function hasDecorators(): bool
141141
/**
142142
* @inheritdoc
143143
*/
144-
public function getClasses(): array
144+
public function getExceptionClasses(): array
145145
{
146-
return $this->getProperty()->getClasses();
146+
return $this->getProperty()->getExceptionClasses();
147147
}
148148

149149
/**

src/Model/Schema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function getUseList(bool $skipGlobalNamespace): array
9292
continue;
9393
}
9494

95-
$use = array_merge($use, [Exception::class], $property->getClasses());
95+
$use = array_merge($use, [Exception::class], $property->getExceptionClasses());
9696
}
9797

9898
if ($skipGlobalNamespace) {

src/PropertyProcessor/Decorator/IntToFloatCastDecorator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ public function decorate(string $input): string
1818
{
1919
return "is_int($input) ? (float) $input : $input";
2020
}
21+
22+
/**
23+
* @inheritdoc
24+
*/
25+
public function getExceptionClasses(): array
26+
{
27+
return [];
28+
}
2129
}

src/PropertyProcessor/Decorator/ObjectInstantiationDecorator.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44

55
namespace PHPModelGenerator\PropertyProcessor\Decorator;
66

7+
use PHPMicroTemplate\Render;
8+
use PHPModelGenerator\Exception\InvalidArgumentException;
9+
710
/**
811
* Class ObjectInstantiationDecorator
912
*
1013
* @package PHPModelGenerator\PropertyProcessor\Decorator
1114
*/
1215
class ObjectInstantiationDecorator implements PropertyDecoratorInterface
1316
{
17+
/** @var Render */
18+
protected static $renderer;
1419
/** @var string */
1520
protected $className;
1621

@@ -22,13 +27,33 @@ class ObjectInstantiationDecorator implements PropertyDecoratorInterface
2227
public function __construct(string $className)
2328
{
2429
$this->className = $className;
30+
31+
if (!static::$renderer) {
32+
static::$renderer = new Render(
33+
join(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', 'Templates']) . DIRECTORY_SEPARATOR
34+
);
35+
}
2536
}
2637

2738
/**
2839
* @inheritdoc
2940
*/
3041
public function decorate(string $input): string
3142
{
32-
return "new {$this->className}($input)";
43+
return static::$renderer->renderTemplate(
44+
DIRECTORY_SEPARATOR . 'Decorator' . DIRECTORY_SEPARATOR . 'ObjectInstantiationDecorator.phptpl',
45+
[
46+
'input' => $input,
47+
'className' => $this->className
48+
]
49+
);
50+
}
51+
52+
/**
53+
* @inheritdoc
54+
*/
55+
public function getExceptionClasses(): array
56+
{
57+
return [InvalidArgumentException::class];
3358
}
3459
}

src/PropertyProcessor/Decorator/PropertyDecoratorInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@ interface PropertyDecoratorInterface
1919
* @return string
2020
*/
2121
public function decorate(string $input): string;
22+
23+
/**
24+
* Return a list of all exception classes which may be thrown by the decorator
25+
*
26+
* @return array
27+
*/
28+
public function getExceptionClasses(): array;
2229
}

src/PropertyProcessor/Decorator/PropertyTransferDecorator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,12 @@ public function decorate(string $input): string
3636
{
3737
return $this->property->resolveDecorator($input);
3838
}
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
public function getExceptionClasses(): array
44+
{
45+
return [];
46+
}
3947
}

src/PropertyProcessor/Property/ArrayProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private function addItemsValidation(PropertyInterface $property, array $property
126126
new PropertyTemplateValidator(
127127
InvalidArgumentException::class,
128128
'Invalid array item',
129-
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'ArrayItem.vtpl',
129+
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'ArrayItem.phptpl',
130130
[
131131
'property' => $nestedProperty,
132132
'viewHelper' => new RenderHelper(),

0 commit comments

Comments
 (0)