|
| 1 | +--TEST-- |
| 2 | +preg_replace_callback() with a trampoline |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | + |
| 6 | +class TrampolineTest { |
| 7 | + public function __call(string $name, array $arguments): string { |
| 8 | + echo 'Trampoline for ', $name, PHP_EOL; |
| 9 | + if ($name === 'trampolineThrow') { |
| 10 | + throw new Exception('boo'); |
| 11 | + } |
| 12 | + return "'" . $arguments[0][0] . "'"; |
| 13 | + } |
| 14 | +} |
| 15 | +$o = new TrampolineTest(); |
| 16 | +$callback = [$o, 'trampoline']; |
| 17 | +$callbackThrow = [$o, 'trampolineThrow']; |
| 18 | + |
| 19 | +$regexesToTest = [ |
| 20 | + [ |
| 21 | + '@\b\w{1,2}\b@', |
| 22 | + '~\A.~', |
| 23 | + ], |
| 24 | + '@\b\w{1,2}\b@', |
| 25 | + [ |
| 26 | + new stdClass(), |
| 27 | + ], |
| 28 | +]; |
| 29 | + |
| 30 | +$subjectsToTest = [ |
| 31 | + [ |
| 32 | + 'a b3 bcd', |
| 33 | + 'v' => 'aksfjk', |
| 34 | + 12 => 'aa bb', |
| 35 | + ['xyz'], |
| 36 | + ], |
| 37 | + 'a b3 bcd', |
| 38 | + [ |
| 39 | + new stdClass(), |
| 40 | + ], |
| 41 | + new stdClass(), |
| 42 | +]; |
| 43 | + |
| 44 | +foreach ([$callback, $callbackThrow] as $fn) { |
| 45 | + foreach ($regexesToTest as $regex) { |
| 46 | + foreach ($subjectsToTest as $subject) { |
| 47 | + try { |
| 48 | + $matches = preg_replace_callback($regex, $fn, $subject); |
| 49 | + var_dump($matches); |
| 50 | + } catch (Throwable $e) { |
| 51 | + echo $e::class, ': ', $e->getMessage(), PHP_EOL; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +?> |
| 58 | +--EXPECTF-- |
| 59 | +Trampoline for trampoline |
| 60 | +Trampoline for trampoline |
| 61 | +Trampoline for trampoline |
| 62 | +Trampoline for trampoline |
| 63 | +Trampoline for trampoline |
| 64 | +Trampoline for trampoline |
| 65 | +Trampoline for trampoline |
| 66 | + |
| 67 | +Warning: Array to string conversion in %s on line %d |
| 68 | +Trampoline for trampoline |
| 69 | +array(4) { |
| 70 | + [0]=> |
| 71 | + string(14) "'''a' 'b3' bcd" |
| 72 | + ["v"]=> |
| 73 | + string(8) "'a'ksfjk" |
| 74 | + [12]=> |
| 75 | + string(11) "'''aa' 'bb'" |
| 76 | + [13]=> |
| 77 | + string(7) "'A'rray" |
| 78 | +} |
| 79 | +Trampoline for trampoline |
| 80 | +Trampoline for trampoline |
| 81 | +Trampoline for trampoline |
| 82 | +string(14) "'''a' 'b3' bcd" |
| 83 | +Error: Object of class stdClass could not be converted to string |
| 84 | +TypeError: preg_replace_callback(): Argument #3 ($subject) must be of type array|string, stdClass given |
| 85 | +Trampoline for trampoline |
| 86 | +Trampoline for trampoline |
| 87 | +Trampoline for trampoline |
| 88 | +Trampoline for trampoline |
| 89 | + |
| 90 | +Warning: Array to string conversion in %s on line %d |
| 91 | +array(4) { |
| 92 | + [0]=> |
| 93 | + string(12) "'a' 'b3' bcd" |
| 94 | + ["v"]=> |
| 95 | + string(6) "aksfjk" |
| 96 | + [12]=> |
| 97 | + string(9) "'aa' 'bb'" |
| 98 | + [13]=> |
| 99 | + string(5) "Array" |
| 100 | +} |
| 101 | +Trampoline for trampoline |
| 102 | +Trampoline for trampoline |
| 103 | +string(12) "'a' 'b3' bcd" |
| 104 | +Error: Object of class stdClass could not be converted to string |
| 105 | +TypeError: preg_replace_callback(): Argument #3 ($subject) must be of type array|string, stdClass given |
| 106 | + |
| 107 | +Warning: Array to string conversion in %s on line %d |
| 108 | +Error: Object of class stdClass could not be converted to string |
| 109 | +Error: Object of class stdClass could not be converted to string |
| 110 | +Error: Object of class stdClass could not be converted to string |
| 111 | +TypeError: preg_replace_callback(): Argument #3 ($subject) must be of type array|string, stdClass given |
| 112 | +Trampoline for trampolineThrow |
| 113 | + |
| 114 | +Warning: Array to string conversion in %s on line %d |
| 115 | +Exception: boo |
| 116 | +Trampoline for trampolineThrow |
| 117 | +Exception: boo |
| 118 | +Error: Object of class stdClass could not be converted to string |
| 119 | +TypeError: preg_replace_callback(): Argument #3 ($subject) must be of type array|string, stdClass given |
| 120 | +Trampoline for trampolineThrow |
| 121 | + |
| 122 | +Warning: Array to string conversion in %s on line %d |
| 123 | +Exception: boo |
| 124 | +Trampoline for trampolineThrow |
| 125 | +Exception: boo |
| 126 | +Error: Object of class stdClass could not be converted to string |
| 127 | +TypeError: preg_replace_callback(): Argument #3 ($subject) must be of type array|string, stdClass given |
| 128 | + |
| 129 | +Warning: Array to string conversion in %s on line %d |
| 130 | +Error: Object of class stdClass could not be converted to string |
| 131 | +Error: Object of class stdClass could not be converted to string |
| 132 | +Error: Object of class stdClass could not be converted to string |
| 133 | +TypeError: preg_replace_callback(): Argument #3 ($subject) must be of type array|string, stdClass given |
0 commit comments