-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consider uninitialized typed properties as possibly undefined
`isset()` & friends can be used to check if a typed property has been initialized. Phan does not consider the property as possibly undefined, so we need to do that explicitly to avoid emitting an issue. Bug: T378286 Change-Id: I6f9463550fd3eba63f25e8b4273bf4fe08fbab0e
- Loading branch information
Showing
3 changed files
with
30 additions
and
0 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
2 changes: 2 additions & 0 deletions
2
tests/plugins/RedundantExistenceChecksPlugin/isset-typedprop/expectedResults.txt
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,2 @@ | ||
plugins/RedundantExistenceChecksPlugin/isset-typedprop/test.php:13 MediaWikiNoIssetIfDefined Found usage of isset() on expression $this->strWithDefault that appears to be always set. isset() should only be used to suppress errors. Check whether the expression is null instead. See https://w.wiki/98zs | ||
plugins/RedundantExistenceChecksPlugin/isset-typedprop/test.php:14 MediaWikiNoIssetIfDefined Found usage of isset() on expression $this->untypedWithDoc that appears to be always set. isset() should only be used to suppress errors. Check whether the expression is null instead. See https://w.wiki/98zs |
19 changes: 19 additions & 0 deletions
19
tests/plugins/RedundantExistenceChecksPlugin/isset-typedprop/test.php
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 @@ | ||
<?php | ||
|
||
class TestTypedProperty { | ||
private stdClass $obj1; | ||
private string $str; | ||
private string $strWithDefault = 'foo'; | ||
/** @var string */ | ||
private $untypedWithDoc; | ||
|
||
function doTest() { | ||
print_r( isset( $this->obj1 ) ); // No issue (to avoid false positives) | ||
print_r( isset( $this->str ) ); // No issue (to avoid false positives) | ||
print_r( isset( $this->strWithDefault ) ); // Redundant isset | ||
print_r( isset( $this->untypedWithDoc ) ); // Redundant isset | ||
|
||
$this->str = 'XcQ'; | ||
print_r( isset( $this->str ) ); // No issue (due to upstream limitation) | ||
} | ||
} |