Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitfc8e374

Browse files
committed
[PropertyInfo] restrict access to PhpStanExtractor based on visibility
1 parent82e0eff commitfc8e374

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

‎src/Symfony/Component/PropertyInfo/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Introduce`PropertyDocBlockExtractorInterface` to extract a property's doc block
8+
* Restrict access to`PhpStanExtractor` based on visibility
89

910
6.4
1011
---

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ final class PhpStanExtractor implements PropertyTypeExtractorInterface, Construc
4747
privatearray$mutatorPrefixes;
4848
privatearray$accessorPrefixes;
4949
privatearray$arrayMutatorPrefixes;
50+
privatebool$allowPrivateAccess;
5051

5152
/**
5253
* @param list<string>|null $mutatorPrefixes
5354
* @param list<string>|null $accessorPrefixes
5455
* @param list<string>|null $arrayMutatorPrefixes
5556
*/
56-
publicfunction__construct(array$mutatorPrefixes =null,array$accessorPrefixes =null,array$arrayMutatorPrefixes =null)
57+
publicfunction__construct(array$mutatorPrefixes =null,array$accessorPrefixes =null,array$arrayMutatorPrefixes =null,bool$allowPrivateAccess =true)
5758
{
5859
if (!class_exists(ContextFactory::class)) {
5960
thrownew \LogicException(sprintf('Unable to use the "%s" class as the "phpdocumentor/type-resolver" package is not installed. Try running composer require "phpdocumentor/type-resolver".',__CLASS__));
@@ -67,6 +68,7 @@ public function __construct(array $mutatorPrefixes = null, array $accessorPrefix
6768
$this->mutatorPrefixes =$mutatorPrefixes ?? ReflectionExtractor::$defaultMutatorPrefixes;
6869
$this->accessorPrefixes =$accessorPrefixes ?? ReflectionExtractor::$defaultAccessorPrefixes;
6970
$this->arrayMutatorPrefixes =$arrayMutatorPrefixes ?? ReflectionExtractor::$defaultArrayMutatorPrefixes;
71+
$this->allowPrivateAccess =$allowPrivateAccess;
7072

7173
$this->phpDocParser =newPhpDocParser(newTypeParser(newConstExprParser()),newConstExprParser());
7274
$this->lexer =newLexer();
@@ -232,6 +234,10 @@ private function getDocBlockFromProperty(string $class, string $property): ?arra
232234
returnnull;
233235
}
234236

237+
if (!$this->canAccessMemberBasedOnItsVisibility($reflectionProperty)) {
238+
returnnull;
239+
}
240+
235241
// Type can be inside property docblock as `@var`
236242
$rawDocNode =$reflectionProperty->getDocComment();
237243
$phpDocNode =$rawDocNode ?$this->getPhpDocNode($rawDocNode) :null;
@@ -274,8 +280,11 @@ private function getDocBlockFromMethod(string $class, string $ucFirstProperty, i
274280
}
275281

276282
if (
277-
(self::ACCESSOR ===$type &&0 ===$reflectionMethod->getNumberOfRequiredParameters())
278-
|| (self::MUTATOR ===$type &&$reflectionMethod->getNumberOfParameters() >=1)
283+
$this->canAccessMemberBasedOnItsVisibility($reflectionMethod)
284+
&& (
285+
(self::ACCESSOR ===$type &&0 ===$reflectionMethod->getNumberOfRequiredParameters())
286+
|| (self::MUTATOR ===$type &&$reflectionMethod->getNumberOfParameters() >=1)
287+
)
279288
) {
280289
break;
281290
}
@@ -305,4 +314,9 @@ private function getPhpDocNode(string $rawDocNode): PhpDocNode
305314

306315
return$phpDocNode;
307316
}
317+
318+
publicfunctioncanAccessMemberBasedOnItsVisibility(\ReflectionProperty|\ReflectionMethod$member):bool
319+
{
320+
return$member->isPublic() ||$this->allowPrivateAccess;
321+
}
308322
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
useSymfony\Component\PropertyInfo\Tests\Fixtures\ConstructorDummyWithoutDocBlock;
1818
useSymfony\Component\PropertyInfo\Tests\Fixtures\DefaultValue;
1919
useSymfony\Component\PropertyInfo\Tests\Fixtures\Dummy;
20+
useSymfony\Component\PropertyInfo\Tests\Fixtures\DummyPropertyAndGetterWithDifferentTypes;
2021
useSymfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy;
2122
useSymfony\Component\PropertyInfo\Tests\Fixtures\Php80Dummy;
2223
useSymfony\Component\PropertyInfo\Tests\Fixtures\Php80PromotedDummy;
@@ -475,6 +476,26 @@ public static function php80TypesProvider()
475476
[Php80PromotedDummy::class,'promoted',null],
476477
];
477478
}
479+
480+
publicstaticfunctionallowPrivateAccessProvider():array
481+
{
482+
return [
483+
[true, [newType(Type::BUILTIN_TYPE_STRING)]],
484+
[false, [newType(Type::BUILTIN_TYPE_ARRAY, collection:true, collectionKeyType:newType('int'), collectionValueType:newType('string'))]],
485+
];
486+
}
487+
488+
/**
489+
* @dataProvider allowPrivateAccessProvider
490+
*/
491+
publicfunctiontestAllowPrivateAccess(bool$allowPrivateAccess,array$expectedTypes)
492+
{
493+
$extractor =newPhpStanExtractor(allowPrivateAccess:$allowPrivateAccess);
494+
$this->assertEquals(
495+
$expectedTypes,
496+
$extractor->getTypes(DummyPropertyAndGetterWithDifferentTypes::class,'foo')
497+
);
498+
}
478499
}
479500

480501
class PhpStanOmittedParamTagTypeDocBlock
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespaceSymfony\Component\PropertyInfo\Tests\Fixtures;
6+
7+
finalreadonlyclass DummyPropertyAndGetterWithDifferentTypes
8+
{
9+
publicfunction__construct(
10+
/**
11+
* @var string
12+
*/
13+
privatestring$foo
14+
)
15+
{
16+
}
17+
18+
/**
19+
* @return array<int, string>
20+
*/
21+
publicfunctiongetFoo():array
22+
{
23+
return (array)$this->foo;
24+
}
25+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp