Skip to content
Permalink
Browse files

Allow mutation-free method calls in pure functions

  • Loading branch information...
muglug committed Sep 1, 2019
1 parent dee2cf3 commit 0279c6f6d9bbe22854526c45eaf1f5482c12ade3
@@ -1205,10 +1205,13 @@ function (PhpParser\Node\Arg $arg) {
&& (!empty($stmt->var->inferredType->external_mutation_free)
|| isset($stmt->var->pure));
if ($context->pure && !$method_storage->pure && !$method_pure_compatible) {
if ($context->pure
&& !$method_storage->mutation_free
&& !$method_pure_compatible
) {
if (IssueBuffer::accepts(
new ImpureMethodCall(
'Cannot call an impure method ' . $method_id . ' from a pure context',
'Cannot call an mutation-free method ' . $method_id . ' from a pure context',
new CodeLocation($source, $stmt->name)
),
$statements_analyzer->getSuppressedIssues()
@@ -58,6 +58,41 @@ public function getHelloA() : string {
}
}',
],
'addToCart' => [
'<?php
/** @psalm-immutable */
class Cart {
/** @var CartItem[] */
public array $items;
/** @param CartItem[] $items */
public function __construct(array $items) {
$this->items = $items;
}
public function addItem(CartItem $item) : self {
$items = $this->items;
$items[] = $item;
return new Cart($items);
}
}
/** @psalm-immutable */
class CartItem {
public string $name;
public float $price;
public function __construct(string $name, float $price) {
$this->name = $name;
$this->price = $price;
}
}
/** @psalm-pure */
function addItemToCart(Cart $c, string $name, float $price) : Cart {
return $c->addItem(new CartItem($name, $price));
}',
],
];
}

0 comments on commit 0279c6f

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