8000 [Validator] Deprecate `ValidatorBuilder::enableAnnotationMapping()` a… · symfony/symfony@9fa9c32 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9fa9c32

Browse files
[Validator] Deprecate ValidatorBuilder::enableAnnotationMapping() and ValidatorBuilder::disableAnnotationMapping()
1 parent 9a0f178 commit 9fa9c32

File tree

6 files changed

+62
-15
lines changed

6 files changed

+62
-15
lines changed

UPGRADE-6.4.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,5 @@ Validator
157157
* Deprecate passing an annotation reader to the constructor signature of `AnnotationLoader`
158158
* Deprecate `ValidatorBuilder::setDoctrineAnnotationReader()`
159159
* Deprecate `ValidatorBuilder::addDefaultDoctrineAnnotationReader()`
160+
* Deprecate `ValidatorBuilder::enableAnnotationMapping()`, use `ValidatorBuilder::enableAttributeMapping()` instead
161+
* Deprecate `ValidatorBuilder::disableAnnotationMapping()`, use `ValidatorBuilder::disableAttributeMapping()` instead

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ CHANGELOG
1212
* Deprecate `ValidatorBuilder::addDefaultDoctrineAnnotationReader()`
1313
* Add `number`, `finite-number` and `finite-float` types to `Type` constraint
1414
* Add the `withSeconds` option to the `Time` constraint that allows to pass time without seconds
15+
* Deprecate `ValidatorBuilder::enableAnnotationMapping()`, use `ValidatorBuilder::enableAttributeMapping()` instead
16+
* Deprecate `ValidatorBuilder::disableAnnotationMapping()`, use `ValidatorBuilder::disableAttributeMapping()` instead
1517

1618
6.3
1719
---

src/Symfony/Component/Validator/Tests/Constraints/ValidValidatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ValidValidatorTest extends TestCase
2020
public function testPropertyPathsArePassedToNestedContexts()
2121
{
2222
$validatorBuilder = new ValidatorBuilder();
23-
$validator = $validatorBuilder->enableAnnotationMapping()->getValidator();
23+
$validator = $validatorBuilder->enableAttributeMapping()->getValidator();
2424

2525
$violations = $validator->validate(new Foo(), null, ['nested']);
2626

@@ -31,7 +31,7 @@ public function testPropertyPathsArePassedToNestedContexts()
3131
public function testNullValues()
3232
{
3333
$validatorBuilder = new ValidatorBuilder();
34-
$validator = $validatorBuilder->enableAnnotationMapping()->getValidator();
34+
$validator = $validatorBuilder->enableAttributeMapping()->getValidator();
3535

3636
$foo = new Foo();
3737
$foo->fooBar = null;

src/Symfony/Component/Validator/Tests/Mapping/Loader/PropertyInfoLoaderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function testLoadClassMetadata()
9292
$propertyInfoLoader = new PropertyInfoLoader($propertyInfoStub, $propertyInfoStub, $propertyInfoStub, '{.*}');
9393

9494
$validator = Validation::createValidatorBuilder()
95-
->enableAnnotationMapping()
95+
->enableAttributeMapping()
9696
->addLoader($propertyInfoLoader)
9797
->getValidator()
9898
;
@@ -230,7 +230,7 @@ public function testClassNoAutoMapping()
230230

231231
$propertyInfoLoader = new PropertyInfoLoader($propertyInfoStub, $propertyInfoStub, $propertyInfoStub, '{.*}');
232232
$validator = Validation::createValidatorBuilder()
233-
->enableAnnotationMapping()
233+
->enableAttributeMapping()
234234
->addLoader($propertyInfoLoader)
235235
->getValidator()
236236
;

src/Symfony/Component/Validator/Tests/ValidatorBuilderTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function testAddMethodMappings()
8181
*/
8282
public function testEnableAnnotationMappingWithDefaultDoctrineAnnotationReader()
8383
{
84-
$this->assertSame($this->builder, $this->builder->enableAnnotationMapping());
84+
$this->assertSame($this->builder, $this->builder->enableAttributeMapping());
8585

8686
$this->expectDeprecation('Since symfony/validator 6.4: Method "Symfony\Component\Validator\ValidatorBuilder::addDefaultDoctrineAnnotationReader()" is deprecated without replacement.');
8787
$this->assertSame($this->builder, $this->builder->addDefaultDoctrineAnnotationReader());
@@ -102,7 +102,7 @@ public function testEnableAnnotationMappingWithCustomDoctrineAnnotationReader()
102102
{
103103
$reader = $this->createMock(Reader::class);
104104

105-
$this->assertSame($this->builder, $this->builder->enableAnnotationMapping());
105+
$this->assertSame($this->builder, $this->builder->enableAttributeMapping());
106106

107107
$this->expectDeprecation('Since symfony/validator 6.4: Method "Symfony\Comp A3E2 onent\Validator\ValidatorBuilder::setDoctrineAnnotationReader()" is deprecated without replacement.');
108108
$this->assertSame($this->builder, $this->builder->setDoctrineAnnotationReader($reader));
@@ -116,9 +116,27 @@ public function testEnableAnnotationMappingWithCustomDoctrineAnnotationReader()
116116
$this->assertSame($reader, $r->getValue($loaders[0]));
117117
}
118118

119-
public function testDisableAnnotationMapping()
119+
/**
120+
* @group legacy
121+
*/
122+
public function testExpectDeprecationWhenEnablingAnnotationMapping()
123+
{
124+
$this->expectDeprecation('Since symfony/validator 6.4: Method "Symfony\Component\Validator\ValidatorBuilder::enableAnnotationMapping()" is deprecated, use "enableAttributeMapping()" instead.');
125+
$this->builder->enableAnnotationMapping();
126+
}
127+
128+
/**
129+
* @group legacy
130+
*/
131+
public function testExpectDeprecationWhenDisablingAnnotationMapping()
132+
{
133+
$this->expectDeprecation('Since symfony/validator 6.4: Method "Symfony\Component\Validator\ValidatorBuilder::disableAnnotationMapping()" is deprecated, use "disableAttributeMapping()" instead.');
134+
$this->builder->disableAnnotationMapping();
135+
}
136+
137+
public function testDisableAttributeMapping()
120138
{
121-
$this->assertSame($this->builder, $this->builder->disableAnnotationMapping());
139+
$this->assertSame($this->builder, $this->builder->disableAttributeMapping());
122140
}
123141

124142
public function testSetMappingCache()

src/Symfony/Component/Validator/ValidatorBuilder.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ValidatorBuilder
4949
private array $yamlMappings = [];
5050
private array $methodMappings = [];
5151
private ?Reader $annotationReader = null;
52-
private bool $enableAnnotationMapping = false;
52+
private bool $enableAttributeMapping = false;
5353
private ?MetadataFactoryInterface $metadataFactory = null;
5454
private ConstraintValidatorFactoryInterface $validatorFactory;
5555
private ?CacheItemPoolInterface $mappingCache = null;
@@ -187,31 +187,56 @@ public function addMethodMappings(array $methodNames): static
187187
}
188188

189189
/**
190-
* Enables annotation and attribute based constraint mapping.
190+
* @deprecated since Symfony 6.4, use "enableAttributeMapping()" instead.
191191
*
192192
* @return $this
193193
*/
194194
public function enableAnnotationMapping(): static
195+
{
196+
trigger_deprecation('symfony/validator', '6.4', 'Method "%s()" is deprecated, use "enableAttributeMapping()" instead.', __METHOD__);
197+
198+
return $this->enableAttributeMapping();
199+
}
200+
201+
/**
202+
* Enables attribute based constraint mapping.
203+
*
204+
* @return $this
205+
*/
206+
public function enableAttributeMapping(): static
195207
{
196208
if (null !== $this->metadataFactory) {
197209
throw new ValidatorException('You cannot enable annotation mapping after setting a custom metadata factory. Configure your metadata factory instead.');
198210
}
199211

200-
$this->enableAnnotationMapping = true;
212+
$this->enableAttributeMapping = true;
201213

202214
return $this;
203215
}
204216

205217
/**
206-
* Disables annotation and attribute based constraint mapping.
218+
* @deprecated since Symfony 6.4, use "disableAttributeMapping()" instead
207219
*
208220
* @return $this
209221
*/
210222
public function disableAnnotationMapping(): static
211223
{
212-
$this->enableAnnotationMapping = false;
224+
trigger_deprecation('symfony/validator', '6.4', 'Method "%s()" is deprecated, use "disableAttributeMapping()" instead.', __METHOD__);
225+
213226
$this->annotationReader = null;
214227

228+
return $this->disableAttributeMapping();
229+
}
230+
231+
/**
232+
* Disables attribute based constraint mapping.
233+
*
234+
* @return $this
235+
*/
236+
public function disableAttributeMapping(): static
237+
{
238+
$this->enableAttributeMapping = false;
239+
215240
return $this;
216241
}
217242

@@ -250,7 +275,7 @@ public function addDefaultDoctrineAnnotationReader(): static
250275
*/
251276
public function setMetadataFactory(MetadataFactoryInterface $metadataFactory): static
252277
{
253-
if (\count($this->xmlMappings) > 0 || \count($this->yamlMappings) > 0 || \count($this->methodMappings) > 0 || $this->enableAnnotationMapping) {
278+
if (\count($this->xmlMappings) > 0 || \count($this->yamlMappings) > 0 || \count($this->methodMappings) > 0 || $this->enableAttributeMapping) {
254279
throw new ValidatorException('You cannot set a custom metadata factory after adding custom mappings. You should do either of both.');
255280
}
256281

@@ -344,7 +369,7 @@ public function getLoaders(): array
344369
$loaders[] = new StaticMethodLoader($methodName);
345370
}
346371

347-
if ($this->enableAnnotationMapping) {
372+
if ($this->enableAttributeMapping) {
348373
$loaders[] = new AnnotationLoader($this->annotationReader);
349374
}
350375

0 commit comments

Comments
 (0)
0