Skip to content
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

make assigning property via reference impure #3082

Merged
merged 3 commits into from
May 30, 2024

Conversation

schlndh
Copy link
Contributor

@schlndh schlndh commented May 17, 2024

Fixes https://phpstan.org/r/cbe5c7f9-f5bd-4101-8cf6-37b7732946d4

I set certain = false, because theoretically the referenced variable may already be set, and the reference may never be written. In which case it would be pure. But I can't imagine why the reference would be used in such a way in the first place.

@schlndh schlndh force-pushed the feature-impureAssignRef branch from 1f01c4c to c2bb307 Compare May 17, 2024 12:28
src/Analyser/NodeScopeResolver.php Outdated Show resolved Hide resolved
$expr,
'propertyAssignByRef',
'property assignment by reference',
false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be true, right? There isn't a case where this might still be pure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be pure:

I set certain = false, because theoretically the referenced variable may already be set, and the reference may never be written. In which case it would be pure. But I can't imagine why the reference would be used in such a way in the first place.

<?php

class Foo
{
    public ?int $foo;
    
    public function bar(): void
    {
        $x = &$this->foo;
    }
}

$foo = new Foo;
$foo->foo = 5;
$foo->bar();
var_dump($foo->foo);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, cool :)

@ondrejmirtes ondrejmirtes merged commit 93d85c4 into phpstan:1.11.x May 30, 2024
441 of 445 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@schlndh schlndh deleted the feature-impureAssignRef branch May 30, 2024 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants