8000 [PropertyAccess] array_keys does not work for ArrayAccess objects by sandermarechal · Pull Request #10705 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyAccess] array_keys does not work for ArrayAccess objects #10705

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

Closed
wants to merge 5 commits into from
Closed
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
13 changes: 12 additions & 1 deletion src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,18 @@ private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $pr
// Create missing nested arrays on demand
if ($isIndex && $isArrayAccess && !isset($objectOrArray[$property])) {
if (!$ignoreInvalidIndices) {
throw new NoSuchIndexException(sprintf('Cannot read property "%s". Available properties are "%s"', $property, print_r(array_keys($objectOrArray), true)));
$message = sprintf('Cannot read property "%s".', $property);
$keys = 'not available';
if (is_array($objectOrArray)) {
$keys = '"'.print_r(array_keys($objectOrArray), true).'"';
} elseif ($objectOrArray instanceof \Traversable) {
$list = array();
foreach ($objectOrArray as $key => &$value) {
$list[] = $key;
}
$keys = '"'.print_r($list, true).'"';
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd use implode() with ", "

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The original code also used print_r() so I left it that way.

}
throw new NoSuchIndexException(sprintf('Cannot read property "%s". Available properties are %s', $property, $keys));
}

$objectOrArray[$property] = $i + 1 < $propertyPath->getLength() ? array() : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\PropertyAccess\Tests;

use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyAccess\Tests\Fixtures\CustomArrayObject;

class PropertyAccessorCustomArrayObjectTest extends PropertyAccessorCollectionTest
Expand All @@ -19,4 +20,15 @@ protected function getCollection(array $array)
{
return new CustomArrayObject($array);
}

/**
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchIndexException
*/
public function testGetNoSuchIndex()
{
$arrayObject = new CustomArrayObject(array('foo', 'bar'));
$propertyAccessor = new PropertyAccessor(false, true);

$propertyAccessor->getValue($arrayObject, '[2]');
}
Copy link
Member

Choose a reason for hiding this comment

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

Can you also add a test for a Travesable object?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

CustomArrayObject already implements Traversable (through IteratorAggregate). Should I add a new fixture class that only implements ArrayAccess? And if so, should I run all collection tests on that new fixture (i.e. add a new test class as well)?

Copy link
Member

Choose a reason for hiding this comment

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

Having a test for ArrayAccess would be good. I think it's also a good idea to run all the tests for this new class.

}
0