8000 Declare output in a upper level for better abstraction · symfony/symfony@a112c2f · GitHub
[go: up one dir, main page]

Skip to content

Commit a112c2f

Browse files
committed
Declare output in a upper level for better abstraction
1 parent 8349681 commit a112c2f

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

src/Symfony/Component/AstGenerator/Hydrate/HydrateFromObjectGenerator.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ public function __construct(PropertyInfoExtractorInterface $propertyInfoExtracto
3838
*/
3939
public function generate($object, array $context = [])
4040
{
41-
if (!isset($context['input'])) {
42-
throw new MissingContextException('Input variable not defined in context');
41+
if (!isset($context['input']) || !($context['input'] instanceof Expr\Variable)) {
42+
throw new MissingContextException('Input variable not defined or not a Expr\Variable in generation context');
4343
}
4444

45-
$dataVariable = new Expr\Variable('data');
46-
$statements = [$this->getAssignStatement($dataVariable)];
45+
if (!isset($context['output']) || !($context['output'] instanceof Expr\Variable)) {
46+
throw new MissingContextException('Output variable not defined or not a Expr\Variable in generation context');
47+
}
48+
49+
$statements = [$this->getAssignStatement($context['output'])];
4750

4851
foreach ($this->propertyInfoExtractor->getProperties($object, $context) as $property) {
4952
// Only normalize readable property
@@ -53,7 +56,7 @@ public function generate($object, array $context = [])
5356

5457
// @TODO Have property info extractor extract the way of reading a property (public or method with method name)
5558
$input = new Expr\MethodCall($context['input'], 'get'.ucfirst($property));
56-
$output = $this->getSubAssignVariableStatement($dataVariable, $property);
59+
$output = $this->getSubAssignVariableStatement($context['output'], $property);
5760
$types = $this->propertyInfoExtractor->getTypes($object, $property, $context);
5861

5962
// If no type can be extracted, directly assign output to input
@@ -86,8 +89,6 @@ public function generate($object, array $context = [])
8689
}
8790
}
8891

89-
$statements[] = new Stmt\Return_($dataVariable);
90-
9192
return $statements;
9293
}
9394

src/Symfony/Component/AstGenerator/Normalizer/NormalizerGenerator.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ protected function createNormalizeMethod($class, array $context = [])
172172
*/
173173
protected function createDenormalizeMethod($class, array $context = [])
174174
{
175+
$input = new Expr\Variable('data');
176+
$output = new Expr\Variable('output');
177+
175178
return new Stmt\ClassMethod('denormalize', [
176179
'type' => Stmt\Class_::MODIFIER_PUBLIC,
177180
'params' => [
@@ -180,9 +183,12 @@ protected function createDenormalizeMethod($class, array $context = [])
180183
new Param('format', new Expr\ConstFetch(new Name("null"))),
181184
new Param('context', new Expr\Array_(), 'array'),
182185
],
183-
'stmts' => $this->denormalizeStatementsGenerator->generate($class, array_merge($context, [
184-
'input' => new Expr\Variable('data')
185-
]))
186+
'stmts' => array_merge($this->denormalizeStatementsGenerator->generate($class, array_merge($context, [
187+
'input' => $input,
188+
'output' => $output
189+
])), [
190+
new Stmt\Return_($output)
191+
])
186192
]);
187193
}
188194

src/Symfony/Component/AstGenerator/Tests/Hydrate/ArrayHydrateGeneratorTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ public function testHydrateGenerator()
4545
$dummyObject = new Dummy();
4646
$dummyObject->foo = "test";
4747

48-
$dummyArray = eval($this->printer->prettyPrint($hydrateGenerator->generate(Dummy::class, [
49-
'input' => new Expr\Variable('dummyObject')
48+
eval($this->printer->prettyPrint($hydrateGenerator->generate(Dummy::class, [
49+
'input' => new Expr\Variable('dummyObject'),
50+
'output' => new Expr\Variable('dummyArray')
5051
])));
5152

5253
$this->assertInternalType('array', $dummyArray);
@@ -63,6 +64,16 @@ public function testNoInput()
6364
$hydrateGenerator = new ArrayHydrateGenerator($propertyInfoExtractor->reveal(), new DummyTypeGenerator());
6465
$hydrateGenerator->generate(Dummy::class);
6566
}
67+
68+
/**
69+
* @expectedException \Symfony\Component\AstGenerator\Exception\MissingContextException
70+
*/
71+
public function testNoOutput()
72+
{
73+
$propertyInfoExtractor = $this->prophesize(PropertyInfoExtractorInterface::class);
74+
$hydrateGenerator = new ArrayHydrateGenerator($propertyInfoExtractor->reveal(), new DummyTypeGenerator());
75+
$hydrateGenerator->generate(Dummy::class, ['input' => new Expr\Variable("test")]);
76+
}
6677
}
6778

6879
class Dummy

src/Symfony/Component/AstGenerator/Tests/Hydrate/StdClassHydrateGeneratorTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ public function testHydrateGenerator()
4545
$fooObject = new Foo();
4646
$fooObject->foo = "test";
4747

48-
$dummyStdClass = eval($this->printer->prettyPrint($hydrateGenerator->generate(Foo::class, [
49-
'input' => new Expr\Variable('fooObject')
48+
eval($this->printer->prettyPrint($hydrateGenerator->generate(Foo::class, [
49+
'input' => new Expr\Variable('fooObject'),
50+
'output' => new Expr\Variable('dummyStdClass')
5051
])));
5152

5253
$this->assertInstanceOf('stdClass', $dummyStdClass);
@@ -63,6 +64,16 @@ public function testNoInput()
6364
$hydrateGenerator = new StdClassHydrateGenerator($propertyInfoExtractor->reveal(), new FooTypeGenerator());
6465
$hydrateGenerator->generate(Foo::class);
6566
}
67+
68+
/**
69+
* @expectedException \Symfony\Component\AstGenerator\Exception\MissingContextException
70+
*/
71+
public function testNoOutput()
72+
{
73+
$propertyInfoExtractor = $this->prophesize(PropertyInfoExtractorInterface::class);
74+
$hydrateGenerator = new StdClassHydrateGenerator($propertyInfoExtractor->reveal(), new FooTypeGenerator());
75+
$hydrateGenerator->generate(Foo::class, ['input' => new Expr\Variable("test")]);
76+
}
6677
}
6778

6879
class Foo

0 commit comments

Comments
 (0)
0