-
Notifications
You must be signed in to change notification settings - Fork 667
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve class constant static analysis.
Add class const covariance support (fixes #5589). Add check for overriding const from interface in PHP < 8.1 (fixes #7108). Add check for ambiguous const inheritance.
- Loading branch information
1 parent
3e03dc6
commit 71f5b09
Showing
18 changed files
with
456 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# AmbiguousConstantInheritance | ||
|
||
Emitted when a constant is inherited from multiple sources. | ||
|
||
```php | ||
<?php | ||
|
||
interface Foo | ||
{ | ||
/** @var non-empty-string */ | ||
public const CONSTANT='foo'; | ||
} | ||
|
||
interface Bar | ||
{ | ||
/** | ||
* @psalm-suppress OverriddenInterfaceConstant | ||
* @var non-empty-string | ||
*/ | ||
public const CONSTANT='bar'; | ||
} | ||
|
||
interface Baz extends Foo, Bar {} | ||
``` | ||
|
||
```php | ||
<?php | ||
|
||
interface Foo | ||
{ | ||
/** @var non-empty-string */ | ||
public const CONSTANT='foo'; | ||
} | ||
|
||
class Bar | ||
{ | ||
/** @var non-empty-string */ | ||
public const CONSTANT='bar'; | ||
} | ||
|
||
class Baz extends Bar implements Foo {} | ||
``` |
28 changes: 28 additions & 0 deletions
28
docs/running_psalm/issues/LessSpecificClassConstantType.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# LessSpecificClassConstantType | ||
|
||
Emitted when a constant type in a child is less specific than the type in the parent. | ||
|
||
```php | ||
<?php | ||
|
||
class Foo | ||
{ | ||
/** @var int<1,max> */ | ||
public const CONSTANT = 3; | ||
|
||
public static function bar(): array | ||
{ | ||
return str_split("foobar", static::CONSTANT); | ||
} | ||
} | ||
|
||
class Bar extends Foo | ||
{ | ||
/** @var int */ | ||
public const CONSTANT = -1; | ||
} | ||
|
||
Bar::bar(); // Error: str_split argument 2 must be greater than 0 | ||
``` | ||
|
||
This issue will always show up when overriding a constant that doesn't have a docblock type. Psalm will infer the most specific type for the constant that it can, you have to add a type annotation to tell it what type constraint you wish to be applied. Otherwise Psalm has no way of telling if you mean for the constant to be a literal `1`, `int<1, max>`, `int`, `numeric`, etc. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# OverriddenInterfaceConstant | ||
|
||
Emitted when a constant declared on an interface is overridden by a child (illegal in PHP < 8.1). | ||
|
||
```php | ||
<?php | ||
|
||
interface Foo | ||
{ | ||
public const BAR='baz'; | ||
} | ||
|
||
interface Bar extends Foo | ||
{ | ||
public const BAR='foobar'; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.