Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 59d0981

Browse files
committed
address reviews
1 parent d24a0e7 commit 59d0981

3 files changed

Lines changed: 7 additions & 4 deletions

File tree

src/Phan/Analysis/ClassConstantTypesAnalyzer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ private static function checkConstantInheritance(CodeBase $code_base, Clazz $cla
292292
continue;
293293
}
294294

295-
// PHP enforces covariance for typed constants: child type must be a subtype of parent type
296-
if (!$constant_real_type->canCastToUnionType($inherited_real_type, $code_base)) {
295+
// PHP enforces covariance for typed constants: every type in the child must be a subtype of the parent
296+
if (!$constant_real_type->canStrictCastToUnionType($code_base, $inherited_real_type)) {
297297
Issue::maybeEmit(
298298
$code_base,
299299
$constant->getContext(),

tests/php83_files/expected/002_typed_class_constants.php.expected

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
%s:60 PhanTypeMismatchDeclaredConstant Constant \ComplexExpressions::WRONG_CALC is declared with type int but has value ('prefix' . self::BASE) of type 'prefix10'
88
%s:67 PhanTypeMismatchDeclaredConstant Constant \UnionTypes::WRONG is declared with type int|string but has value 3.14 of type 3.14
99
%s:74 PhanTypeMismatchDeclaredConstant Constant \NullableTypes::WRONG_NULL is declared with type ?int but has value 'not null' of type 'not null'
10-
%s:94 PhanConstantTypeMismatchInheritance Class constant \NonCovariantChild::STR is declared with type ?string which is not covariant with type string inherited from \NonCovariantParent (constant types must be invariant or covariant)
10+
%s:95 PhanConstantTypeMismatchInheritance Class constant \NonCovariantChild::STR is declared with type ?string which is not covariant with type string inherited from \NonCovariantParent (constant types must be invariant or covariant)
11+
%s:96 PhanConstantTypeMismatchInheritance Class constant \NonCovariantChild::NUM is declared with type int|string which is not covariant with type int inherited from \NonCovariantParent (constant types must be invariant or covariant)

tests/php83_files/src/002_typed_class_constants.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ class CovariantChild extends CovariantParent {
8888
// Test non-covariant inheritance: widening is not allowed
8989
class NonCovariantParent {
9090
const string STR = 'test';
91+
const int NUM = 1;
9192
}
9293

9394
class NonCovariantChild extends NonCovariantParent {
94-
const ?string STR = null; // ERROR: ?string widens string (not covariant)
95+
const ?string STR = null; // ERROR: ?string widens string (not covariant)
96+
const int|string NUM = 1; // ERROR: int|string widens int (not covariant)
9597
}

0 commit comments

Comments
 (0)