Skip to content

Commit e63cd9f

Browse files
authored
Merge pull request #14485 from danog/native-constant-override-type
Report an override that widens or drops the native type of a class constant
2 parents 3a3fdf6 + 3c4a74d commit e63cd9f

3 files changed

Lines changed: 90 additions & 0 deletions

File tree

‎docs/running_psalm/issues/InvalidClassConstantType.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,25 @@ class LowerLimits extends Limits
4242
public const int MAX = 5; // no issue
4343
}
4444
```
45+
46+
As in PHP, an override must keep a native type contained in the parent's:
47+
48+
```php
49+
<?php
50+
51+
class Limits
52+
{
53+
public const int MAX = 10;
54+
}
55+
56+
class WiderLimits extends Limits
57+
{
58+
public const int|string MAX = 5; // InvalidClassConstantType
59+
}
60+
61+
class UntypedLimits extends Limits
62+
{
63+
/** @var int */
64+
public const MAX = 5; // InvalidClassConstantType
65+
}
66+
```

‎src/Psalm/Internal/Analyzer/Statements/Expression/ClassConstAnalyzer.php‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,8 @@ public static function analyze(
813813
? $parent_const_storage->signature_type
814814
: $parent_const_storage->type;
815815

816+
$reported_type = false;
817+
816818
// Check covariance
817819
if ($const_storage->type !== null
818820
&& $parent_type !== null
@@ -853,6 +855,35 @@ public static function analyze(
853855
$const_storage->suppressed_issues,
854856
);
855857
}
858+
859+
$reported_type = true;
860+
}
861+
862+
// PHP requires an override of a natively typed constant to declare a native type the parent's contains
863+
$parent_signature_type = $parent_const_storage->signature_type;
864+
if (!$reported_type
865+
&& $parent_signature_type !== null
866+
&& $parent_const_storage !== $const_storage
867+
&& ($const_storage->signature_type === null
868+
|| !UnionTypeComparator::isContainedBy(
869+
$codebase,
870+
$const_storage->signature_type,
871+
$parent_signature_type,
872+
))
873+
) {
874+
IssueBuffer::maybeAdd(
875+
new InvalidClassConstantType(
876+
($const_storage->signature_type === null
877+
? "{$class_storage->name}::{$const_name} must declare a native type contained in"
878+
: "The native type \"{$const_storage->signature_type->getId()}\" of "
879+
. "{$class_storage->name}::{$const_name} is not contained in")
880+
. " the native type \"{$parent_signature_type->getId()}\" of "
881+
. "{$parent_classlike_storage->name}::{$const_name}",
882+
$type_location,
883+
"{$class_storage->name}::{$const_name}",
884+
),
885+
$const_storage->suppressed_issues,
886+
);
856887
}
857888

858889
// Check overridden final

‎tests/ConstantTest.php‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,18 @@ function bar(): Bar
309309
public static function providerValidCodeParse(): iterable
310310
{
311311
return [
312+
'overrideNarrowingTheNativeTypeOfTheParentConstant' => [
313+
'code' => '<?php
314+
class A {
315+
public const int|string X = 1;
316+
}
317+
final class B extends A {
318+
public const int X = 2;
319+
}',
320+
'assertions' => [],
321+
'ignored_issues' => [],
322+
'php_version' => '8.3',
323+
],
312324
'overrideKeepingTheNativeTypeOfTheParentConstant' => [
313325
'code' => '<?php
314326
interface HasVersion {
@@ -2818,6 +2830,31 @@ public function f(): int {
28182830
public static function providerInvalidCodeParse(): iterable
28192831
{
28202832
return [
2833+
'overrideWideningTheNativeTypeOfTheParentConstant' => [
2834+
'code' => '<?php
2835+
class A {
2836+
public const int X = 1;
2837+
}
2838+
final class B extends A {
2839+
public const int|string X = 2;
2840+
}',
2841+
'error_message' => 'InvalidClassConstantType - src' . DIRECTORY_SEPARATOR . 'somefile.php:6:49 - The native type "int|string" of B::X is not contained in the native type "int" of A::X',
2842+
'error_levels' => [],
2843+
'php_version' => '8.3',
2844+
],
2845+
'overrideDroppingTheNativeTypeOfTheParentConstant' => [
2846+
'code' => '<?php
2847+
interface I {
2848+
const int X = 1;
2849+
}
2850+
final class B implements I {
2851+
/** @var int */
2852+
const X = 2;
2853+
}',
2854+
'error_message' => 'InvalidClassConstantType - src' . DIRECTORY_SEPARATOR . 'somefile.php:7:31 - B::X must declare a native type contained in the native type "int" of I::X',
2855+
'error_levels' => [],
2856+
'php_version' => '8.3',
2857+
],
28212858
'constantDeclaredWithClassConstantWildcardTypeRejectsOtherValues' => [
28222859
'code' => '<?php
28232860
class Foo

0 commit comments

Comments
 (0)