8000 Merge branch '5.0' into 5.1 · phpfour/symfony@8ecd0c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8ecd0c9

Browse files
Merge branch '5.0' into 5.1
* 5.0: cs fix add german translation of the html5 color validation Fix PHP 8 deprecations ensure compatibility with PHP 8 stack traces clean up HHVM instructions sync translations from master branch silently ignore uninitialized properties when mapping data to forms
2 parents 2b18acc + 6e59119 commit 8ecd0c9

File tree

5 files changed

+88
-10
lines changed

5 files changed

+88
-10
lines changed

src/Symfony/Component/Config/Resource/ClassExistenceResource.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,17 @@ public static function throwOnRequiredClass(string $class, \Exception $previous
190190
'args' => [$class],
191191
];
192192

193-
if (false === $i = array_search($autoloadFrame, $trace, true)) {
193+
if (\PHP_VERSION_ID >= 80000 && isset($trace[1])) {
194+
$callerFrame = $trace[1];
195+
$i = 2;
196+
} elseif (false !== $i = array_search($autoloadFrame, $trace, true)) {
197+
$callerFrame = $trace[++$i];
198+
} else {
194199
throw $e;
195200
}
196201

197-
if (isset($trace[++$i]['function']) && !isset($trace[$i]['class'])) {
198-
switch ($trace[$i]['function']) {
202+
if (isset($callerFrame['function']) && !isset($callerFrame['class'])) {
203+
switch ($callerFrame['function']) {
199204
case 'get_class_methods':
200205
case 'get_class_vars':
201206
case 'get_parent_class':
@@ -214,8 +219,8 @@ public static function throwOnRequiredClass(string $class, \Exception $previous
214219
}
215220

216221
$props = [
217-
'file' => isset($trace[$i]['file']) ? $trace[$i]['file'] : null,
218-
'line' => isset($trace[$i]['line']) ? $trace[$i]['line'] : null,
222+
'file' => isset($callerFrame['file']) ? $callerFrame['file'] : null,
223+
'line' => isset($callerFrame['line']) ? $callerFrame['line'] : null,
219224
'trace' => \array_slice($trace, 1 + $i),
220225
];
221226

src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Symfony\Component\Form\DataMapperInterface;
1515
use Symfony\Component\Form\Exception\UnexpectedTypeException;
16+
use Symfony\Component\PropertyAccess\Exception\AccessException;
17+
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
1618
use Symfony\Component\PropertyAccess\PropertyAccess;
1719
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
1820

@@ -46,7 +48,7 @@ public function mapDataToForms($data, iterable $forms)
4648
$config = $form->getConfig();
4749

4850
if (!$empty && null !== $propertyPath && $config->getMapped()) {
49-
$form->setData($this->propertyAccessor->getValue($data, $propertyPath));
51+
$form->setData($this->getPropertyValue($data, $propertyPath));
5052
} else {
5153
$form->setData($config->getData());
5254
}
@@ -76,16 +78,32 @@ public function mapFormsToData(iterable $forms, &$data)
7678
$propertyValue = $form->getData();
7779
// If the field is of type DateTimeInterface and the data is the same skip the update to
7880
// keep the original object hash
79-
if ($propertyValue instanceof \DateTimeInterface && $propertyValue == $this->propertyAccessor->getValue($data, $propertyPath)) {
81+
if ($propertyValue instanceof \DateTimeInterface && $propertyValue == $this->getPropertyValue($data, $propertyPath)) {
8082
continue;
8183
}
8284

8385
// If the data is identical to the value in $data, we are
8486
// dealing with a reference
85-
if (!\is_object($data) || !$config->getByReference() || $propertyValue !== $this->propertyAccessor->getValue($data, $propertyPath)) {
87+
if (!\is_object($data) || !$config->getByReference() || $propertyValue !== $this->getPropertyValue($data, $propertyPath)) {
8688
$this->propertyAccessor->setValue($data, $propertyPath, $propertyValue);
8789
}
8890
}
8991
}
9092
}
93+
94+
private function getPropertyValue($data, $propertyPath)
95+
{
96+
try {
97+
return $this->propertyAccessor->getValue($data, $propertyPath);
98+
} catch (AccessException $e) {
99+
if (!$e instanceof UninitializedPropertyException
100+
// For versions without UninitializedPropertyException check the exception message
101+
&& (class_exists(UninitializedPropertyException::class) || false === strpos($e->getMessage(), 'You should initialize it'))
102+
) {
103+
throw $e;
104+
}
105+
106+
return null;
107+
}
108+
}
91109
}

src/Symfony/Component/Form/Resources/translations/validators.de.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
<source>The CSRF token is invalid. Please try to resubmit the form.</source>
1515
<target>Der CSRF-Token ist ungültig. Versuchen Sie bitte das Formular erneut zu senden.</target>
1616
</trans-unit>
17+
<trans-unit id="99">
18+
<source>This value is not a valid HTML5 color.</source>
19+
<target>Dieser Wert ist keine gültige HTML5 Farbe.</target>
20+
</trans-unit>
1721
</body>
1822
</file>
1923
</xliff>

src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
1818
use Symfony\Component\Form\Form;
1919
use Symfony\Component\Form\FormConfigBuilder;
20+
use Symfony\Component\Form\Tests\Fixtures\TypehintedPropertiesCar;
2021
use Symfony\Component\PropertyAccess\PropertyAccess;
2122
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
2223
use Symfony\Component\PropertyAccess\PropertyPath;
@@ -113,6 +114,23 @@ public function testMapDataToFormsIgnoresUnmapped()
113114
$this->assertNull($form->getData());
114115
}
115116

117+
/**
118+
* @requires PHP 7.4
119+
*/
120+
public function testMapDataToFormsIgnoresUninitializedProperties()
121+
{
122+
$engineForm = new Form(new FormConfigBuilder('engine', null, $this->dispatcher));
123+
$colorForm = new Form(new FormConfigBuilder('color', null, $this->dispatcher));
124+
125+
$car = new TypehintedPropertiesCar();
126+
$car->engine = 'BMW';
127+
128+
$this->mapper->mapDataToForms($car, [$engineForm, $colorForm]);
129+
130+
$this->assertSame($car->engine, $engineForm->getData());
131+
$this->assertNull($colorForm->getData());
132+
}
133+
116134
public function testMapDataToFormsSetsDefaultDataIfPassedDataIsNull()
117135
{
118136
$default = new \stdClass();
@@ -293,13 +311,28 @@ public function testMapFormsToDataIgnoresDisabled()
293311
$config->setPropertyPath($propertyPath);
294312
$config->setData($engine);
295313
$config->setDisabled(true);
296-
$form = new Form($config);
314+
$form = new SubmittedForm($config);
297315

298316
$this->mapper->mapFormsToData([$form], $car);
299317

300318
$this->assertSame($initialEngine, $car->engine);
301319
}
302320

321+
/**
322+
* @requires PHP 7.4
323+
*/
324+
public function testMapFormsToUninitializedProperties()
325+
{
326+
$car = new TypehintedPropertiesCar();
327+
$config = new FormConfigBuilder('engine', null, $this->dispatcher);
328+
$config->setData('BMW');
329+
$form = new SubmittedForm($config);
330+
331+
$this->mapper->mapFormsToData([$form], $car);
332+
333+
$this->assertSame('BMW', $car->engine);
334+
}
335+
303336
/**
304337
* @dataProvider provideDate
305338
*/
@@ -339,7 +372,7 @@ public function isSubmitted(): bool
339372
}
340373
}
341374

342-
class NotSynchronizedForm extends Form
375+
class NotSynchronizedForm extends SubmittedForm
343376
{
344377
public function isSynchronized(): bool
345378
{
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\Fixtures;
13+
14+
class TypehintedPropertiesCar
15+
{
16+
public ?string $engine;
17+
public ?string $color;
18+
}

0 commit comments

Comments
 (0)
0