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
Wrong PossiblyNullReference on if condition #3825
Labels
Comments
I found these snippets: https://psalm.dev/r/1100ba2a85<?php
class PropertyClass
{
public function test(): string
{
return 'test';
}
}
class MainClass
{
/** @var null|PropertyClass */
public $property = null;
public function getProperty(): ?PropertyClass //<-- may be nullable
{
return $this->property;
}
}
$main = new MainClass();
if (random_int(0, 1) === 1) {
$main->property = new PropertyClass();
}
if ($main->getProperty() !== null && $main->getProperty()->test() === 'test') { //<-- can't be null after first condition
echo 'ok' . PHP_EOL;
}
|
Simple getters should be assumed to be mutation-free; I need to re-evaluate the circumstances in which that occurs |
thx! |
So now this is fixed you have a bunch of possible solutions available:
|
I found these snippets: https://psalm.dev/r/4da4914033<?php
class PropertyClass
{
public function test(): string
{
return 'test';
}
}
class MainClass
{
/** @var null|PropertyClass */
public $property = null;
public function getProperty(): ?PropertyClass
{
return $this->property;
}
}
$main = new MainClass();
if (random_int(0, 1) === 1) {
$main->property = new PropertyClass();
}
if ($main->getProperty() && $main->getProperty()->test() === 'test') {
echo 'ok' . PHP_EOL;
}
https://psalm.dev/r/3a3f4ba96e<?php
class PropertyClass
{
public function test(): string
{
return 'test';
}
}
class MainClass
{
/** @var null|PropertyClass */
public $property = null;
final public function getProperty(): ?PropertyClass
{
return $this->property;
}
}
$main = new MainClass();
if (random_int(0, 1) === 1) {
$main->property = new PropertyClass();
}
if ($main->getProperty() !== null && $main->getProperty()->test() === 'test') {
echo 'ok' . PHP_EOL;
}
https://psalm.dev/r/3e031e963d<?php
class PropertyClass
{
public function test(): string
{
return 'test';
}
}
final class MainClass
{
/** @var null|PropertyClass */
public $property = null;
public function getProperty(): ?PropertyClass
{
return $this->property;
}
}
$main = new MainClass();
if (random_int(0, 1) === 1) {
$main->property = new PropertyClass();
}
if ($main->getProperty() !== null && $main->getProperty()->test() === 'test') {
echo 'ok' . PHP_EOL;
}
https://psalm.dev/r/91a1bb120d<?php
class PropertyClass
{
public function test(): string
{
return 'test';
}
}
class MainClass
{
/** @var null|PropertyClass */
public $property = null;
/**
* @psalm-mutation-free
*/
public function getProperty(): ?PropertyClass
{
return $this->property;
}
}
$main = new MainClass();
if (random_int(0, 1) === 1) {
$main->property = new PropertyClass();
}
if ($main->getProperty() !== null && $main->getProperty()->test() === 'test') {
echo 'ok' . PHP_EOL;
}
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, i wrote a example
https://psalm.dev/r/1100ba2a85
I think psalm shouldn't warn because
$main->getProperty()
is already!== null
I found similar issue #3563, but it already fixed
The text was updated successfully, but these errors were encountered: