Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8952c02

Browse filesBrowse files
committed
feature #30706 [PropertyInfo] Add possibility to extract private and protected properties in reflection extractor (joelwurtz)
This PR was squashed before being merged into the 4.3-dev branch (closes #30706). Discussion ---------- [PropertyInfo] Add possibility to extract private and protected properties in reflection extractor | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #30248 | License | MIT | Doc PR | TODO This PR add the possibility to extract private and protected properties from a class by passing a new argument to the `ReflectionExtractor` This new argument consist of flag that filters properties, so someone will also be able to use the `ReflectionExtractor` only for private property ```php new ReflectionExtractor(null, null, null, true, ReflectionExtractor::ALLOW_PRIVATE | ReflectionExtractor::ALLOW_PROTECTED) ``` Flags method was prefered over a list of bool to avoid too many parameters and also be close to the reflection API of PHP Commits ------- 05e487f [PropertyInfo] Add possibility to extract private and protected properties in reflection extractor
2 parents bc18e39 + 05e487f commit 8952c02
Copy full SHA for 8952c02

File tree

Expand file treeCollapse file tree

2 files changed

+65
-7
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+65
-7
lines changed

‎src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php
+43-7Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
4242
*/
4343
public static $defaultArrayMutatorPrefixes = ['add', 'remove'];
4444

45+
public const ALLOW_PRIVATE = 1;
46+
public const ALLOW_PROTECTED = 2;
47+
public const ALLOW_PUBLIC = 4;
48+
4549
private const MAP_TYPES = [
4650
'integer' => Type::BUILTIN_TYPE_INT,
4751
'boolean' => Type::BUILTIN_TYPE_BOOL,
@@ -52,18 +56,20 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
5256
private $accessorPrefixes;
5357
private $arrayMutatorPrefixes;
5458
private $enableConstructorExtraction;
59+
private $accessFlags;
5560

5661
/**
5762
* @param string[]|null $mutatorPrefixes
5863
* @param string[]|null $accessorPrefixes
5964
* @param string[]|null $arrayMutatorPrefixes
6065
*/
61-
public function __construct(array $mutatorPrefixes = null, array $accessorPrefixes = null, array $arrayMutatorPrefixes = null, bool $enableConstructorExtraction = true)
66+
public function __construct(array $mutatorPrefixes = null, array $accessorPrefixes = null, array $arrayMutatorPrefixes = null, bool $enableConstructorExtraction = true, int $accessFlags = self::ALLOW_PUBLIC)
6267
{
6368
$this->mutatorPrefixes = null !== $mutatorPrefixes ? $mutatorPrefixes : self::$defaultMutatorPrefixes;
6469
$this->accessorPrefixes = null !== $accessorPrefixes ? $accessorPrefixes : self::$defaultAccessorPrefixes;
6570
$this->arrayMutatorPrefixes = null !== $arrayMutatorPrefixes ? $arrayMutatorPrefixes : self::$defaultArrayMutatorPrefixes;
6671
$this->enableConstructorExtraction = $enableConstructorExtraction;
72+
$this->accessFlags = $accessFlags;
6773
}
6874

6975
/**
@@ -77,16 +83,34 @@ public function getProperties($class, array $context = [])
7783
return;
7884
}
7985

86+
$propertyFlags = 0;
87+
$methodFlags = 0;
88+
89+
if ($this->accessFlags & self::ALLOW_PUBLIC) {
90+
$propertyFlags = $propertyFlags | \ReflectionProperty::IS_PUBLIC;
91+
$methodFlags = $methodFlags | \ReflectionMethod::IS_PUBLIC;
92+
}
93+
94+
if ($this->accessFlags & self::ALLOW_PRIVATE) {
95+
$propertyFlags = $propertyFlags | \ReflectionProperty::IS_PRIVATE;
96+
$methodFlags = $methodFlags | \ReflectionMethod::IS_PRIVATE;
97+
}
98+
99+
if ($this->accessFlags & self::ALLOW_PROTECTED) {
100+
$propertyFlags = $propertyFlags | \ReflectionProperty::IS_PROTECTED;
101+
$methodFlags = $methodFlags | \ReflectionMethod::IS_PROTECTED;
102+
}
103+
80104
$reflectionProperties = $reflectionClass->getProperties();
81105

82106
$properties = [];
83107
foreach ($reflectionProperties as $reflectionProperty) {
84-
if ($reflectionProperty->isPublic()) {
108+
if ($reflectionProperty->getModifiers() & $propertyFlags) {
85109
$properties[$reflectionProperty->name] = $reflectionProperty->name;
86110
}
87111
}
88112

89-
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
113+
foreach ($reflectionClass->getMethods($methodFlags) as $reflectionMethod) {
90114
if ($reflectionMethod->isStatic()) {
91115
continue;
92116
}
@@ -134,7 +158,7 @@ public function getTypes($class, $property, array $context = [])
134158
*/
135159
public function isReadable($class, $property, array $context = [])
136160
{
137-
if ($this->isPublicProperty($class, $property)) {
161+
if ($this->isAllowedProperty($class, $property)) {
138162
return true;
139163
}
140164

@@ -148,7 +172,7 @@ public function isReadable($class, $property, array $context = [])
148172
*/
149173
public function isWritable($class, $property, array $context = [])
150174
{
151-
if ($this->isPublicProperty($class, $property)) {
175+
if ($this->isAllowedProperty($class, $property)) {
152176
return true;
153177
}
154178

@@ -317,12 +341,24 @@ private function resolveTypeName(string $name, \ReflectionMethod $reflectionMeth
317341
return $name;
318342
}
319343

320-
private function isPublicProperty(string $class, string $property): bool
344+
private function isAllowedProperty(string $class, string $property): bool
321345
{
322346
try {
323347
$reflectionProperty = new \ReflectionProperty($class, $property);
324348

325-
return $reflectionProperty->isPublic();
349+
if ($this->accessFlags & self::ALLOW_PUBLIC && $reflectionProperty->isPublic()) {
350+
return true;
351+
}
352+
353+
if ($this->accessFlags & self::ALLOW_PROTECTED && $reflectionProperty->isProtected()) {
354+
return true;
355+
}
356+
357+
if ($this->accessFlags & self::ALLOW_PRIVATE && $reflectionProperty->isPrivate()) {
358+
return true;
359+
}
360+
361+
return false;
326362
} catch (\ReflectionException $e) {
327363
// Return false if the property doesn't exist
328364
}

‎src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
1616
use Symfony\Component\PropertyInfo\Tests\Fixtures\AdderRemoverDummy;
1717
use Symfony\Component\PropertyInfo\Tests\Fixtures\DefaultValue;
18+
use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy;
1819
use Symfony\Component\PropertyInfo\Tests\Fixtures\NotInstantiable;
1920
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php71Dummy;
2021
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php71DummyExtended2;
@@ -294,6 +295,27 @@ public function testSingularize()
294295
$this->assertEquals(['analyses', 'feet'], $this->extractor->getProperties(AdderRemoverDummy::class));
295296
}
296297

298+
public function testPrivatePropertyExtractor()
299+
{
300+
$privateExtractor = new ReflectionExtractor(null, null, null, true, ReflectionExtractor::ALLOW_PUBLIC | ReflectionExtractor::ALLOW_PRIVATE | ReflectionExtractor::ALLOW_PROTECTED);
301+
$properties = $privateExtractor->getProperties(Dummy::class);
302+
303+
$this->assertContains('bar', $properties);
304+
$this->assertContains('baz', $properties);
305+
306+
$this->assertTrue($privateExtractor->isReadable(Dummy::class, 'bar'));
307+
$this->assertTrue($privateExtractor->isReadable(Dummy::class, 'baz'));
308+
309+
$protectedExtractor = new ReflectionExtractor(null, null, null, true, ReflectionExtractor::ALLOW_PUBLIC | ReflectionExtractor::ALLOW_PROTECTED);
310+
$properties = $protectedExtractor->getProperties(Dummy::class);
311+
312+
$this->assertNotContains('bar', $properties);
313+
$this->assertContains('baz', $properties);
314+
315+
$this->assertFalse($protectedExtractor->isReadable(Dummy::class, 'bar'));
316+
$this->assertTrue($protectedExtractor->isReadable(Dummy::class, 'baz'));
317+
}
318+
297319
/**
298320
* @dataProvider getInitializableProperties
299321
*/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.