Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2f23d97

Browse files
committed
[Validator] Reduced number of method calls on the execution context
1 parent 73c9cc5 commit 2f23d97

File tree

4 files changed

+27
-72
lines changed

4 files changed

+27
-72
lines changed

src/Symfony/Component/Validator/Context/ExecutionContext.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,24 +139,10 @@ public function __construct(ValidatorInterface $validator, $root, TranslatorInte
139139
/**
140140
* {@inheritdoc}
141141
*/
142-
public function setValue($value)
142+
public function setNode($value, MetadataInterface $metadata = null, $propertyPath)
143143
{
144144
$this->value = $value;
145-
}
146-
147-
/**
148-
* {@inheritdoc}
149-
*/
150-
public function setMetadata(MetadataInterface $metadata = null)
151-
{
152145
$this->metadata = $metadata;
153-
}
154-
155-
/**
156-
* {@inheritdoc}
157-
*/
158-
public function setPropertyPath($propertyPath)
159-
{
160146
$this->propertyPath = (string) $propertyPath;
161147
}
162148

src/Symfony/Component/Validator/Context/ExecutionContextInterface.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -102,32 +102,14 @@ public function getValidator();
102102
/**
103103
* Sets the currently validated value.
104104
*
105-
* @param mixed $value The validated value
105+
* @param mixed $value The validated value
106+
* @param MetadataInterface $metadata The validation metadata
107+
* @param string $propertyPath The property path to the current value
106108
*
107109
* @internal Used by the validator engine. Should not be called by user
108110
* code.
109111
*/
110-
public function setValue($value);
111-
112-
/**
113-
* Sets the current validation metadata.
114-
*
115-
* @param MetadataInterface $metadata The validation metadata
116-
*
117-
* @internal Used by the validator engine. Should not be called by user
118-
* code.
119-
*/
120-
public function setMetadata(MetadataInterface $metadata = null);
121-
122-
/**
123-
* Sets the property path leading to the current value.
124-
*
125-
* @param string $propertyPath The property path to the current value
126-
*
127-
* @internal Used by the validator engine. Should not be called by user
128-
* code.
129-
*/
130-
public function setPropertyPath($propertyPath);
112+
public function setNode($value, MetadataInterface $metadata = null, $propertyPath);
131113

132114
/**
133115
* Sets the currently validated group.

src/Symfony/Component/Validator/NodeVisitor/NodeValidationVisitor.php

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ public function visit(Node $node, ExecutionContextInterface $context)
6969
return true;
7070
}
7171

72-
$context->setValue($node->value);
73-
$context->setMetadata($node->metadata);
74-
$context->setPropertyPath($node->propertyPath);
72+
$context->setNode($node->value, $node->metadata, $node->propertyPath);
7573

7674
if ($node instanceof ClassNode) {
7775
$this->replaceDefaultGroup($node);
@@ -171,43 +169,34 @@ private function traverseGroupSequence(Node $node, GroupSequence $groupSequence,
171169
*/
172170
private function validateNodeForGroup(Node $node, $group, ExecutionContextInterface $context, $objectHash)
173171
{
174-
try {
175-
$context->setGroup($group);
172+
$context->setGroup($group);
176173

177-
foreach ($node->metadata->findConstraints($group) as $constraint) {
178-
// Prevent duplicate validation of constraints, in the case
179-
// that constraints belong to multiple validated groups
180-
if (null !== $objectHash) {
181-
$constraintHash = spl_object_hash($constraint);
174+
foreach ($node->metadata->findConstraints($group) as $constraint) {
175+
// Prevent duplicate validation of constraints, in the case
176+
// that constraints belong to multiple validated groups
177+
if (null !== $objectHash) {
178+
$constraintHash = spl_object_hash($constraint);
182179

183-
if ($node instanceof ClassNode) {
184-
if ($context->isClassConstraintValidated($objectHash, $constraintHash)) {
185-
continue;
186-
}
187-
188-
$context->markClassConstraintAsValidated($objectHash, $constraintHash);
189-
} elseif ($node instanceof PropertyNode) {
190-
$propertyName = $node->metadata->getPropertyName();
180+
if ($node instanceof ClassNode) {
181+
if ($context->isClassConstraintValidated($objectHash, $constraintHash)) {
182+
continue;
183+
}
191184

192-
if ($context->isPropertyConstraintValidated($objectHash, $propertyName, $constraintHash)) {
193-
continue;
194-
}
185+
$context->markClassConstraintAsValidated($objectHash, $constraintHash);
186+
} elseif ($node instanceof PropertyNode) {
187+
$propertyName = $node->metadata->getPropertyName();
195188

196-
$context->markPropertyConstraintAsValidated($objectHash, $propertyName, $constraintHash);
189+
if ($context->isPropertyConstraintValidated($objectHash, $propertyName, $constraintHash)) {
190+
continue;
197191
}
198-
}
199192

200-
$validator = $this->validatorFactory->getInstance($constraint);
201-
$validator->initialize($context);
202-
$validator->validate($node->value, $constraint);
193+
$context->markPropertyConstraintAsValidated($objectHash, $propertyName, $constraintHash);
194+
}
203195
}
204196

205-
$context->setGroup(null);
206-
} catch (\Exception $e) {
207-
// Should be put into a finally block once we switch to PHP 5.5
208-
$context->setGroup(null);
209-
210-
throw $e;
197+
$validator = $this->validatorFactory->getInstance($constraint);
198+
$validator->initialize($context);
199+
$validator->validate($node->value, $constraint);
211200
}
212201
}
213202

src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,9 +581,7 @@ private function cascadeObject($container, $containerHash, $propertyPath, array
581581
*/
582582
public function validateNode($value, $valueHash, $container, $containerHash, MetadataInterface $metadata = null, $propertyPath, array $groups, $traversalStrategy, ExecutionContextInterface $context)
583583
{
584-
$context->setValue($value);
585-
$context->setMetadata($metadata);
586-
$context->setPropertyPath($propertyPath);
584+
$context->setNode($value, $metadata, $propertyPath);
587585

588586
// if group (=[<G1,G2>,G3,G4]) contains group sequence (=<G1,G2>)
589587
// then call traverse() with each entry of the group sequence and abort

0 commit comments

Comments
 (0)