Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7666 from AndrolGenhald/more-class-const-improvem…
…ents More class const improvements.
- Loading branch information
Showing
11 changed files
with
195 additions
and
27 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
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 @@ | ||
# InvalidClassConstantType | ||
|
||
Emitted when a constant type in a child does not satisfy 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<min,-1> */ | ||
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,19 @@ | ||
# OverriddenFinalConstant | ||
|
||
Emitted when a constant declared as final is overridden in a child class or interface. | ||
|
||
```php | ||
<?php | ||
|
||
class Foo | ||
{ | ||
/** @var string */ | ||
final public const BAR='baz'; | ||
} | ||
|
||
class Bar extends Foo | ||
{ | ||
/** @var string */ | ||
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
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,9 @@ | ||
<?php | ||
|
||
namespace Psalm\Issue; | ||
|
||
final class InvalidClassConstantType extends ClassConstantIssue | ||
{ | ||
public const ERROR_LEVEL = 1; | ||
public const SHORTCODE = 309; | ||
} |
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,9 @@ | ||
<?php | ||
|
||
namespace Psalm\Issue; | ||
|
||
final class OverriddenFinalConstant extends ClassConstantIssue | ||
{ | ||
public const ERROR_LEVEL = 6; | ||
public const SHORTCODE = 310; | ||
} |
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
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