Skip to content

PHPStan emits error when Safe\define() creates a dynamic constant with an enum value #446

Open
@acabal

Description

@acabal

Environment

PHP version: 8.1.28
PHPStan version: 1.11.1
Safe version: 2.5.0
PHPStan Safe Rule version: 1.2.0

Description of problem

When creating a constant using the Safe\define() function, and the value of the constant is an enum, PHPStan seems unable to understand the constant as a dynamic constant.

Example

PHPStan configuration

parameters:
	level:
		9
	dynamicConstantNames:
		- FOO_CONST
		- BAR_CONST

PHP

<?php
use function Safe\define;

enum TestEnum: string{
	case Foo = "foo";
	case Bar = "bar";
}

/** @var string $fooConfig */
$fooConfig = get_cfg_var('app.foo');

// Try to use Safe\define() with an enum
define('FOO_CONST', TestEnum::tryFrom($fooConfig) ?? TestEnum::Foo); // Safe\define()

if(FOO_CONST == TestEnum::Foo){ // Error: PHPStan emits: If condition is always true.
	print('Foo');
}

// Try to use \define() with an enum
\define('BAR_CONST', TestEnum::tryFrom($fooConfig) ?? TestEnum::Foo); // \define()

if(BAR_CONST == TestEnum::Foo){ // OK, got expected output - PHPStan emits: Function define is unsafe to use.
	print('Foo');
}
?>

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions