-
-
Notifications
You must be signed in to change notification settings - Fork 7
fix: allow private and static trait methods on $this in test closures #19
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
MrPunyapal
merged 2 commits into
pestphp:5.x
from
bpmore:fix/bound-trait-private-and-static-visibility
Oct 2, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Pest\PHPStan\Type\Pest; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr; | ||
| use PhpParser\Node\Expr\ClassConstFetch; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PhpParser\Node\Expr\StaticCall; | ||
| use PhpParser\Node\Expr\Variable; | ||
| use PhpParser\Node\Identifier; | ||
| use PHPStan\Analyser\Error; | ||
| use PHPStan\Analyser\IgnoreErrorExtension; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Type\ObjectType; | ||
| use PHPStan\Type\Type; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class TestCaseMemberVisibilityIgnoreExtension implements IgnoreErrorExtension | ||
| { | ||
| private const array METHOD_IDENTIFIERS = ['method.protected', 'method.private']; | ||
|
|
||
| private const array STATIC_METHOD_IDENTIFIERS = ['staticMethod.protected', 'staticMethod.private']; | ||
|
|
||
| private const array CONSTANT_IDENTIFIERS = ['classConstant.protected', 'classConstant.private']; | ||
|
|
||
| public function shouldIgnore(Error $error, Node $node, Scope $scope): bool | ||
| { | ||
| $identifier = $error->getIdentifier(); | ||
|
|
||
| $receiver = match (true) { | ||
| $node instanceof MethodCall && in_array($identifier, self::METHOD_IDENTIFIERS, true) => $node->var, | ||
| $node instanceof StaticCall && in_array($identifier, self::STATIC_METHOD_IDENTIFIERS, true) => $node->class, | ||
| $node instanceof ClassConstFetch && in_array($identifier, self::CONSTANT_IDENTIFIERS, true) => $node->class, | ||
| default => null, | ||
| }; | ||
|
|
||
| if (! $receiver instanceof Variable || $receiver->name !== 'this') { | ||
| return false; | ||
| } | ||
|
|
||
| if (! $scope->isInAnonymousFunction()) { | ||
| return false; | ||
| } | ||
|
|
||
| if (! $scope->hasVariableType('this')->yes()) { | ||
| return false; | ||
| } | ||
|
|
||
| $thisType = $scope->getVariableType('this'); | ||
|
|
||
| if (! new ObjectType(TestCase::class)->isSuperTypeOf($thisType)->yes()) { | ||
| return false; | ||
| } | ||
|
|
||
| // @note: the closure is bound to a subclass of the test case, so its protected members are reachable. | ||
| if (str_ends_with($identifier, '.protected')) { | ||
| return true; | ||
| } | ||
|
|
||
| // @note: a private member is reachable only when it is declared by a trait that uses() binds, because Pest flattens those traits onto the generated class. | ||
| $declaringClass = $this->declaringClassName($node, $thisType, $scope); | ||
|
|
||
| return $declaringClass !== null | ||
| && $thisType instanceof PestTestCaseWithTraitsType | ||
| && in_array($declaringClass, $thisType->getTraitNames(), true); | ||
| } | ||
|
|
||
| private function declaringClassName(Expr $node, Type $thisType, Scope $scope): ?string | ||
| { | ||
| if (! ($node instanceof MethodCall || $node instanceof StaticCall || $node instanceof ClassConstFetch)) { | ||
| return null; | ||
| } | ||
|
|
||
| if (! $node->name instanceof Identifier) { | ||
| return null; | ||
| } | ||
|
|
||
| $name = $node->name->toString(); | ||
|
|
||
| if ($node instanceof ClassConstFetch) { | ||
| return $thisType->hasConstant($name)->yes() | ||
| ? $thisType->getConstant($name)->getDeclaringClass()->getName() | ||
| : null; | ||
| } | ||
|
|
||
| return $thisType->hasMethod($name)->yes() | ||
| ? $thisType->getMethod($name, $scope)->getDeclaringClass()->getName() | ||
| : null; | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
tests/Fixtures/CustomTestCaseInference/TraitVisibility/trait-visibility-calls.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Tests\Type\Fixtures\CustomTestCase; | ||
| use Tests\Type\Fixtures\VisibilityTrait; | ||
|
|
||
| uses(CustomTestCase::class, VisibilityTrait::class); | ||
|
|
||
| it('can call protected and private trait methods on $this', function (): void { | ||
| $this->protectedTraitMethod(); | ||
| $this->privateTraitMethod(); | ||
| }); | ||
|
|
||
| it('can call protected and private static trait methods through $this', function (): void { | ||
| $this::protectedStaticTraitMethod(); | ||
| $this::privateStaticTraitMethod(); | ||
| }); | ||
|
|
||
| it('can read protected and private trait constants through $this', function (): void { | ||
| expect($this::PROTECTED_TRAIT_CONSTANT)->toBe('protected'); | ||
| expect($this::PRIVATE_TRAIT_CONSTANT)->toBe('private'); | ||
| }); | ||
|
|
||
| beforeEach(function (): void { | ||
| $this->privateTraitMethod(); | ||
| $this::privateStaticTraitMethod(); | ||
| }); |
21 changes: 21 additions & 0 deletions
21
tests/Fixtures/CustomTestCaseInference/TraitVisibility/trait-visibility-errors.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Tests\Type\Fixtures\CustomTestCase; | ||
| use Tests\Type\Fixtures\VisibilityTrait; | ||
| use Tests\Type\Fixtures\VisibilityTraitUser; | ||
|
|
||
| uses(CustomTestCase::class, VisibilityTrait::class); | ||
|
|
||
| it('still reports private methods inherited from a parent class', function (): void { | ||
| $this->runTest(); | ||
| }); | ||
|
|
||
| it('still reports private trait methods called on an object other than $this', function (): void { | ||
| $other = new VisibilityTraitUser; | ||
|
|
||
| $other->privateTraitMethod(); | ||
| $other::privateStaticTraitMethod(); | ||
| expect($other::PRIVATE_TRAIT_CONSTANT)->toBe('private'); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\Rules; | ||
|
|
||
| use PHPStan\Rules\Classes\ClassConstantRule; | ||
| use PHPStan\Rules\Methods\CallMethodsRule; | ||
| use PHPStan\Rules\Methods\CallStaticMethodsRule; | ||
| use Tests\RuleTestCase; | ||
|
|
||
| beforeAll(function (): void { | ||
| RuleTestCase::$additionalConfigFiles = [ | ||
| __DIR__.'/../Type/custom-testcase-extension.neon', | ||
| ]; | ||
| }); | ||
|
|
||
| test('protected and private trait methods are callable on $this in pest closures', function (): void { | ||
| RuleTestCase::$rule = RuleTestCase::resolveRule(CallMethodsRule::class); | ||
|
|
||
| $this->analyse([ | ||
| __DIR__.'/../Fixtures/CustomTestCaseInference/TraitVisibility/trait-visibility-calls.php', | ||
| ], []); | ||
| }); | ||
|
|
||
| test('protected and private static trait methods are callable through $this in pest closures', function (): void { | ||
| RuleTestCase::$rule = RuleTestCase::resolveRule(CallStaticMethodsRule::class); | ||
|
|
||
| $this->analyse([ | ||
| __DIR__.'/../Fixtures/CustomTestCaseInference/TraitVisibility/trait-visibility-calls.php', | ||
| ], []); | ||
| }); | ||
|
|
||
| test('private methods not flattened from a bound trait are still reported', function (): void { | ||
| RuleTestCase::$rule = RuleTestCase::resolveRule(CallMethodsRule::class); | ||
|
|
||
| $this->analyse([ | ||
| __DIR__.'/../Fixtures/CustomTestCaseInference/TraitVisibility/trait-visibility-errors.php', | ||
| ], [ | ||
| ['Call to private method runTest() of class PHPUnit\Framework\TestCase.', 12], | ||
| ['Call to private method privateTraitMethod() of class Tests\Type\Fixtures\VisibilityTraitUser.', 18], | ||
| ]); | ||
| }); | ||
|
|
||
| test('private static trait methods called on another object are still reported', function (): void { | ||
| RuleTestCase::$rule = RuleTestCase::resolveRule(CallStaticMethodsRule::class); | ||
|
|
||
| $this->analyse([ | ||
| __DIR__.'/../Fixtures/CustomTestCaseInference/TraitVisibility/trait-visibility-errors.php', | ||
| ], [ | ||
| ['Call to private static method privateStaticTraitMethod() of class Tests\Type\Fixtures\VisibilityTraitUser.', 19], | ||
| ]); | ||
| }); | ||
|
|
||
| test('protected and private trait constants are readable through $this in pest closures', function (): void { | ||
| RuleTestCase::$rule = RuleTestCase::resolveRule(ClassConstantRule::class); | ||
|
|
||
| $this->analyse([ | ||
| __DIR__.'/../Fixtures/CustomTestCaseInference/TraitVisibility/trait-visibility-calls.php', | ||
| ], []); | ||
| }); | ||
|
|
||
| test('private trait constants read from another object are still reported', function (): void { | ||
| RuleTestCase::$rule = RuleTestCase::resolveRule(ClassConstantRule::class); | ||
|
|
||
| $this->analyse([ | ||
| __DIR__.'/../Fixtures/CustomTestCaseInference/TraitVisibility/trait-visibility-errors.php', | ||
| ], [ | ||
| ['Access to private constant PRIVATE_TRAIT_CONSTANT of class Tests\\Type\\Fixtures\\VisibilityTraitUser.', 20], | ||
| ]); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\Type\Fixtures; | ||
|
|
||
| trait VisibilityTrait | ||
| { | ||
| protected const string PROTECTED_TRAIT_CONSTANT = 'protected'; | ||
|
|
||
| private const string PRIVATE_TRAIT_CONSTANT = 'private'; | ||
|
|
||
| protected static function protectedStaticTraitMethod(): string | ||
| { | ||
| return 'protected static'; | ||
| } | ||
|
|
||
| protected function protectedTraitMethod(): string | ||
| { | ||
| return 'protected'; | ||
| } | ||
|
|
||
| private static function privateStaticTraitMethod(): string | ||
| { | ||
| return 'private static'; | ||
| } | ||
|
|
||
| private function privateTraitMethod(): string | ||
| { | ||
| return 'private'; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\Type\Fixtures; | ||
|
|
||
| final class VisibilityTraitUser | ||
| { | ||
| use VisibilityTrait; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.