Skip to content

Type of global constant from define() #2644

@sapphirecat

Description

@sapphirecat

I have a Slim Framework app I'm adding Psalm to. There is a global constant defined at runtime during configuration:

<?php
// in the main settings file (slightly simplified),
// included by the front controller
// (phpunit bootstrap file defines it as 'test')
if (! defined('ENVIRONMENT')) {
    define('ENVIRONMENT', $_SERVER['APP_ENV'] ?? 'prod'); 
}

// in a PSR-4 autoloaded class file
class ErrorHandler {
  private function formatException(Throwable $ex) {
    // ...
    if (ENVIRONMENT === 'dev' || ENVIRONMENT === 'test') {
      // add stack trace to error responses
    }
  }
}

This constant is not available when Psalm is running, because it doesn't load in via the framework's front controller. If I give Psalm a bootstrap file that defines the constant, then Psalm ends up thinking the constant can have only one value, and flags the other values as impossible. (Psalm is set up to analyze this settings file, since it's under src/ with everything else, but it doesn't seem to be picking up this information about the constant and applying it to other files. Which is reasonable, since Psalm doesn't know the entry points / load order.)

Maybe the type could be set in <globals> in the config, except that it's not a variable, so that didn't work. It doesn't look like globals supports a const/constant tag.

It seems to be too complex to reproduce on https://psalm.dev – if everything is in one file, then Psalm can recognize that the constant can have multiple values and reports "no issues".

How do I get the correct type, or even just string, to Psalm; or, should I replace the constant with a global variable?

vimeo/psalm 3.8.2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions