8000 [WIP][Serializer]Use PropertyInfo to extract properties by fbourigault · Pull Request #28775 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[WIP][Serializer]Use PropertyInfo to extract properties #28775

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 8 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
5 changes: 5 additions & 0 deletions src/Symfony/Component/PropertyInfo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.3.0
-----

* added `exclude_static_properties` option to `ReflectionExtractor` to exclude public static properties when listing properties

4.2.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTypeExtractorInterface, PropertyAccessExtractorInterface, PropertyInitializableExtractorInterface
{
public const EXCLUDE_STATIC_PROPERTIES = 'exclude_static_properties';

/**
* @internal
*/
Expand Down Expand Up @@ -75,7 +77,7 @@ public function getProperties($class, array $context = array())

$properties = array();
foreach ($reflectionProperties as $reflectionProperty) {
if ($reflectionProperty->isPublic()) {
if ($reflectionProperty->isPublic() && (!($context[self::EXCLUDE_STATIC_PROPERTIES] ?? false) || !$reflectionProperty->isStatic())) {
Copy link
Member

Choose a reason for hiding this comment

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

It almost looks like a bug to me that we used to include static properties. I would like to change the default behavior and add a flag to include static props instead, but it's maybe to late (BC break). WDYT @symfony/deciders?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I am afraid changing that would be a BC break.

$properties[$reflectionProperty->name] = $reflectionProperty->name;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,113 +34,176 @@ protected function setUp()
$this->extractor = new ReflectionExtractor();
}

public function testGetProperties()
/**
* @dataProvider getPropertiesProvider
*/
public function testGetProperties($class, $expected, $mutatorPrefixes, $accessorPrefixes, $arrayMutatorPrefixes, $context)
{
$this->assertSame(
array(
'bal',
'parent',
'collection',
'nestedCollection',
'mixedCollection',
'B',
'Guid',
'g',
'h',
'i',
'j',
'emptyVar',
'iteratorCollection',
'iteratorCollectionWithKey',
'nestedIterators',
'foo',
'foo2',
'foo3',
'foo4',
'foo5',
'files',
'a',
'DOB',
'Id',
'123',
'self',
'realParent',
'c',
'd',
'e',
'f',
),
$this->extractor->getProperties('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy')
);
$extractor = new ReflectionExtractor($mutatorPrefixes, $accessorPrefixes, $arrayMutatorPrefixes);

$this->assertNull($this->extractor->getProperties('Symfony\Component\PropertyInfo\Tests\Fixtures\NoProperties'));
$this->assertSame($expected, $extractor->getProperties($class, $context));
}

public function testGetPropertiesWithCustomPrefixes()
public function getPropertiesProvider()
{
$customExtractor = new ReflectionExtractor(array('add', 'remove'), array('is', 'can'));

$this->assertSame(
return array(
array(
'bal',
'parent',
'collection',
'nestedCollection',
'mixedCollection',
'B',
'Guid',
'g',
'h',
'i',
'j',
'emptyVar',
'iteratorCollection',
'iteratorCollectionWithKey',
'nestedIterators',
'foo',
'foo2',
'foo3',
'foo4',
'foo5',
'files',
'c',
'd',
'e',
'f',
'Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy',
array(
'bal',
'parent',
'collection',
'nestedCollection',
'mixedCollection',
'B',
'Guid',
'g',
'h',
'i',
'j',
'k',
'emptyVar',
'iteratorCollection',
'iteratorCollectionWithKey',
'nestedIterators',
'foo',
'foo2',
'foo3',
'foo4',
'foo5',
'files',
'a',
'DOB',
'Id',
'123',
'self',
'realParent',
'c',
'd',
'e',
'f',
),
null,
null,
null,
array(),
),
array(
'Symfony\Component\PropertyInfo\Tests\Fixtures\NoProperties',
null,
null,
null,
null,
array(),
),
array(
'Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy',
array(
'bal',
'parent',
'collection',
'nestedCollection',
'mixedCollection',
'B',
'Guid',
'g',
'h',
'i',
'j',
'k',
'emptyVar',
'iteratorCollection',
'iteratorCollectionWithKey',
'nestedIterators',
'foo',
'foo2',
'foo3',
'foo4',
'foo5',
'files',
'c',
'd',
'e',
'f',
),
array('add', 'remove'),
array('is', 'can'),
null,
array(),
),
array(
'Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy',
array(
'bal',
'parent',
'collection',
'nestedCollection',
'mixedCollection',
'B',
'Guid',
'g' 1E79 ,
'h',
'i',
'j',
'k',
'emptyVar',
'iteratorCollection',
'iteratorCollectionWithKey',
'nestedIterators',
'foo',
'foo2',
'foo3',
'foo4',
'foo5',
F438 'files',
),
array(),
array(),
array(),
array(),
),
$customExtractor->getProperties('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy')
);
}

public function testGetPropertiesWithNoPrefixes()
{
$noPrefixExtractor = new ReflectionExtractor(array(), array(), array());

$this->assertSame(
array(
'bal',
'parent',
'collection',
'nestedCollection',
'mixedCollection',
'B',
'Guid',
'g',
'h',
'i',
'j',
'emptyVar',
'iteratorCollection',
'iteratorCollectionWithKey',
'nestedIterators',
'foo',
'foo2',
'foo3',
'foo4',
'foo5',
'files',
'Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy',
array(
'bal',
'parent',
'collection',
'nestedCollection',
'mixedCollection',
'B',
'Guid',
'g',
'h',
'i',
'j',
'emptyVar',
'iteratorCollection',
'iteratorCollectionWithKey',
'nestedIterators',
'foo',
'foo2',
'foo3',
'foo4',
'foo5',
'files',
'a',
'DOB',
'Id',
'123',
'self',
'realParent',
'c',
'd',
'e',
'f',
),
null,
null,
null,
array(
ReflectionExtractor::EXCLUDE_STATIC_PROPERTIES => true,
),
),
$noPrefixExtractor->getProperties('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy')
);
}

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/PropertyInfo/Tests/Fixtures/Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ class Dummy extends ParentDummy
*/
public $j;

/**
* @var ?string
*/
public static $k;

/**
* This should not be removed.
*
Expand Down
Loading
0