|
| 1 | +<?php declare(strict_types = 1); // lint >= 8.0 |
| 2 | + |
| 3 | +namespace Bug15066Variables; |
| 4 | + |
| 5 | +function variadicByRef(string|null &...$refs): void |
| 6 | +{ |
| 7 | + foreach ($refs as &$ref) { |
| 8 | + $ref = $ref === null ? null : trim($ref); |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +function variadicByIndex(string|null &...$refs): void |
| 13 | +{ |
| 14 | + foreach ($refs as $key => $value) { |
| 15 | + $refs[$key] = $value === null ? null : trim($value); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +function variadicWrongType(string|null &...$refs): void |
| 20 | +{ |
| 21 | + foreach ($refs as $key => $value) { |
| 22 | + $refs[$key] = 42; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +/** @param-out string|null $refs */ |
| 27 | +function variadicParamOut(string|null &...$refs): void |
| 28 | +{ |
| 29 | + foreach ($refs as $key => $value) { |
| 30 | + $refs[$key] = $value === null ? null : trim($value); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/** @param-out string $refs */ |
| 35 | +function variadicParamOutNeverWritten(string|null &...$refs): void |
| 36 | +{ |
| 37 | +} |
| 38 | + |
| 39 | +class Foo |
| 40 | +{ |
| 41 | + |
| 42 | + public function variadicByIndex(string|null &...$refs): void |
| 43 | + { |
| 44 | + foreach ($refs as $key => $value) { |
| 45 | + $refs[$key] = $value === null ? null : trim($value); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public function variadicWrongType(string|null &...$refs): void |
| 50 | + { |
| 51 | + foreach ($refs as $key => $value) { |
| 52 | + $refs[$key] = 42; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | +// Rebinding the packed variable discards the references it held, so PHP writes nothing back to any |
| 59 | +// caller. Nothing is reported for a non-array, because no out value is left to check. An array is |
| 60 | +// still reported: a write through an offset reaches the caller and leaves the variable as an array |
| 61 | +// too, so the two cannot be told apart here, and the offset write is the case that matters. |
| 62 | +function variadicRebindNonArray(string|null &...$refs): void |
| 63 | +{ |
| 64 | + $refs = 42; |
| 65 | +} |
| 66 | + |
| 67 | +function variadicRebindWrongArray(string|null &...$refs): void |
| 68 | +{ |
| 69 | + $refs = [42]; |
| 70 | +} |
| 71 | + |
| 72 | +function variadicRebindOkArray(string|null &...$refs): void |
| 73 | +{ |
| 74 | + $refs = ['ok']; |
| 75 | +} |
| 76 | + |
| 77 | +// The packed variable may end up only maybe holding an array. There is then no element type to |
| 78 | +// speak of, so the comparison is skipped rather than run against a nonexistent one. |
| 79 | +function variadicRebindMaybeArray(string|null &...$refs): void |
| 80 | +{ |
| 81 | + $refs = rand(0, 1) === 1 ? [42] : null; |
| 82 | +} |
| 83 | + |
0 commit comments