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

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

Open
acabal opened this issue May 15, 2024 · 0 comments

Comments

@acabal
Copy link

acabal commented May 15, 2024

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');
}
?>
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