Skip to content
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
2 changes: 1 addition & 1 deletion extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ services:
- phpstan.broker.methodsClassReflectionExtension

-
class: Pest\PHPStan\Type\Pest\ProtectedMethodCallIgnoreExtension
class: Pest\PHPStan\Type\Pest\TestCaseMemberVisibilityIgnoreExtension
tags:
- phpstan.ignoreErrorExtension

Expand Down
6 changes: 6 additions & 0 deletions src/Type/Pest/PestTestCaseWithTraitsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public function __construct(
parent::__construct($className);
}

/** @return list<class-string> */
public function getTraitNames(): array
{
return $this->traitNames;
}

#[Override]
public function hasMethod(string $methodName): TrinaryLogic
{
Expand Down
46 changes: 0 additions & 46 deletions src/Type/Pest/ProtectedMethodCallIgnoreExtension.php

This file was deleted.

93 changes: 93 additions & 0 deletions src/Type/Pest/TestCaseMemberVisibilityIgnoreExtension.php
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;
}
}
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();
});
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');
});
71 changes: 71 additions & 0 deletions tests/Rules/TraitMemberVisibilityTest.php
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 {
Comment thread
MrPunyapal marked this conversation as resolved.
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],
]);
});
32 changes: 32 additions & 0 deletions tests/Type/Fixtures/VisibilityTrait.php
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';
}
}
10 changes: 10 additions & 0 deletions tests/Type/Fixtures/VisibilityTraitUser.php
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;
}
Loading