@@ -53,7 +53,7 @@ final class PhpStanExtractor implements PropertyTypeExtractorInterface, Construc
53
53
* @param list<string>|null $accessorPrefixes
54
54
* @param list<string>|null $arrayMutatorPrefixes
55
55
*/
56
- public function __construct (?array $ mutatorPrefixes = null , ?array $ accessorPrefixes = null , ?array $ arrayMutatorPrefixes = null )
56
+ public function __construct (?array $ mutatorPrefixes = null , ?array $ accessorPrefixes = null , ?array $ arrayMutatorPrefixes = null , private bool $ allowPrivateAccess = true )
57
57
{
58
58
if (!class_exists (ContextFactory::class)) {
59
59
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__ ));
@@ -232,6 +232,10 @@ private function getDocBlockFromProperty(string $class, string $property): ?arra
232
232
return null ;
233
233
}
234
234
235
+ if (!$ this ->canAccessMemberBasedOnItsVisibility ($ reflectionProperty )) {
236
+ return null ;
237
+ }
238
+
235
239
// Type can be inside property docblock as `@var`
236
240
$ rawDocNode = $ reflectionProperty ->getDocComment ();
237
241
$ phpDocNode = $ rawDocNode ? $ this ->getPhpDocNode ($ rawDocNode ) : null ;
@@ -274,8 +278,11 @@ private function getDocBlockFromMethod(string $class, string $ucFirstProperty, i
274
278
}
275
279
276
280
if (
277
- (self ::ACCESSOR === $ type && 0 === $ reflectionMethod ->getNumberOfRequiredParameters ())
278
- || (self ::MUTATOR === $ type && $ reflectionMethod ->getNumberOfParameters () >= 1 )
281
+ (
282
+ (self ::ACCESSOR === $ type && 0 === $ reflectionMethod ->getNumberOfRequiredParameters ())
283
+ || (self ::MUTATOR === $ type && $ reflectionMethod ->getNumberOfParameters () >= 1 )
284
+ )
285
+ && $ this ->canAccessMemberBasedOnItsVisibility ($ reflectionMethod )
279
286
) {
280
287
break ;
281
288
}
@@ -305,4 +312,9 @@ private function getPhpDocNode(string $rawDocNode): PhpDocNode
305
312
306
313
return $ phpDocNode ;
307
314
}
315
+
316
+ private function canAccessMemberBasedOnItsVisibility (\ReflectionProperty |\ReflectionMethod $ member ): bool
317
+ {
318
+ return $ this ->allowPrivateAccess || $ member ->isPublic ();
319
+ }
308
320
}
0 commit comments