Skip to editor
Psalm
Docs
Playground
Plugins
Blog
Paid support
Share
Fix issues
Show original
PHP
Settings
<?php // Psalm 7 purity: declare which side effects a function may have, and // Psalm reports the hidden ones, including those hidden in callbacks. /** @psalm-immutable */ final class Item { public function __construct( public string $name, public int $cents, ) {} } final class Cart { /** @param list<Item> $items */ public function __construct( public array $items = [], public ?string $coupon = null, ) {} } // ---- Capabilities: exactly which side effects are allowed ---- // Bug: a "pure" price that depends on the clock. Cache it or test it, // and it gives Friday's answer on Monday. /** @psalm-pure */ function discount(int $cents): int { return date('N') === '5' ? intdiv($cents, 10) : 0; // ImpureFunctionCall: date() requires time } // Fixed: reading the clock is part of the contract, and callers see it /** @psalm-capabilities time */ function fridayDiscount(int $cents): int { return date('N') === '5' ? intdiv($cents, 10) : 0; // ok } // Bug: a check that quietly rewrites the caller's cart /** @psalm-capabilities read-props */ function hasValidCoupon(Cart $cart): bool { if ($cart->coupon !== null) { $cart->coupon = strtoupper($cart->coupon); // ImpurePropertyAssignment: requires write-props } return $cart->coupon === 'SAVE10'; } // ---- Purity templates: as pure as what you pass in ---- // array_map() is exactly as pure as its callback /** * @psalm-pure * @param list<Item> $items * @return list<int> */ function prices(array $items): array { return array_map(fn(Item $item): int => $item->cents, $items); // ok } // Bug: logging hidden in the callback of a pure function /** * @psalm-pure * @param list<Item> $items * @return list<int> */ function loggedPrices(array $items): array { return array_map(function (Item $item): int { // ImpureFunctionCall: array_map() requires io error_log("pricing {$item->name}"); return $item->cents; }, $items); } // Your own interfaces too: each implementation declares what it does /** @psalm-purity-template P */ interface PriceRule { /** * @psalm-pure * @psalm-purity-from-template P */ public function apply(int $cents): int; } /** @implements PriceRule[pure] */ final class TenPercentOff implements PriceRule { /** @psalm-pure */ public function apply(int $cents): int { return $cents - intdiv($cents, 10); } } /** @implements PriceRule[time] */ final class HappyHour implements PriceRule { /** @psalm-capabilities time */ public function apply(int $cents): int { return date('G') === '17' ? intdiv($cents, 2) : $cents; } } // Bug: a price cache that accepts a rule whose answer changes every hour /** * @psalm-pure * @param PriceRule[pure] $rule */ function cachedPrice(PriceRule $rule, int $cents): int { return $rule->apply($cents); } echo cachedPrice(new TenPercentOff(), 1000); // ok echo cachedPrice(new HappyHour(), 1000); // InvalidArgument: HappyHour is a PriceRule[time]
Issues
Types
idle
Saved Nov 2 2026 18:47 UTC
⌘/Ctrl+Enter
run
⌘/Ctrl+S
share
F8
/
⇧F8
next/prev issue