Skip to content
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

PhanUndeclaredConstant when constant is loaded late #4855

Open
anomiex opened this issue May 2, 2024 · 0 comments
Open

PhanUndeclaredConstant when constant is loaded late #4855

anomiex opened this issue May 2, 2024 · 0 comments

Comments

@anomiex
Copy link

anomiex commented May 2, 2024

Phan version: 5.4.3, v5.x-dev 790e4f7

Reproduction

  1. Create the following file:
    <?php
    
    class Foo {
        /**
         * @var string
         */
        public $var = SOME_CONSTANT;
    }
    
    define( 'SOME_CONSTANT', 'const' );
  2. Do vendor/bin/phan --init --init-no-composer --init-analyze-file=test.php
  3. Run vendor/bin/phan

Expected results

No issues reported, successful exit code.

Actual results

test.php:7 PhanUndeclaredConstant Reference to undeclared constant \SOME_CONSTANT. This will cause a thrown Error in php 8.0+.

Other notes

In the production case where I encountered this, the class and constant are in separate files, with the constant's file being processed after the class's. Switching the processing order of the two files makes the bug go away.

The @var phpdoc is required, the bug doesn't reproduce without it.

Despite the message, PHP 8.3 throws no errors when executing this test code, nor when instantiating the class defined by this code. It seems PHP doesn't try to resolve the constant until the first time the class is instantiated or a static member is accessed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant