Skip to content
Permalink
Browse files

Don’t complain about initialisation of external-mutation-free classes…

… from pure context
  • Loading branch information...
muglug committed Aug 30, 2019
1 parent bbde2d6 commit d891217a48dfd7257ea0a3cd3d8f2cdaa5cc7df3
@@ -1242,7 +1242,7 @@ function (PhpParser\Node\Arg $arg) {
}
} elseif (($method_storage->mutation_free
|| ($method_storage->external_mutation_free
&& isset($stmt->var->external_mutation_free)))
&& (isset($stmt->var->external_mutation_free) || isset($stmt->var->pure))))
&& $codebase->find_unused_variables
&& !$context->inside_conditional
&& !$context->inside_unset
@@ -415,7 +415,7 @@ public static function analyze(
if ($declaring_method_id) {
$method_storage = $codebase->methods->getStorage($declaring_method_id);
if (!$method_storage->pure) {
if (!$method_storage->external_mutation_free) {
if (IssueBuffer::accepts(
new ImpureMethodCall(
'Cannot call an impure constructor from a pure context',
@@ -523,13 +523,20 @@ public function __construct(string $foo) {
$this->foo = $foo;
}
public function setFoo(string $foo) : void {
$this->foo = $foo;
public function getFoo() : void {
return "abular" . $this->foo;
}
}
/**
* @psalm-pure
*/
function makeA(string $s) : A {
return new A($s);
}
function foo() : void {
$a = (new A("hello"))->setFoo("goodbye");
$a = makeA("hello")->getFoo();
}',
'<?php
/**
@@ -542,11 +549,18 @@ public function __construct(string $foo) {
$this->foo = $foo;
}
public function setFoo(string $foo) : void {
$this->foo = $foo;
public function getFoo() : void {
return "abular" . $this->foo;
}
}
/**
* @psalm-pure
*/
function makeA(string $s) : A {
return new A($s);
}
function foo() : void {
}',
'7.1',
@@ -793,6 +793,35 @@ function foo() : void {
}',
'error_message' => 'UnusedMethodCall',
],
'unusedMethodCallForGeneratingMethod' => [
'<?php
/**
* @psalm-external-mutation-free
*/
class A {
private string $foo;
public function __construct(string $foo) {
$this->foo = $foo;
}
public function getFoo() : string {
return "abular" . $this->foo;
}
}
/**
* @psalm-pure
*/
function makeA(string $s) : A {
return new A($s);
}
function foo() : void {
makeA("hello")->getFoo();
}',
'error_message' => 'UnusedMethodCall',
],
];
}
}

0 comments on commit d891217

Please sign in to comment.
You can’t perform that action at this time.