Skip to content

Commit 759432c

Browse files
committed
Feature 34483 - Taking into account code review.
1 parent 79b2fa1 commit 759432c

File tree

4 files changed

+20
-26
lines changed

4 files changed

+20
-26
lines changed

src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationPath.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,8 @@ public function isIndex(int $index)
196196
/**
197197
* {@inheritdoc}
198198
*/
199-
public function isOptional(int $index)
199+
public function isNullSafe(int $index)
200200
{
201-
// Nothing is optional
202201
return false;
203202
}
204203

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,12 @@ private function readPropertiesUntil(array $zval, PropertyPathInterface $propert
279279
for ($i = 0; $i < $lastIndex; ++$i) {
280280
$property = $propertyPath->getElement($i);
281281
$isIndex = $propertyPath->isIndex($i);
282-
$isOptional = $propertyPath->isOptional($i);
282+
283+
$isOptional = false;
284+
if (method_exists($propertyPath, 'isNullSafe')) {
285+
// To be removed in symfony 6 once we are sure isOptional is always implemented.
286+
$isOptional = $propertyPath->isNullSafe($i);
287+
}
283288

284289
if ($isIndex) {
285290
// Create missing nested arrays on demand
@@ -375,11 +380,10 @@ private function readProperty(array $zval, string $property, bool $ignoreInvalid
375380
if (!\is_object($zval[self::VALUE])) {
376381
if (!$isOptional) {
377382
throw new NoSuchPropertyException(sprintf('Cannot read property "%s" from an array. Maybe you intended to write the property path as "[%1$s]" instead.', $property));
378-
} else {
379-
$result[self::VALUE] = null;
380-
381-
return $result;
382383
}
384+
385+
$result[self::VALUE] = null;
386+
return $result;
383387
}
384388

385389
$object = $zval[self::VALUE];

src/Symfony/Component/PropertyAccess/PropertyPath.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class PropertyPath implements \IteratorAggregate, PropertyPathInterface
5555
*
5656
* @var array
5757
*/
58-
private $isOptional = [];
58+
private $isNullSafe = [];
5959

6060
/**
6161
* String representation of the path.
@@ -80,7 +80,7 @@ public function __construct($propertyPath)
8080
$this->elements = $propertyPath->elements;
8181
$this->length = $propertyPath->length;
8282
$this->isIndex = $propertyPath->isIndex;
83-
$this->isOptional = $propertyPath->isOptional;
83+
$this->isNullSafe = $propertyPath->isNullSafe;
8484
$this->pathAsString = $propertyPath->pathAsString;
8585

8686
return;
@@ -111,10 +111,10 @@ public function __construct($propertyPath)
111111

112112
// Mark as optional when last character is "?".
113113
if ('?' === substr($element, -1, 1)) {
114-
$this->isOptional[] = true;
114+
$this->isNullSafe[] = true;
115115
$element = substr($element, 0, -1);
116116
} else {
117-
$this->isOptional[] = false;
117+
$this->isNullSafe[] = false;
118118
}
119119
$this->elements[] = $element;
120120

@@ -161,7 +161,7 @@ public function getParent()
161161
$parent->pathAsString = substr($parent->pathAsString, 0, max(strrpos($parent->pathAsString, '.'), strrpos($parent->pathAsString, '[')));
162162
array_pop($parent->elements);
163163
array_pop($parent->isIndex);
164-
array_pop($parent->isOptional);
164+
array_pop($parent->isNullSafe);
165165

166166
return $parent;
167167
}
@@ -223,12 +223,12 @@ public function isIndex(int $index)
223223
/**
224224
* {@inheritdoc}
225225
*/
226-
public function isOptional(int $index)
226+
public function isNullSafe(int $index)
227227
{
228-
if (!isset($this->isOptional[$index])) {
228+
if (!isset($this->isNullSafe[$index])) {
229229
throw new OutOfBoundsException(sprintf('The index %s is not within the property path', $index));
230230
}
231231

232-
return $this->isOptional[$index];
232+
return $this->isNullSafe[$index];
233233
}
234234
}

src/Symfony/Component/PropertyAccess/PropertyPathInterface.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* A sequence of property names or array indices.
1616
*
1717
* @author Bernhard Schussek <bschussek@gmail.com>
18+
*
19+
* @method bool isNullSafe(int $index) Returns whether the element at the given index is null sage. Not implementing it is deprecated since Symfony 5.2
1820
*/
1921
interface PropertyPathInterface extends \Traversable
2022
{
@@ -83,15 +85,4 @@ public function isProperty(int $index);
8385
* @throws Exception\OutOfBoundsException If the offset is invalid
8486
*/
8587
public function isIndex(int $index);
86-
87-
/**
88-
* Returns whether the element at the given index is optional.
89-
*
90-
* @param int $index The index in the property path
91-
*
92-
* @return bool Whether the element at this index is an array index
93-
*
94-
* @throws Exception\OutOfBoundsException If the offset is invalid
95-
*/
96-
public function isOptional(int $index);
9788
}

0 commit comments

Comments
 (0)