10000 minor #13742 [PropertyAccess] refactor type checks (Tobion) · symfony/symfony@7b9bc80 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7b9bc80

Browse files
committed
minor #13742 [PropertyAccess] refactor type checks (Tobion)
This PR was merged into the 2.3 branch. Discussion ---------- [PropertyAccess] refactor type checks | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | - #13735 for 2.3 Commits ------- 9cacecb [PropertyAccess] the property path constructor already implements the type check 4e11c07 [PropertyAccess] refactor type checks to remove duplicate logic
2 parents 19aa6dc + 9cacecb commit 7b9bc80

File tree

2 files changed

+33
-56
lines changed

2 files changed

+33
-56
lines changed

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,8 @@ public function __construct($magicCall = false)
4040
*/
4141
public function getValue($objectOrArray, $propertyPath)
4242
{
43-
if (is_string($propertyPath)) {
43+
if (!$propertyPath instanceof PropertyPathInterface) {
4444
$propertyPath = new PropertyPath($propertyPath);
45-
} elseif (!$propertyPath instanceof PropertyPathInterface) {
46-
throw new UnexpectedTypeException($propertyPath, 'string or Symfony\Component\PropertyAccess\PropertyPathInterface');
4745
}
4846

4947
$propertyValues = & $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength());
@@ -56,10 +54,8 @@ public function getValue($objectOrArray, $propertyPath)
5654
*/
5755
public function setValue(&$objectOrArray, $propertyPath, $value)
5856
{
59-
if (is_string($propertyPath)) {
57+
if (!$propertyPath instanceof PropertyPathInterface) {
6058
$propertyPath = new PropertyPath($propertyPath);
61-
} elseif (!$propertyPath instanceof PropertyPathInterface) {
62-
throw new UnexpectedTypeException($propertyPath, 'string or Symfony\Component\PropertyAccess\PropertyPathInterface');
6359
}
6460

6561
$propertyValues = & $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength() - 1);
@@ -75,10 +71,6 @@ public function setValue(&$objectOrArray, $propertyPath, $value)
7571
$objectOrArray = & $propertyValues[$i][self::VALUE];
7672

7773
if ($overwrite) {
78-
if (!is_object($objectOrArray) && !is_array($objectOrArray)) {
79-
throw new UnexpectedTypeException($objectOrArray, 'object or array');
80-
}
81-
8274
$property = $propertyPath->getElement($i);
8375
//$singular = $propertyPath->singulars[$i];
8476
$singular = null;
@@ -108,13 +100,13 @@ public function setValue(&$objectOrArray, $propertyPath, $value)
108100
*/
109101
private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $propertyPath, $lastIndex)
110102
{
103+
if (!is_object($objectOrArray) && !is_array($objectOrArray)) {
104+
throw new UnexpectedTypeException($objectOrArray, 'object or array');
105+
}
106+
111107
$propertyValues = array();
112108

113109
for ($i = 0; $i < $lastIndex; ++$i) {
114-
if (!is_object($objectOrArray) && !is_array($objectOrArray)) {
115-
throw new UnexpectedTypeException($objectOrArray, 'object or array');
116-
}
117-
118110
$property = $propertyPath->getElement($i);
119111
$isIndex = $propertyPath->isIndex($i);
120112

@@ -137,6 +129,11 @@ private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $pr
137129

138130
$objectOrArray = & $propertyValue[self::VALUE];
139131

132+
// the final value of the path must not be validated
133+
if ($i + 1 < $propertyPath->getLength() && !is_object($objectOrArray) && !is_array($objectOrArray)) {
134+
throw new UnexpectedTypeException($objectOrArray, 'object or array');
135+
}
136+
140137
$propertyValues[] = & $propertyValue;
141138
}
142139

src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ protected function setUp()
2929
$this->propertyAccessor = new PropertyAccessor();
3030
}
3131

32+
public function getPathsWithUnexpectedType()
33+
{
34+
return array(
35+
array('', 'foobar'),
36+
array('foo', 'foobar'),
37+
array(null, 'foobar'),
38+
array(123, 'foobar'),
39+
array((object) array('prop' => null), 'prop.foobar'),
40+
array((object) array('prop' => (object) array('subProp' => null)), 'prop.subProp.foobar'),
41+
array(array('index' => null), '[index][foobar]'),
42+
array(array('index' => array('subIndex' => null)), '[index][subIndex][foobar]'),
43+
);
44+
}
45+
3246
public function testGetValueReadsArray()
3347
{
3448
$array = array('firstName' => 'Bernhard');
@@ -198,27 +212,13 @@ public function testGetValueThrowsExceptionIfPropertyDoesNotExist()
198212
}
199213

200214
/**
215+
* @dataProvider getPathsWithUnexpectedType
201216
* @expectedException \Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException
217+
* @expectedExceptionMessage Expected argument of type "object or array"
202218
*/
203-
public function testGetValueThrowsExceptionIfNotObjectOrArray()
204-
{
205-
$this->propertyAccessor->getValue('baz', 'foobar');
206-
}
207-
208-
/**
209-
* @expectedException \Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException
210-
*/
211-
public function testGetValueThrowsExceptionIfNull()
219+
public function testGetValueThrowsExceptionIfNotObjectOrArray($objectOrArray, $path)
212220
{
213-
$this->propertyAccessor->getValue(null, 'foobar');
214-
}
215-
216-
/**
217-
* @expectedException \Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException
218-
*/
219-
public function testGetValueThrowsExceptionIfEmpty()
220-
{
221-
$this->propertyAccessor->getValue('', 'foobar');
221+
$this->propertyAccessor->getValue($objectOrArray, $path);
222222
}
223223

224224
public function testGetValueWhenArrayValueIsNull()
@@ -311,33 +311,13 @@ public function testSetValueThrowsExceptionIfGetterIsNotPublic()
311311
}
312312

313313
/**
314+
* @dataProvider getPathsWithUnexpectedType
314315
* @expectedException \Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException
316+
* @expectedExceptionMessage Expected argument of type "object or array"
315317
*/
316-
public function testSetValueThrowsExceptionIfNotObjectOrArray()
318+
public function testSetValueThrowsExceptionIfNotObjectOrArray($objectOrArray, $path)
317319
{
318-
$value = 'baz';
319-
320-
$this->propertyAccessor->setValue($value, 'foobar', 'bam');
321-
}
322-
323-
/**
324-
* @expectedException \Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException
325-
*/
326-
public function testSetValueThrowsExceptionIfNull()
327-
{
328-
$value = null;
329-
330-
$this->propertyAccessor->setValue($value, 'foobar', 'bam');
331-
}
332-
333-
/**
334-
* @expectedException \Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException
335-
*/
336-
public function testSetValueThrowsExceptionIfEmpty()
337-
{
338-
$value = '';
339-
340-
$this->propertyAccessor->setValue($value, 'foobar', 'bam');
320+
$this->propertyAccessor->setValue($objectOrArray, $path, 'value');
341321
}
342322

343323
/**

0 commit comments

Comments
 (0)
0