New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve class constant static analysis #7154
Merged
orklah
merged 3 commits into
vimeo:master
from
AndrolGenhald:feature/class-const-improvements
Jan 26, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,41 @@ | ||
# AmbiguousConstantInheritance | ||
|
||
Emitted when a constant is inherited from multiple sources. | ||
|
||
```php | ||
<?php | ||
|
||
interface Foo | ||
{ | ||
/** @var non-empty-string */ | ||
public const CONSTANT='foo'; | ||
} | ||
|
||
interface Bar | ||
{ | ||
/** | ||
* @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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,17 @@ | |
use PhpParser; | ||
use Psalm\CodeLocation; | ||
use Psalm\Context; | ||
use Psalm\FileManipulation; | ||
use Psalm\Internal\Analyzer\Statements\Expression\ClassConstAnalyzer; | ||
use Psalm\Internal\FileManipulation\FileManipulationBuffer; | ||
use Psalm\Internal\Provider\NodeDataProvider; | ||
use Psalm\Issue\ParseError; | ||
use Psalm\Issue\UndefinedInterface; | ||
use Psalm\IssueBuffer; | ||
use UnexpectedValueException; | ||
|
||
use function strtolower; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
|
@@ -32,6 +37,8 @@ public function analyze(): void | |
throw new LogicException('Something went badly wrong'); | ||
} | ||
|
||
$interface_context = new Context($this->fq_class_name); | ||
|
||
$project_analyzer = $this->file_analyzer->project_analyzer; | ||
$codebase = $project_analyzer->getCodebase(); | ||
$config = $project_analyzer->getConfig(); | ||
|
@@ -106,6 +113,7 @@ public function analyze(): void | |
); | ||
} | ||
|
||
$member_stmts = []; | ||
foreach ($this->class->stmts as $stmt) { | ||
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod) { | ||
$method_analyzer = new MethodAnalyzer($stmt, $this); | ||
|
@@ -141,7 +149,35 @@ public function analyze(): void | |
); | ||
|
||
return; | ||
} elseif ($stmt instanceof PhpParser\Node\Stmt\ClassConst) { | ||
$member_stmts[] = $stmt; | ||
|
||
foreach ($stmt->consts as $const) { | ||
$const_id = strtolower($this->fq_class_name) . '::' . $const->name; | ||
|
||
foreach ($codebase->class_constants_to_rename as $original_const_id => $new_const_name) { | ||
if ($const_id === $original_const_id) { | ||
$file_manipulations = [ | ||
new FileManipulation( | ||
(int) $const->name->getAttribute('startFilePos'), | ||
(int) $const->name->getAttribute('endFilePos') + 1, | ||
$new_const_name | ||
) | ||
]; | ||
|
||
FileManipulationBuffer::add( | ||
$this->getFilePath(), | ||
$file_manipulations | ||
); | ||
} | ||
} | ||
} | ||
Comment on lines
+152
to
+174
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copied this part from ClassAnalyzer. Haven't tested it, but I assume it's probably fine. |
||
} | ||
} | ||
|
||
$statements_analyzer = new StatementsAnalyzer($this, new NodeDataProvider()); | ||
$statements_analyzer->analyze($member_stmts, $interface_context, null, true); | ||
|
||
ClassConstAnalyzer::analyze($this->storage, $this->getCodebase()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this an issue when the type are compatibles?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an issue even when it's the same exact thing: https://3v4l.org/tjYML
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, I thought "ambiguous" was too tame a word for something that broke at compile time, I was wrong!