Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[PropertyInfo] Restrict access to PhpStanExtractor based on visibility #53362

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

Merged
Merged
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
1 change: 1 addition & 0 deletions src/Symfony/Component/PropertyInfo/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

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

6.4
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class PhpStanExtractor implements PropertyTypeExtractorInterface, Construc
* @param list<string>|null $accessorPrefixes
* @param list<string>|null $arrayMutatorPrefixes
*/
public function __construct(?array $mutatorPrefixes = null, ?array $accessorPrefixes = null, ?array $arrayMutatorPrefixes = null)
public function __construct(?array $mutatorPrefixes = null, ?array $accessorPrefixes = null, ?array $arrayMutatorPrefixes = null, private bool $allowPrivateAccess = true)
{
if (!class_exists(ContextFactory::class)) {
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__));
Expand Down Expand Up @@ -232,6 +232,10 @@ private function getDocBlockFromProperty(string $class, string $property): ?arra
return null;
}

if (!$this->canAccessMemberBasedOnItsVisibility($reflectionProperty)) {
return null;
}

// Type can be inside property docblock as `@var`
$rawDocNode = $reflectionProperty->getDocComment();
$phpDocNode = $rawDocNode ? $this->getPhpDocNode($rawDocNode) : null;
Expand Down Expand Up @@ -274,8 +278,11 @@ private function getDocBlockFromMethod(string $class, string $ucFirstProperty, i
}

if (
(self::ACCESSOR === $type && 0 === $reflectionMethod->getNumberOfRequiredParameters())
|| (self::MUTATOR === $type && $reflectionMethod->getNumberOfParameters() >= 1)
(
(self::ACCESSOR === $type && 0 === $reflectionMethod->getNumberOfRequiredParameters())
|| (self::MUTATOR === $type && $reflectionMethod->getNumberOfParameters() >= 1)
)
&& $this->canAccessMemberBasedOnItsVisibility($reflectionMethod)
) {
break;
}
Expand Down Expand Up @@ -305,4 +312,9 @@ private function getPhpDocNode(string $rawDocNode): PhpDocNode

return $phpDocNode;
}

private function canAccessMemberBasedOnItsVisibility(\ReflectionProperty|\ReflectionMethod $member): bool
{
return $this->allowPrivateAccess || $member->isPublic();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\PropertyInfo\Tests\Fixtures\ConstructorDummyWithoutDocBlock;
use Symfony\Component\PropertyInfo\Tests\Fixtures\DefaultValue;
use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\DummyPropertyAndGetterWithDifferentTypes;
use Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php80Dummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php80PromotedDummy;
Expand Down Expand Up @@ -475,6 +476,26 @@ public static function php80TypesProvider()
[Php80PromotedDummy::class, 'promoted', null],
];
}

public static function allowPrivateAccessProvider(): array
{
return [
[true, [new Type(Type::BUILTIN_TYPE_STRING)]],
[false, [new Type(Type::BUILTIN_TYPE_ARRAY, collection: true, collectionKeyType: new Type('int'), collectionValueType: new Type('string'))]],
];
}

/**
* @dataProvider allowPrivateAccessProvider
*/
public function testAllowPrivateAccess(bool $allowPrivateAccess, array $expectedTypes)
{
$extractor = new PhpStanExtractor(allowPrivateAccess: $allowPrivateAccess);
$this->assertEquals(
$expectedTypes,
$extractor->getTypes(DummyPropertyAndGetterWithDifferentTypes::class, 'foo')
);
}
}

class PhpStanOmittedParamTagTypeDocBlock
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\PropertyInfo\Tests\Fixtures;

final readonly class DummyPropertyAndGetterWithDifferentTypes
{
public function __construct(
/**
* @var string
*/
private string $foo
) {
}

/**
* @return array<int, string>
*/
public function getFoo(): array
{
return (array)$this->foo;
}
}