Skip to content

Commit 794d8fe

Browse files
phpstan-botclaude
authored andcommitted
Add test for PHPDoc type incompatible with native value
Tests that when a dynamicConstantNames constant has a @var PHPDoc type that is incompatible with its actual value (e.g. @var int on a null constant), the PHPDoc type is used as-is. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent f354773 commit 794d8fe

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

tests/PHPStan/Analyser/data/dynamic-constant.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class DynamicConstantClass
1919

2020
/** @var list<string> */
2121
const DYNAMIC_EMPTY_ARRAY_WITH_PHPDOC_CONSTANT = [];
22+
23+
/** @var int */
24+
const DYNAMIC_INCOMPATIBLE_PHPDOC_CONSTANT = null;
2225
}
2326

2427
class NoDynamicConstantClass
@@ -37,5 +40,6 @@ private function rip()
3740
assertType('string|null', GLOBAL_DYNAMIC_CONSTANT_WITH_EXPLICIT_TYPES);
3841
assertType('string|null', DynamicConstantClass::DYNAMIC_NULL_WITH_PHPDOC_CONSTANT);
3942
assertType('list<string>', DynamicConstantClass::DYNAMIC_EMPTY_ARRAY_WITH_PHPDOC_CONSTANT);
43+
assertType('int', DynamicConstantClass::DYNAMIC_INCOMPATIBLE_PHPDOC_CONSTANT);
4044
}
4145
}

tests/PHPStan/Analyser/dynamic-constants.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ parameters:
66
- DynamicConstants\DynamicConstantClass::DYNAMIC_CONSTANT_IN_CLASS
77
- DynamicConstants\DynamicConstantClass::DYNAMIC_NULL_WITH_PHPDOC_CONSTANT
88
- DynamicConstants\DynamicConstantClass::DYNAMIC_EMPTY_ARRAY_WITH_PHPDOC_CONSTANT
9+
- DynamicConstants\DynamicConstantClass::DYNAMIC_INCOMPATIBLE_PHPDOC_CONSTANT
910
- GLOBAL_DYNAMIC_CONSTANT
1011
DynamicConstants\DynamicConstantClass::DYNAMIC_CONSTANT_WITH_EXPLICIT_TYPES_IN_CLASS: 'string|null'
1112
GLOBAL_DYNAMIC_CONSTANT_WITH_EXPLICIT_TYPES: 'string|null'

0 commit comments

Comments
 (0)