Skip to content

Commit 03763ec

Browse files
authored
Configuration defined type validation: Improve error message (#5700)
1 parent 293a6ea commit 03763ec

10 files changed

Lines changed: 28 additions & 3 deletions

‎src/Rules/Constants/ValueAssignedToClassConstantRule.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private function processSingleConstant(ClassReflection $classReflection, string
8585
'Configuration defined type for constant %s::%s (%s) does not accept value %s.',
8686
$constantReflection->getDeclaringClass()->getDisplayName(),
8787
$constantName,
88-
$configuredType->describe(VerbosityLevel::typeOnly()),
88+
$configuredType->describe(VerbosityLevel::value()),
8989
$valueExprType->describe($verbosity),
9090
))->acceptsReasonsTip($accepts->reasons)->identifier('classConstant.value')->build(),
9191
];

‎src/Rules/Constants/ValueAssignedToDefineRule.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function processNode(Node $node, Scope $scope): array
7272
$errors[] = RuleErrorBuilder::message(sprintf(
7373
'Configuration defined type for constant %s (%s) does not accept value %s.',
7474
$constantName,
75-
$configuredType->describe(VerbosityLevel::typeOnly()),
75+
$configuredType->describe(VerbosityLevel::value()),
7676
$valueType->describe($verbosity),
7777
))->acceptsReasonsTip($accepts->reasons)->identifier('constant.defineValue')->build();
7878
}

‎src/Rules/Constants/ValueAssignedToGlobalConstantRule.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function processNode(Node $node, Scope $scope): array
5252
$errors[] = RuleErrorBuilder::message(sprintf(
5353
'Configuration defined type for constant %s (%s) does not accept value %s.',
5454
$constantName,
55-
$configuredType->describe(VerbosityLevel::typeOnly()),
55+
$configuredType->describe(VerbosityLevel::value()),
5656
$valueType->describe($verbosity),
5757
))->acceptsReasonsTip($accepts->reasons)->identifier('constant.value')->build();
5858
}

‎tests/PHPStan/Rules/Constants/ValueAssignedToClassConstantWithDynamicNamesRuleTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public function testRule(): void
3838
'Configuration defined type for constant ValueAssignedToClassConstantDynamicNames\Foo::MAYBE_BAR (int<1, max>) does not accept value int.',
3939
14,
4040
],
41+
[
42+
"Configuration defined type for constant ValueAssignedToClassConstantDynamicNames\Foo::A_NON_EMPTY_STRING (non-empty-string) does not accept value ''.",
43+
15,
44+
],
4145
]);
4246
}
4347

‎tests/PHPStan/Rules/Constants/ValueAssignedToDefineRuleTest.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ public function testRule(): void
3737
'Configuration defined type for constant BAR_CONSTANT (int|string|null) does not accept value int|false.',
3838
6,
3939
],
40+
[
41+
"Configuration defined type for constant A_NON_EMPTY_STRING (non-empty-string) does not accept value ''.",
42+
12,
43+
],
44+
[
45+
'Configuration defined type for constant A_NON_EMPTY_STRING (non-empty-string) does not accept value string.',
46+
14,
47+
],
4048
]);
4149
}
4250

‎tests/PHPStan/Rules/Constants/ValueAssignedToGlobalConstantRuleTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public function testRule(): void
3737
'Configuration defined type for constant MAYBE_CONSTANT (int<1, max>) does not accept value int.',
3838
5,
3939
],
40+
[
41+
"Configuration defined type for constant A_NON_EMPTY_STRING (non-empty-string) does not accept value ''.",
42+
6,
43+
],
4044
]);
4145
}
4246

‎tests/PHPStan/Rules/Constants/data/value-assigned-to-class-constant-dynamic-names.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ class Foo
1212
const BAR = false; // error - configured as int|string|null
1313
const BAR2 = 1; // fine - not in dynamicConstantNames
1414
const MAYBE_BAR = Bar::DYNAMIC; // error (maybe) - positive-int doesn't fully accept int
15+
const A_NON_EMPTY_STRING = '';
1516
}

‎tests/PHPStan/Rules/Constants/data/value-assigned-to-define.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@
99
define('BAR_CONSTANT', null); // fine
1010
define('BAR_CONSTANT', rand(0,1) ? 1 : 'hello'); // fine
1111
define('OTHER_CONSTANT', false); // fine - not in dynamicConstantNames
12+
define('A_NON_EMPTY_STRING', '');
13+
define('A_NON_EMPTY_STRING', '0');
14+
define('A_NON_EMPTY_STRING', getString());
15+
16+
function getString(): string {}

‎tests/PHPStan/Rules/Constants/data/value-assigned-to-global-constant.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
const BAR_CONSTANT = false; // error
44
const OTHER_CONSTANT = false; // fine - not in dynamicConstantNames
55
const MAYBE_CONSTANT = DYNAMIC_INT_CONSTANT; // error (maybe) - positive-int doesn't fully accept int
6+
const A_NON_EMPTY_STRING = '';

‎tests/PHPStan/Rules/Constants/value-assigned-dynamic-constant.neon‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ parameters:
66
BAR_CONSTANT: 'int|string|null'
77
DYNAMIC_INT_CONSTANT: 'int'
88
MAYBE_CONSTANT: 'positive-int'
9+
A_NON_EMPTY_STRING: 'non-empty-string'
910
ValueAssignedToClassConstantDynamicNames\Foo::BAR: 'int|string|null'
1011
ValueAssignedToClassConstantDynamicNames\Bar::DYNAMIC: 'int'
1112
ValueAssignedToClassConstantDynamicNames\Foo::MAYBE_BAR: 'positive-int'
13+
ValueAssignedToClassConstantDynamicNames\Foo::A_NON_EMPTY_STRING: 'non-empty-string'

0 commit comments

Comments
 (0)