Skip to content

make assigning property via reference impure#3082

Merged
ondrejmirtes merged 3 commits into
phpstan:1.11.xfrom
schlndh:feature-impureAssignRef
May 30, 2024
Merged

make assigning property via reference impure#3082
ondrejmirtes merged 3 commits into
phpstan:1.11.xfrom
schlndh:feature-impureAssignRef

Conversation

@schlndh

@schlndh schlndh commented May 17, 2024

Copy link
Copy Markdown
Contributor

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
Comment thread src/Analyser/NodeScopeResolver.php Outdated
Comment thread src/Analyser/NodeScopeResolver.php
$expr,
'propertyAssignByRef',
'property assignment by reference',
false,

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
@ondrejmirtes

Copy link
Copy Markdown
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