8000 [AstGenerator] [WIP] New Component, add normalizer generator by joelwurtz · Pull Request #17516 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[AstGenerator] [WIP] New Component, add normalizer generator #17516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Prev Previous commit
Use long array syntax
  • Loading branch information
GuilhemN authored and joelwurtz committed Aug 5, 2016
commit 8e636a098371584a1a49a81530036ee6cb6eeaff
6 changes: 3 additions & 3 deletions src/Symfony/Component/AstGenerator/AstGeneratorChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AstGeneratorChain implements AstGeneratorInterface
/** @var bool Whether the generation must return as soon as possible or use all generators, default to false */
protected $returnOnFirst;

public function __construct(array $generators = [], $returnOnFirst = false)
public function __construct(array $generators = array(), $returnOnFirst = false)
{
$this->generators = $generators;
$this->returnOnFirst = $returnOnFirst;
Expand All @@ -33,9 +33,9 @@ public function __construct(array $generators = [], $returnOnFirst = false)
/**
* {@inheritdoc}
*/
public function generate($object, array $context = [])
public function generate($object, array $context = array())
{
$nodes = [];
$nodes = array();

foreach ($this->generators as $generator) {
if ($generator instanceof AstGeneratorInterface && $generator->supportsGeneration($object)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The $generator instanceof AstGeneratorInterface test should be moved in the constructor to avoid doing it at every call to generate()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface AstGeneratorInterface
*
* @return \PhpParser\Node[] An array of statements (AST Node)
*/
public function generate($object, array $context = []);
public function generate($object, array $context = array());

/**
* Check whether the given object is supported for generation by this generator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(PropertyInfoExtractorInterface $propertyInfoExtracto
/**
* {@inheritdoc}
*/
public function generate($object, array $context = [])
public function generate($object, array $context = array())
{
if (!isset($context['input']) || !($context['input'] instanceof Expr\Variable)) {
throw new MissingContextException('Input variable not defined or not a Expr\Variable in generation context');
Expand All @@ -49,7 +49,7 @@ public function generate($object, array $context = [])
throw new MissingContextException('Output variable not defined or not a Expr\Variable in generation context');
}

$statements = [$this->getAssignStatement($context['output'])];
$statements = array($this->getAssignStatement($context['output']));

foreach ($this->propertyInfoExtractor->getProperties($object, $context) as $property) {
// Only normalize readable property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(PropertyInfoExtractorInterface $propertyInfoExtracto
/**
* {@inheritdoc}
*/
public function generate($object, array $context = [])
public function generate($object, array $context = array())
{
if (!isset($context['input']) || !($context['input'] instanceof Expr\Variable)) {
throw new MissingContextException('Input variable not defined or not a Expr\Variable in generation context');
Expand All @@ -52,9 +52,9 @@ public function generate($object, array $context = [])
}

$uniqueVariableScope = isset($context['unique_variable_scope']) ? $context['unique_variable_scope'] : new UniqueVariableScope();
$statements = [
$statements = array(
new Expr\Assign($context['output'], new Expr\New_(new Name("\\".$object))),
];
);

foreach ($this->propertyInfoExtractor->getProperties($object, $context) as $property) {
// Only hydrate writable property
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

properties

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function __construct(
*
* @param Type $object A type extracted with PropertyInfo component
*/
public function generate($object, array $context = [])
public function generate($object, array $context = array())
{
if (!isset($context['input']) || !($context['input'] instanceof Expr)) {
throw new MissingContextException('Input variable not defined or not an Expr in generation context');
Expand All @@ -82,9 +82,9 @@ public function generate($object, array $context = [])
}

$uniqueVariableScope = isset($context['unique_variable_scope']) ? $context['unique_variable_scope'] : new UniqueVariableScope();
$statements = [
$statements = array(
new Expr\Assign($context['output'], $this->createCollectionAssignStatement($object)),
];
);

// Create item input
$loopValueVar = new Expr\Variable($uniqueVariableScope->getUniqueName('value'));
Expand All @@ -94,19 +94,19 @@ public function generate($object, array $context = [])
$output = $this->createCollectionItemExpr($object, $loopKeyVar, $context['output']);

// Loop statements
$loopStatements = [new Expr\Assign($output, $loopValueVar)];
$loopStatements = array(new Expr\Assign($output, $loopValueVar));

if (null !== $object->getCollectionValueType() && $this->subValueTypeGenerator->supportsGeneration($object->getCollectionValueType())) {
$loopStatements = $this->subValueTypeGenerator->generate($object->getCollectionValueType(), array_merge($context, [
$loopStatements = $this->subValueTypeGenerator->generate($object->getCollectionValueType(), array_merge($context, array(
'input' => $loopValueVar,
'output' => $output
]));
)));
}

$statements[] = new Stmt\Foreach_($context['input'], $loopValueVar, [
$statements[] = new Stmt\Foreach_($context['input'], $loopValueVar, array(
'keyVar' => $loopKeyVar,
'stmts' => $loopStatements
]);
));

return $statements;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DenormalizableObjectTypeGenerator implements AstGeneratorInterface
*
* @param Type $object A type extracted with PropertyInfo component
*/
public function generate($object, array $context = [])
public function generate($object, array $context = array())
{
if (!isset($context['input']) || !($context['input'] instanceof Expr)) {
throw new MissingContextException('Input variable not defined or not an Expr in generation context');
Expand Down Expand Up @@ -68,18 +68,18 @@ public function generate($object, array $context = [])
];

if (isset($context['condition']) && $context['condition']) {
return [new Stmt\If_(
return array(new Stmt\If_(
new Expr\BinaryOp\LogicalAnd(
new Expr\MethodCall(
$context['denormalizer'],
'supportsDenormalization',
$normalizationArgs
)
),
[
array(
'stmts' => $assign
]
)];
)
));
}

return $assign;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class NormalizableObjectTypeGenerator implements AstGeneratorInterface
*
* @param Type $object A type extracted with PropertyInfo component
*/
public function generate($object, array $context = [])
public function generate($object, array $context = array())
{
if (!isset($context['input']) || !($context['input'] instanceof Expr)) {
throw new MissingContextException('Input variable not defined or not an Expr in generation context');
Expand All @@ -55,16 +55,16 @@ public function generate($object, array $context = [])
$normalizationArgs[] = new Arg($context['context']);
}

$assign = [
$assign = array(
new Expr\Assign($context['output'], new Expr\MethodCall(
$context['normalizer'],
'normalize',
$normalizationArgs
))
];
);

if (isset($context['condition']) && $context['condition']) {
return [new Stmt\If_(
return array(new Stmt\If_(
new Expr\BinaryOp\LogicalAnd(
new Expr\Instanceof_(new Expr\Variable('data'), new Name($object->getClassName())),
new Expr\MethodCall(
Expand All @@ -73,10 +73,10 @@ public function generate($object, array $context = [])
$normalizationArgs
)
),
[
array(
'stmts' => $assign
]
)];
)
));
}

return $assign;
Expand Down
26 changes: 13 additions & 13 deletions src/Symfony/Component/AstGenerator/Hydrate/Type/TypeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,28 @@
*/
class TypeGenerator implements AstGeneratorInterface
{
protected $supportedTypes = [
protected $supportedTypes = array(
Type::BUILTIN_TYPE_BOOL,
Type::BUILTIN_TYPE_FLOAT,
Type::BUILTIN_TYPE_INT,
Type::BUILTIN_TYPE_NULL,
Type::BUILTIN_TYPE_STRING,
];
);

protected $conditionMapping = [
protected $conditionMapping = array(
Type::BUILTIN_TYPE_BOOL => 'is_bool',
Type::BUILTIN_TYPE_FLOAT => 'is_float',
Type::BUILTIN_TYPE_INT => 'is_int',
Type::BUILTIN_TYPE_NULL => 'is_null',
Type::BUILTIN_TYPE_STRING => 'is_string',
];
);

/**
* {@inheritdoc}
*
* @param Type $object A type extracted with PropertyInfo component
*/
public function generate($object, array $context = [])
public function generate($object, array $context = array())
{
if (!isset($context['input']) || !($context['input'] instanceof Expr)) {
throw new MissingContextException('Input variable not defined or not an Expr in generation context');
Expand All @@ -57,22 +57,22 @@ public function generate($object, array $context = [])
throw new MissingContextException('Output variable not defined or not an Expr in generation context');
}

$assign = [
$assign = array(
new Expr\Assign($context['output'], $context['input'])
];
);

if (isset($context['condition']) && $context['condition']) {
return [new Stmt\If_(
return array(new Stmt\If_(
new Expr\FuncCall(
new Name($this->conditionMapping[$object->getBuiltinType()]),
[
array(
new Arg($context['input'])
]
)
),
[
array(
'stmts' => $assign
]
)];
)
));
}

return $assign;
Expand Down
Loading
0