@@ -47,13 +47,14 @@ final class PhpStanExtractor implements PropertyTypeExtractorInterface, Construc
47
47
private array $ mutatorPrefixes ;
48
48
private array $ accessorPrefixes ;
49
49
private array $ arrayMutatorPrefixes ;
50
+ private bool $ allowPrivateAccess ;
50
51
51
52
/**
52
53
* @param list<string>|null $mutatorPrefixes
53
54
* @param list<string>|null $accessorPrefixes
54
55
* @param list<string>|null $arrayMutatorPrefixes
55
56
*/
56
- public function __construct (array $ mutatorPrefixes = null , array $ accessorPrefixes = null , array $ arrayMutatorPrefixes = null )
57
+ public function __construct (array $ mutatorPrefixes = null , array $ accessorPrefixes = null , array $ arrayMutatorPrefixes = null , bool $ allowPrivateAccess = true )
57
58
{
58
59
if (!class_exists (ContextFactory::class)) {
59
60
throw new \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
67
68
$ this ->mutatorPrefixes = $ mutatorPrefixes ?? ReflectionExtractor::$ defaultMutatorPrefixes ;
68
69
$ this ->accessorPrefixes = $ accessorPrefixes ?? ReflectionExtractor::$ defaultAccessorPrefixes ;
69
70
$ this ->arrayMutatorPrefixes = $ arrayMutatorPrefixes ?? ReflectionExtractor::$ defaultArrayMutatorPrefixes ;
71
+ $ this ->allowPrivateAccess = $ allowPrivateAccess ;
70
72
71
73
$ this ->phpDocParser = new PhpDocParser (new TypeParser (new ConstExprParser ()), new ConstExprParser ());
72
74
$ this ->lexer = new Lexer ();
@@ -232,6 +234,10 @@ private function getDocBlockFromProperty(string $class, string $property): ?arra
232
234
return null ;
233
235
}
234
236
237
+ if (!$ this ->canAccessMemberBasedOnItsVisibility ($ reflectionProperty )) {
238
+ return null ;
239
+ }
240
+
235
241
// Type can be inside property docblock as `@var`
236
242
$ rawDocNode = $ reflectionProperty ->getDocComment ();
237
243
$ phpDocNode = $ rawDocNode ? $ this ->getPhpDocNode ($ rawDocNode ) : null ;
@@ -274,8 +280,11 @@ private function getDocBlockFromMethod(string $class, string $ucFirstProperty, i
274
280
}
275
281
276
282
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
+ )
279
288
) {
280
289
break ;
281
290
}
@@ -305,4 +314,9 @@ private function getPhpDocNode(string $rawDocNode): PhpDocNode
305
314
306
315
return $ phpDocNode ;
307
316
}
317
+
318
+ public function canAccessMemberBasedOnItsVisibility (\ReflectionProperty |\ReflectionMethod $ member ): bool
319
+ {
320
+ return $ member ->isPublic () || $ this ->allowPrivateAccess ;
321
+ }
308
322
}
0 commit comments