8000 [Validator] deprecate member metadata accessors by Tobion · Pull Request #11703 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] deprecate member metadata accessors #11703

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

Merged
merged 2 commits into from
Aug 31, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
use Symfony\Component\Form\Guess\ValueGuess;
use Symfony\Component\Validator\MetadataFactoryInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
use Symfony\Component\Validator\MetadataFactoryInterface;

class ValidatorTypeGuesser implements FormTypeGuesserInterface
{
Expand Down Expand Up @@ -264,8 +265,8 @@ protected function guess($class, $property, \Closure $closure, $defaultValue = n
$guesses = array();
$classMetadata = $this->metadataFactory->getMetadataFor($class);

if ($classMetadata->hasMemberMetadatas($property)) {
$memberMetadatas = $classMetadata->getMemberMetadatas($property);
if ($classMetadata instanceof ClassMetadataInterface && $classMetadata->hasPropertyMetadata($property)) {
Copy link
Member

Choose a reason for hiding this comment

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

the metadata for a class should always be a ClassMetadataInterface so I don't think this check is necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's necessary because the interface only says it returns an MetadataInterface. Everything else is implementation detail we cannot trust.

$memberMetadatas = $classMetadata->getPropertyMetadata($property);

foreach ($memberMetadatas as $memberMetadata) {
$constraints = $memberMetadata->getConstraints();
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"symfony/property-access": "~2.3"
},
"require-dev": {
"symfony/validator": "~2.2",
"symfony/validator": "~2.3",
"symfony/http-foundation": "~2.2",
"symfony/http-kernel": "~2.4",
"symfony/security-csrf": "~2.4",
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ CHANGELOG
* [BC BREAK] `UserPasswordValidator` source message change
* [BC BREAK] added internal `ExecutionContextInterface::setConstraint()`
* added `ConstraintViolation::getConstraint()`
* deprecated `ClassMetadata::hasMemberMetadatas()`
* deprecated `ClassMetadata::getMemberMetadatas()`
* deprecated `ClassMetadata::addMemberMetadata()`
* [BC BREAK] added `Mapping\MetadataInterface::getConstraints()`

2.5.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class ExecutionContext implements ExecutionContextInterface
/**
* The current validation metadata.
*
* @var MetadataInterface
* @var MetadataInterface|null
*/
private $metadata;

Expand Down
42 changes: 27 additions & 15 deletions src/Symfony/Component/Validator/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function accept(ValidationVisitorInterface $visitor, $value, $group, $pro
$pathPrefix = empty($propertyPath) ? '' : $propertyPath.'.';

foreach ($this->getConstrainedProperties() as $property) {
foreach ($this->getMemberMetadatas($property) as $member) {
foreach ($this->getPropertyMetadata($property) as $member) {
$member->accept($visitor, $member->getPropertyValue($value), $group, $pathPrefix.$property, $propagatedGroup);
}
}
Expand Down Expand Up @@ -268,7 +268,7 @@ public function addPropertyConstraint($property, Constraint $constraint)
if (!isset($this->properties[$property])) {
$this->properties[$property] = new PropertyMetadata($this->getClassName(), $property);

$this->addMemberMetadata($this->properties[$property]);
$this->addPropertyMetadata($this->properties[$property]);
}

$constraint->addImplicitGroupName($this->getDefaultGroup());
Expand All @@ -294,7 +294,7 @@ public function addGetterConstraint($property, Constraint $constraint)
if (!isset($this->getters[$property])) {
$this->getters[$property] = new GetterMetadata($this->getClassName(), $property);

$this->addMemberMetadata($this->getters[$property]);
$this->addPropertyMetadata($this->getters[$property]);
}

$constraint->addImplicitGroupName($this->getDefaultGroup());
Expand All @@ -316,16 +316,16 @@ public function mergeConstraints(ClassMetadata $source)
}

foreach ($source->getConstrainedProperties() as $property) {
foreach ($source->getMemberMetadatas($property) as $member) {
foreach ($source->getPropertyMetadata($property) as $member) {
$member = clone $member;

foreach ($member->getConstraints() as $constraint) {
$constraint->addImplicitGroupName($this->getDefaultGroup());
}

$this->addMemberMetadata($member);
$this->addPropertyMetadata($member);

if (!$member->isPrivate($this->name)) {
if ($member instanceof MemberMetadata && !$member->isPrivate($this->name)) {
$property = $member->getPropertyName();

if ($member instanceof PropertyMetadata && !isset($this->properties[$property])) {
Expand All @@ -342,12 +342,12 @@ public function mergeConstraints(ClassMetadata $source)
* Adds a member metadata.
*
* @param MemberMetadata $metadata
*
* @deprecated Deprecated since version 2.6, to be removed in 3.0.
*/
prote 9E88 cted function addMemberMetadata(MemberMetadata $metadata)
{
$property = $metadata->getPropertyName();

$this->members[$property][] = $metadata;
$this->addPropertyMetadata($metadata);
}

/**
Expand All @@ -356,10 +356,12 @@ protected function addMemberMetadata(MemberMetadata $metadata)
* @param string $property The name of the property
*
* @return bool
*
* @deprecated Deprecated since version 2.6, to be removed in 3.0. Use {@link hasPropertyMetadata} instead.
*/
public function hasMemberMetadatas($property)
{
return array_key_exists($property, $this->members);
return $this->hasPropertyMetadata($property);
}

/**
Expand All @@ -368,14 +370,12 @@ public function hasMemberMetadatas($property)
* @param string $property The name of the property
*
* @return MemberMetadata[] An array of MemberMetadata
*
* @deprecated Deprecated since version 2.6, to be removed in 3.0. Use {@link getPropertyMetadata} instead.
*/
public function getMemberMetadatas($property)
{
if (!isset($this->members[$property])) {
return array();
}

return $this->members[$property];
return $this->getPropertyMetadata($property);
}

/**
Expand Down Expand Up @@ -505,4 +505,16 @@ public function getCascadingStrategy()
{
return CascadingStrategy::NONE;
}

/**
* Adds a property metadata.
*
* @param PropertyMetadataInterface $metadata
*/
private function addPropertyMetadata(PropertyMetadataInterface $metadata)
{
$property = $metadata->getPropertyName();

$this->members[$property][] = $metadata;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function __construct(LoaderInterface $loader = null, CacheInterface $cach
}

/**
* Returns the metadata for the given class name or object.
* {@inheritdoc}
*
* If the method was called with the same class name (or an object of that
* class) before, the same metadata instance is returned.
Expand All @@ -87,12 +87,6 @@ public function __construct(LoaderInterface $loader = null, CacheInterface $cach
* configured with a loader, the metadata is passed to the
* {@link LoaderInterface::loadClassMetadata()} method for further
* configuration. At last, the new object is returned.
*
* @param string|object $value A class name or an object
*
* @return MetadataInterface The metadata for the value
*
* @throws NoSuchMetadataException If no metadata exists for the given value
*/
public function getMetadataFor($value)
{
Expand Down Expand Up @@ -141,12 +135,7 @@ public function getMetadataFor($value)
}

/**
* Returns whether the factory is able to return metadata for the given
* class name or object.
*
* @param string|object $value A class name or an object
*
* @return bool Whether metadata can be returned for that class
* {@inheritdoc}
*/
public function hasMetadataFor($value)
{
Expand Down
11 changes: 3 additions & 8 deletions src/Symfony/Component/Validator/Mapping/GenericMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@ public function addConstraints(array $constraints)
}

/**
* Returns all constraints of this element.
*
* @return Constraint[] A list of Constraint instances
* {@inheritdoc}
*/
public function getConstraints()
{
Expand All @@ -200,12 +198,9 @@ public function hasConstraints()
}

/**
* Returns the constraints of the given group and global ones (* group).
*
* @param string $group The group name
* {@inheritdoc}
*
* @return Constraint[] An list of all the Constraint instances belonging
* to the group
* Aware of the global group (* group).
*/
public function findConstraints($group)
{
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/Validator/Mapping/MetadataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@ public function getCascadingStrategy();
* @see TraversalStrategy
*/
public function getTraversalStrategy();

/**
* Returns all constraints of this element.
*
* @return Constraint[] A list of Constraint instances
*/
public function getConstraints();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class FakeClassMetadata extends ClassMetadata
{
public function addPropertyMetadata($propertyName, $metadata)
public function addCustomPropertyMetadata($propertyName, $metadata)
{
if (!isset($this->members[$propertyName])) {
$this->members[$propertyName] = array();
Expand Down
F987 8 changes: 4 additions & 4 deletions src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function testMergeConstraintsMergesMemberConstraints()
))),
);

$members = $this->metadata->getMemberMetadatas('firstName');
$members = $this->metadata->getPropertyMetadata('firstName');

$this->assertCount(1, $members);
$this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
Expand All @@ -112,8 +112,8 @@ public function testMemberMetadatas()
{
$this->metadata->addPropertyConstraint('firstName', new ConstraintA());

$this->assertTrue($this->metadata->hasMemberMetadatas('firstName'));
$this->assertFalse($this->metadata->hasMemberMetadatas('non_existant_field'));
$this->assertTrue($this->metadata->hasPropertyMetadata('firstName'));
$this->assertFalse($this->metadata->hasPropertyMetadata('non_existant_field'));
}

public function testMergeConstraintsKeepsPrivateMembersSeparate()
Expand All @@ -138,7 +138,7 @@ public function testMergeConstraintsKeepsPrivateMembersSeparate()
))),
);

$members = $this->metadata->getMemberMetadatas('internal');
$members = $this->metadata->getPropertyMetadata('internal');

$this->assertCount(2, $members);
$this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ public function testPropertyMetadataMustImplementPropertyMetadataInterface()
// Legacy interface
$propertyMetadata = $this->getMock('Symfony\Component\Validator\MetadataInterface');
$metadata = new FakeClassMetadata(get_class($entity));
$metadata->addPropertyMetadata('firstName', $propertyMetadata);
$metadata->addCustomPropertyMetadata('firstName', $propertyMetadata);

$this->metadataFactory->addMetadata($metadata);

Expand Down
0