diff --git a/src/Rules/Constants/ValueAssignedToClassConstantRule.php b/src/Rules/Constants/ValueAssignedToClassConstantRule.php index d7a75f98f2..5948b1b3fa 100644 --- a/src/Rules/Constants/ValueAssignedToClassConstantRule.php +++ b/src/Rules/Constants/ValueAssignedToClassConstantRule.php @@ -85,7 +85,7 @@ private function processSingleConstant(ClassReflection $classReflection, string 'Configuration defined type for constant %s::%s (%s) does not accept value %s.', $constantReflection->getDeclaringClass()->getDisplayName(), $constantName, - $configuredType->describe(VerbosityLevel::typeOnly()), + $configuredType->describe(VerbosityLevel::value()), $valueExprType->describe($verbosity), ))->acceptsReasonsTip($accepts->reasons)->identifier('classConstant.value')->build(), ]; diff --git a/src/Rules/Constants/ValueAssignedToDefineRule.php b/src/Rules/Constants/ValueAssignedToDefineRule.php index 04c6f49f34..75af39cca4 100644 --- a/src/Rules/Constants/ValueAssignedToDefineRule.php +++ b/src/Rules/Constants/ValueAssignedToDefineRule.php @@ -72,7 +72,7 @@ public function processNode(Node $node, Scope $scope): array $errors[] = RuleErrorBuilder::message(sprintf( 'Configuration defined type for constant %s (%s) does not accept value %s.', $constantName, - $configuredType->describe(VerbosityLevel::typeOnly()), + $configuredType->describe(VerbosityLevel::value()), $valueType->describe($verbosity), ))->acceptsReasonsTip($accepts->reasons)->identifier('constant.defineValue')->build(); } diff --git a/src/Rules/Constants/ValueAssignedToGlobalConstantRule.php b/src/Rules/Constants/ValueAssignedToGlobalConstantRule.php index e9f17ab9b1..dd75bddd0c 100644 --- a/src/Rules/Constants/ValueAssignedToGlobalConstantRule.php +++ b/src/Rules/Constants/ValueAssignedToGlobalConstantRule.php @@ -52,7 +52,7 @@ public function processNode(Node $node, Scope $scope): array $errors[] = RuleErrorBuilder::message(sprintf( 'Configuration defined type for constant %s (%s) does not accept value %s.', $constantName, - $configuredType->describe(VerbosityLevel::typeOnly()), + $configuredType->describe(VerbosityLevel::value()), $valueType->describe($verbosity), ))->acceptsReasonsTip($accepts->reasons)->identifier('constant.value')->build(); } diff --git a/tests/PHPStan/Rules/Constants/ValueAssignedToClassConstantWithDynamicNamesRuleTest.php b/tests/PHPStan/Rules/Constants/ValueAssignedToClassConstantWithDynamicNamesRuleTest.php index dd74f58562..dd909d9b32 100644 --- a/tests/PHPStan/Rules/Constants/ValueAssignedToClassConstantWithDynamicNamesRuleTest.php +++ b/tests/PHPStan/Rules/Constants/ValueAssignedToClassConstantWithDynamicNamesRuleTest.php @@ -38,6 +38,10 @@ public function testRule(): void 'Configuration defined type for constant ValueAssignedToClassConstantDynamicNames\Foo::MAYBE_BAR (int<1, max>) does not accept value int.', 14, ], + [ + "Configuration defined type for constant ValueAssignedToClassConstantDynamicNames\Foo::A_NON_EMPTY_STRING (non-empty-string) does not accept value ''.", + 15, + ], ]); } diff --git a/tests/PHPStan/Rules/Constants/ValueAssignedToDefineRuleTest.php b/tests/PHPStan/Rules/Constants/ValueAssignedToDefineRuleTest.php index 37afefc7de..1bc78c327b 100644 --- a/tests/PHPStan/Rules/Constants/ValueAssignedToDefineRuleTest.php +++ b/tests/PHPStan/Rules/Constants/ValueAssignedToDefineRuleTest.php @@ -37,6 +37,14 @@ public function testRule(): void 'Configuration defined type for constant BAR_CONSTANT (int|string|null) does not accept value int|false.', 6, ], + [ + "Configuration defined type for constant A_NON_EMPTY_STRING (non-empty-string) does not accept value ''.", + 12, + ], + [ + 'Configuration defined type for constant A_NON_EMPTY_STRING (non-empty-string) does not accept value string.', + 14, + ], ]); } diff --git a/tests/PHPStan/Rules/Constants/ValueAssignedToGlobalConstantRuleTest.php b/tests/PHPStan/Rules/Constants/ValueAssignedToGlobalConstantRuleTest.php index 3c64355ec6..e941bc8f77 100644 --- a/tests/PHPStan/Rules/Constants/ValueAssignedToGlobalConstantRuleTest.php +++ b/tests/PHPStan/Rules/Constants/ValueAssignedToGlobalConstantRuleTest.php @@ -37,6 +37,10 @@ public function testRule(): void 'Configuration defined type for constant MAYBE_CONSTANT (int<1, max>) does not accept value int.', 5, ], + [ + "Configuration defined type for constant A_NON_EMPTY_STRING (non-empty-string) does not accept value ''.", + 6, + ], ]); } diff --git a/tests/PHPStan/Rules/Constants/data/value-assigned-to-class-constant-dynamic-names.php b/tests/PHPStan/Rules/Constants/data/value-assigned-to-class-constant-dynamic-names.php index be63b30d9a..33ad772ba7 100644 --- a/tests/PHPStan/Rules/Constants/data/value-assigned-to-class-constant-dynamic-names.php +++ b/tests/PHPStan/Rules/Constants/data/value-assigned-to-class-constant-dynamic-names.php @@ -12,4 +12,5 @@ class Foo const BAR = false; // error - configured as int|string|null const BAR2 = 1; // fine - not in dynamicConstantNames const MAYBE_BAR = Bar::DYNAMIC; // error (maybe) - positive-int doesn't fully accept int + const A_NON_EMPTY_STRING = ''; } diff --git a/tests/PHPStan/Rules/Constants/data/value-assigned-to-define.php b/tests/PHPStan/Rules/Constants/data/value-assigned-to-define.php index 583432d532..1bd09e86c6 100644 --- a/tests/PHPStan/Rules/Constants/data/value-assigned-to-define.php +++ b/tests/PHPStan/Rules/Constants/data/value-assigned-to-define.php @@ -9,3 +9,8 @@ define('BAR_CONSTANT', null); // fine define('BAR_CONSTANT', rand(0,1) ? 1 : 'hello'); // fine define('OTHER_CONSTANT', false); // fine - not in dynamicConstantNames +define('A_NON_EMPTY_STRING', ''); +define('A_NON_EMPTY_STRING', '0'); +define('A_NON_EMPTY_STRING', getString()); + +function getString(): string {} diff --git a/tests/PHPStan/Rules/Constants/data/value-assigned-to-global-constant.php b/tests/PHPStan/Rules/Constants/data/value-assigned-to-global-constant.php index 6bfc737476..1ed5262ed4 100644 --- a/tests/PHPStan/Rules/Constants/data/value-assigned-to-global-constant.php +++ b/tests/PHPStan/Rules/Constants/data/value-assigned-to-global-constant.php @@ -3,3 +3,4 @@ const BAR_CONSTANT = false; // error const OTHER_CONSTANT = false; // fine - not in dynamicConstantNames const MAYBE_CONSTANT = DYNAMIC_INT_CONSTANT; // error (maybe) - positive-int doesn't fully accept int +const A_NON_EMPTY_STRING = ''; diff --git a/tests/PHPStan/Rules/Constants/value-assigned-dynamic-constant.neon b/tests/PHPStan/Rules/Constants/value-assigned-dynamic-constant.neon index 828c3abeb6..9181280086 100644 --- a/tests/PHPStan/Rules/Constants/value-assigned-dynamic-constant.neon +++ b/tests/PHPStan/Rules/Constants/value-assigned-dynamic-constant.neon @@ -6,6 +6,8 @@ parameters: BAR_CONSTANT: 'int|string|null' DYNAMIC_INT_CONSTANT: 'int' MAYBE_CONSTANT: 'positive-int' + A_NON_EMPTY_STRING: 'non-empty-string' ValueAssignedToClassConstantDynamicNames\Foo::BAR: 'int|string|null' ValueAssignedToClassConstantDynamicNames\Bar::DYNAMIC: 'int' ValueAssignedToClassConstantDynamicNames\Foo::MAYBE_BAR: 'positive-int' + ValueAssignedToClassConstantDynamicNames\Foo::A_NON_EMPTY_STRING: 'non-empty-string'