Skip to content

[Bug]: Class constant check reads private/protected constants from file scope (fatal "Cannot access private constant") #135

Closed

Description

@klunejko

Bug report

Version: typephp/typephp v0.14.0, PHP 8.5 (worked on v0.11.2)

Description
For a class constant with an @var docblock that cannot be proven statically,
ContractVisitor::collectUnprovenClassConstantChecks() appends a check statement
after the class body, at file scope:

} ($__typephpVal = \TypePHP\Internal\RuntimeTypeChecker::checkConstant('App\Example::MAP', \App\Example::MAP, ...)) ...

The method does not check the constant's visibility. For a private or protected
constant, \App\Example::MAP is not accessible from file scope, so loading the
file fails with a fatal error, even though the code is valid.

Reproduction

<?php declare(strict_types=1);

namespace App;

final class Example
{
    /** @var array<string, class-string> */
    private const array MAP = ['key' => \stdClass::class];

    public static function map(): array
    {
        return self::MAP;
    }
}

Loading this class with TypePHP enabled throws:

Error: Cannot access private constant App\Example::MAP

With TypePHP disabled the class loads normally.

Expected
Either check non-public constants from inside the class scope (e.g. a scope-bound
closure via Closure::bind(..., null, Example::class)), or skip the check for
non-public constants.

Workaround
@typephp-ignore on the constant's docblock (with respect_ignore_tags enabled).

Activity

  1. added
    bug
    on Nov 21, 2026
  2. self-assigned this
    on Nov 22, 2026
  3. added theissue type on Nov 22, 2026
  4. rcalicdan commented on Nov 22, 2026

    @rcalicdan
    Member

    @klunejko Hello this now resolve in recent v0.14.1 release..

    Note this release brings massive performance improvement..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions