1--TEST--
2Test sprintf() function : Rope Optimization
3--FILE--
4<?php
5function func($str) {
6	return strtoupper($str);
7}
8function sideeffect() {
9	echo "Called!\n";
10	return "foo";
11}
12class Foo {
13	public function __construct() {
14		echo "Called\n";
15	}
16}
17
18$a = "foo";
19$b = "bar";
20$c = new stdClass();
21
22try {
23	var_dump(sprintf("const"));
24} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
25
26try {
27	var_dump(sprintf("%s", $a));
28} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
29
30try {
31	var_dump(sprintf("%s/%s", $a, $b));
32} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
33
34try {
35	var_dump(sprintf("%s/%s/%s", $a, $b));
36} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
37
38try {
39	var_dump(sprintf("%s/%s/%s", $a, $b, $c));
40} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
41
42try {
43	var_dump(sprintf("%s/", func("baz")));
44} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
45
46try {
47	var_dump(sprintf("/%s", func("baz")));
48} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
49
50try {
51	var_dump(sprintf("/%s/", func("baz")));
52} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
53
54try {
55	var_dump(sprintf("%s%s%s%s", $a, $b, func("baz"), $a));
56} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
57
58try {
59	var_dump(sprintf("%s/%s", sprintf("%s:%s", $a, $b), sprintf("%s-%s", func('baz'), func('baz'))));
60} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
61
62try {
63	var_dump(sprintf(sideeffect()));
64} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
65
66try {
67	var_dump(sprintf("%s-%s-%s", __FILE__, __LINE__, 1));
68} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
69
70try {
71	$values = range('a', 'z');
72	var_dump(sprintf("%s%s%s", "{$values[0]}{$values[1]}{$values[2]}", "{$values[3]}{$values[4]}{$values[5]}", "{$values[6]}{$values[7]}{$values[8]}"));
73} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
74
75try {
76	var_dump(sprintf("%s%s%s", new Foo(), new Foo(), new Foo(), ));
77} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
78
79try {
80	var_dump(sprintf(...));
81} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
82
83try {
84	var_dump(sprintf('%%s'));
85} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
86
87try {
88	var_dump(sprintf('%%s', 'test'));
89} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
90
91try {
92	var_dump(sprintf('%s-%s-%s', [], [], []));
93} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
94
95try {
96	var_dump(sprintf(""));
97} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
98
99try {
100	var_dump(sprintf());
101} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
102
103try {
104	var_dump(sprintf('%s-%s-%s', true, false, true));
105} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
106
107echo "Done";
108?>
109--EXPECTF--
110string(5) "const"
111
112string(3) "foo"
113
114string(7) "foo/bar"
115
116ArgumentCountError: 4 arguments are required, 3 given in %s:32
117Stack trace:
118#0 %s(32): sprintf('%s/%s/%s', 'foo', 'bar')
119#1 {main}
120
121Error: Object of class stdClass could not be converted to string in %s:36
122Stack trace:
123#0 {main}
124
125string(4) "BAZ/"
126
127string(4) "/BAZ"
128
129string(5) "/BAZ/"
130
131string(12) "foobarBAZfoo"
132
133string(15) "foo:bar/BAZ-BAZ"
134
135Called!
136string(3) "foo"
137
138string(%d) "%ssprintf_rope_optimization_001.php-%d-1"
139
140string(9) "abcdefghi"
141
142Called
143Called
144Called
145Error: Object of class Foo could not be converted to string in %s:73
146Stack trace:
147#0 {main}
148
149object(Closure)#3 (2) {
150  ["function"]=>
151  string(7) "sprintf"
152  ["parameter"]=>
153  array(2) {
154    ["$format"]=>
155    string(10) "<required>"
156    ["$values"]=>
157    string(10) "<optional>"
158  }
159}
160
161string(2) "%s"
162
163string(2) "%s"
164
165
166Warning: Array to string conversion in %s on line 89
167
168Warning: Array to string conversion in %s on line 89
169
170Warning: Array to string conversion in %s on line 89
171string(17) "Array-Array-Array"
172
173string(0) ""
174
175ArgumentCountError: sprintf() expects at least 1 argument, 0 given in %s:97
176Stack trace:
177#0 %s(97): sprintf()
178#1 {main}
179
180string(4) "1--1"
181
182Done
183