Do not restrict @internal usages of anonymous class members - #6235
Do not restrict @internal usages of anonymous class members#6235peter17 wants to merge 1 commit into
@internal usages of anonymous class members#6235Conversation
Anonymous classes are not part of any namespace, so the root-namespace comparison in RestrictedInternalUsageHelper always reported their members as used from outside their namespace.
|
@SanderMuller please review |
|
Reviewed at 11e3dc5 (base 0d109aa). The fix is correct and in the right place. One behaviour change deserves to be recorded before merge, because it isn't what the description says the PR does. Fix and tests
The early return also fires on the referenced classThe four static-access rules re-ask the extension with a rewritten declaring class and drop the error when that second answer is namespace Vendor\Library;
class Base
{
/** @internal */ public static $internalStatic;
/** @internal */ public const INTERNAL = 'x';
/** @internal */ public static function internalStaticMethod(): void {}
/** @internal */ public function internalMethod(): void {}
}
namespace App\Consumer;
use Vendor\Library\Base;
class NamedSubclass extends Base
{
public function f(): void
{
echo self::$internalStatic; // not reported, before or after
echo self::INTERNAL; // not reported, before or after
self::internalStaticMethod(); // not reported, before or after
$this->internalMethod(); // reported, before and after
}
}
function anonSubclass(): object
{
return new class extends Base {
public function f(): void
{
echo self::$internalStatic; // was reported, now silent
echo self::INTERNAL; // was reported, now silent
self::internalStaticMethod(); // was reported, now silent
$this->internalMethod(); // reported, before and after
}
};
}It is the same effect as lines 161, 162 and 164 of the new fixture losing their errors relative to the base. I think this is defensible rather than a bug.
Two small asks:
Other notes
|
Anonymous classes are not part of any namespace, so the root-namespace comparison in RestrictedInternalUsageHelper always reported their members as used from outside their namespace.
Fixes phpstan/phpstan#13042 (comment)
https://phpstan.org/r/3b58b0a4-f740-4941-a7b3-d74e3564876f