-
Notifications
You must be signed in to change notification settings - Fork 506
Don't remember non-nullable properties as nullable #3943
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
Closed
+115
−9
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9d91353
Don't remember non-nullable properties as nullable
staabm 077c040
Update property-null-after-assignment.php
staabm bd78d22
readonly property is php 8.1+
staabm 6491c5d
rename test
staabm 3285edc
more tests
staabm 7612714
Update remember-non-nullable-property.php
staabm 4d1b094
Update NodeScopeResolver.php
staabm 2c8c9ee
property cannot get null, even in strict-types
staabm 307d43e
restructure
staabm d88023a
restructure 2
staabm 938ed5c
Delete tests/PHPStan/Analyser/nsrt/remember-non-nullable-property.php
staabm f1be009
TypeInferenceTestCase: support declare(); before // lint
staabm aad5ec2
Delete remember-non-nullable-property.php
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
86 changes: 86 additions & 0 deletions
86
tests/PHPStan/Analyser/nsrt/remember-non-nullable-property-non-strict.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,86 @@ | ||
<?php declare(strict_types = 0); // lint >= 8.1 | ||
|
||
namespace RememberNonNullablePropertyWhenStrictTypesDisabled; | ||
|
||
use function PHPStan\Testing\assertNativeType; | ||
use function PHPStan\Testing\assertType; | ||
|
||
class KeepsPropertyNonNullable { | ||
private readonly int $i; | ||
|
||
public function __construct() | ||
{ | ||
$this->i = getIntOrNull(); | ||
} | ||
|
||
public function doFoo(): void { | ||
assertType('int', $this->i); | ||
assertNativeType('int', $this->i); | ||
} | ||
} | ||
|
||
class DontCoercePhpdocType { | ||
/** @var int */ | ||
private $i; | ||
|
||
public function __construct() | ||
{ | ||
$this->i = getIntOrNull(); | ||
} | ||
|
||
public function doFoo(): void { | ||
assertType('int', $this->i); | ||
assertNativeType('mixed', $this->i); | ||
} | ||
} | ||
|
||
function getIntOrNull(): ?int { | ||
if (rand(0, 1) === 0) { | ||
return null; | ||
} | ||
return 1; | ||
} | ||
|
||
|
||
class KeepsPropertyNonNullable2 { | ||
private int|float $i; | ||
|
||
public function __construct() | ||
{ | ||
$this->i = getIntOrFloatOrNull(); | ||
} | ||
|
||
public function doFoo(): void { | ||
assertType('float|int', $this->i); | ||
assertNativeType('float|int', $this->i); | ||
} | ||
} | ||
|
||
function getIntOrFloatOrNull(): null|int|float { | ||
if (rand(0, 1) === 0) { | ||
return null; | ||
} | ||
|
||
if (rand(0, 10) === 0) { | ||
return 1.0; | ||
} | ||
return 1; | ||
} | ||
|
||
class NarrowsNativeUnion { | ||
private readonly int|float $i; | ||
|
||
public function __construct() | ||
{ | ||
$this->i = getInt(); | ||
} | ||
|
||
public function doFoo(): void { | ||
assertType('int', $this->i); | ||
assertNativeType('int', $this->i); | ||
} | ||
} | ||
|
||
function getInt(): int { | ||
return 1; | ||
} |
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.
added support for
<?php declare(…); // lint >= 8.1
-format ingatherAssertTypesFromDirectory