|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PureUnlessAllMethodsPure; |
| 4 | + |
| 5 | +/** |
| 6 | + * @phpstan-all-methods-pure |
| 7 | + */ |
| 8 | +class Replacer |
| 9 | +{ |
| 10 | + |
| 11 | + /** |
| 12 | + * @param-out int $count |
| 13 | + * @pure-unless-parameter-passed $count |
| 14 | + */ |
| 15 | + public function replace(string $subject, int &$count = 0): string |
| 16 | + { |
| 17 | + $count = 1; |
| 18 | + |
| 19 | + return $subject; |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * @param callable(string): string $cb |
| 24 | + * @pure-unless-callable-is-impure $cb |
| 25 | + */ |
| 26 | + public function map(callable $cb, string $subject): string |
| 27 | + { |
| 28 | + return $cb($subject); |
| 29 | + } |
| 30 | + |
| 31 | +} |
| 32 | + |
| 33 | +interface ReplacerInterface |
| 34 | +{ |
| 35 | + |
| 36 | + /** |
| 37 | + * @param-out int $count |
| 38 | + * @pure-unless-parameter-passed $count |
| 39 | + */ |
| 40 | + public function replace(string $subject, int &$count = 0): string; |
| 41 | + |
| 42 | + /** |
| 43 | + * @param callable(string): string $cb |
| 44 | + * @pure-unless-callable-is-impure $cb |
| 45 | + */ |
| 46 | + public function map(callable $cb, string $subject): string; |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * @phpstan-all-methods-pure |
| 52 | + */ |
| 53 | +class InheritingReplacer implements ReplacerInterface |
| 54 | +{ |
| 55 | + |
| 56 | + public function replace(string $subject, int &$count = 0): string |
| 57 | + { |
| 58 | + $count = 1; |
| 59 | + |
| 60 | + return $subject; |
| 61 | + } |
| 62 | + |
| 63 | + public function map(callable $cb, string $subject): string |
| 64 | + { |
| 65 | + return $cb($subject); |
| 66 | + } |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * @phpstan-all-methods-impure |
| 72 | + */ |
| 73 | +class ImpureClassInheritingReplacer implements ReplacerInterface |
| 74 | +{ |
| 75 | + |
| 76 | + public function replace(string $subject, int &$count = 0): string |
| 77 | + { |
| 78 | + echo $subject; |
| 79 | + $count = 1; |
| 80 | + |
| 81 | + return $subject; |
| 82 | + } |
| 83 | + |
| 84 | + public function map(callable $cb, string $subject): string |
| 85 | + { |
| 86 | + echo $subject; |
| 87 | + |
| 88 | + return $cb($subject); |
| 89 | + } |
| 90 | + |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * @phpstan-pure |
| 95 | + */ |
| 96 | +function omittingCount(Replacer $r, InheritingReplacer $i, string $s): string |
| 97 | +{ |
| 98 | + // $count is omitted, so the calls stay pure. |
| 99 | + return $r->replace($s) . $i->replace($s); |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * @phpstan-pure |
| 104 | + */ |
| 105 | +function passingCount(Replacer $r, InheritingReplacer $i, string $s): string |
| 106 | +{ |
| 107 | + $c = 0; |
| 108 | + $d = 0; |
| 109 | + // $count is passed, so the calls are impure although the classes are marked |
| 110 | + // @phpstan-all-methods-pure. |
| 111 | + return $r->replace($s, $c) . $i->replace($s, $d); |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * @phpstan-pure |
| 116 | + */ |
| 117 | +function pureCallback(Replacer $r, InheritingReplacer $i, string $s): string |
| 118 | +{ |
| 119 | + return $r->map(static fn (string $x): string => $x, $s) . $i->map(static fn (string $x): string => $x, $s); |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * @phpstan-pure |
| 124 | + */ |
| 125 | +function impureCallback(Replacer $r, InheritingReplacer $i, string $s): string |
| 126 | +{ |
| 127 | + // The callback is impure, so the calls are impure although the classes are |
| 128 | + // marked @phpstan-all-methods-pure. |
| 129 | + $cb = static function (string $x): string { |
| 130 | + echo $x; |
| 131 | + |
| 132 | + return $x; |
| 133 | + }; |
| 134 | + |
| 135 | + return $r->map($cb, $s) . $i->map($cb, $s); |
| 136 | +} |
0 commit comments