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

Fix false-positive ImpureVariable issue #10990

Open
wants to merge 2 commits into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

use function in_array;
use function is_string;
use function substr;
use function time;

/**
Expand Down Expand Up @@ -117,7 +118,7 @@ public static function analyze(
}

if (!$context->collect_mutations && !$context->collect_initializations) {
if ($context->pure) {
if ($context->pure && substr($context->calling_method_id, -13) !== '::__construct') {
IssueBuffer::maybeAdd(
new ImpureVariable(
'Cannot reference $this in a pure context',
Expand Down
24 changes: 24 additions & 0 deletions tests/PureAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,30 @@ class Date2{
Date2::tt();
',
],
'Issue #10974 - https://github.com/vimeo/psalm/issues/10974' => [
'code' => '<?php

class Foo
{
private int $int;

/** @psalm-pure */
public function __construct(int $int)
{
$this->int = $int;
}
}

class Factory
{
/** @psalm-pure */
public function getFoo(): Foo
{
return new Foo(42);
}
}
',
],
];
}

Expand Down