Skip to content

Commit ed26a6e

Browse files
committed
Promote warnings to exceptions in settype() function
1 parent bfae722 commit ed26a6e

File tree

4 files changed

+45
-110
lines changed

4 files changed

+45
-110
lines changed

ext/standard/tests/general_functions/gettype_settype_basic.phpt

Lines changed: 25 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,19 @@ foreach ($types as $type) {
9393
$loop_count = 1;
9494
foreach ($values as $var) {
9595
echo "-- Iteration $loop_count --\n"; $loop_count ++;
96-
// set to new type
97-
var_dump( settype($var, $type) );
9896

99-
// dump the var
100-
var_dump( $var );
97+
try {
98+
// set to new type
99+
var_dump( settype($var, $type) );
101100

102-
// check the new type
103-
var_dump( gettype($var) );
101+
// dump the var
102+
var_dump( $var );
103+
104+
// check the new type
105+
var_dump( gettype($var) );
106+
} catch (ValueError $exception) {
107+
echo $exception->getMessage() . "\n";
108+
}
104109
}
105110
}
106111

@@ -539,94 +544,33 @@ string(7) "boolean"
539544

540545
-- Setting type of data to resource --
541546
-- Iteration 1 --
542-
2: settype(): Cannot convert to resource type
543-
bool(false)
544-
array(3) {
545-
[0]=>
546-
int(1)
547-
[1]=>
548-
int(2)
549-
[2]=>
550-
int(3)
551-
}
552-
string(5) "array"
547+
Cannot convert to resource type
553548
-- Iteration 2 --
554-
2: settype(): Cannot convert to resource type
555-
bool(false)
556-
string(14) "another string"
557-
string(6) "string"
549+
Cannot convert to resource type
558550
-- Iteration 3 --
559-
2: settype(): Cannot convert to resource type
560-
bool(false)
561-
array(3) {
562-
[0]=>
563-
int(2)
564-
[1]=>
565-
int(3)
566-
[2]=>
567-
int(4)
568-
}
569-
string(5) "array"
551+
Cannot convert to resource type
570552
-- Iteration 4 --
571-
2: settype(): Cannot convert to resource type
572-
bool(false)
573-
int(1)
574-
string(7) "integer"
553+
Cannot convert to resource type
575554
-- Iteration 5 --
576-
2: settype(): Cannot convert to resource type
577-
bool(false)
578-
int(-20)
579-
string(7) "integer"
555+
Cannot convert to resource type
580556
-- Iteration 6 --
581-
2: settype(): Cannot convert to resource type
582-
bool(false)
583-
float(2.54)
584-
string(6) "double"
557+
Cannot convert to resource type
585558
-- Iteration 7 --
586-
2: settype(): Cannot convert to resource type
587-
bool(false)
588-
float(-2.54)
589-
string(6) "double"
559+
Cannot convert to resource type
590560
-- Iteration 8 --
591-
2: settype(): Cannot convert to resource type
592-
bool(false)
593-
NULL
594-
string(4) "NULL"
561+
Cannot convert to resource type
595562
-- Iteration 9 --
596-
2: settype(): Cannot convert to resource type
597-
bool(false)
598-
bool(false)
599-
string(7) "boolean"
563+
Cannot convert to resource type
600564
-- Iteration 10 --
601-
2: settype(): Cannot convert to resource type
602-
bool(false)
603-
string(11) "some string"
604-
string(6) "string"
565+
Cannot convert to resource type
605566
-- Iteration 11 --
606-
2: settype(): Cannot convert to resource type
607-
bool(false)
608-
string(6) "string"
609-
string(6) "string"
567+
Cannot convert to resource type
610568
-- Iteration 12 --
611-
2: settype(): Cannot convert to resource type
612-
bool(false)
613-
resource(%d) of type (stream)
614-
string(8) "resource"
569+
Cannot convert to resource type
615570
-- Iteration 13 --
616-
2: settype(): Cannot convert to resource type
617-
bool(false)
618-
resource(%d) of type (stream)
619-
string(8) "resource"
571+
Cannot convert to resource type
620572
-- Iteration 14 --
621-
2: settype(): Cannot convert to resource type
622-
bool(false)
623-
object(point)#1 (2) {
624-
["x"]=>
625-
int(10)
626-
["y"]=>
627-
int(20)
628-
}
629-
string(6) "object"
573+
Cannot convert to resource type
630574

631575
-- Setting type of data to array --
632576
-- Iteration 1 --

ext/standard/tests/general_functions/gettype_settype_error.phpt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ echo "**** Testing gettype() and settype() functions ****\n";
1616
echo "\n*** Testing settype(): error conditions ***\n";
1717

1818
// passing an invalid type to set
19-
var_dump( settype( $var, "unknown" ) );
19+
try {
20+
settype( $var, "unknown" );
21+
} catch (ValueError $exception) {
22+
echo $exception->getMessage() . "\n";
23+
}
2024

2125
echo "Done\n";
2226
?>
2327
--EXPECTF--
2428
**** Testing gettype() and settype() functions ****
2529

2630
*** Testing settype(): error conditions ***
27-
28-
Warning: settype(): Invalid type in %s on line %d
29-
bool(false)
31+
Invalid type
3032
Done

ext/standard/tests/general_functions/type.phpt

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ bool(true)
160160
bool(true)
161161
bool(true)
162162
bool(true)
163-
string(42) "settype(): Cannot convert to resource type"
164-
bool(false)
163+
Error: Cannot convert to resource type
165164
array(3) {
166165
[0]=>
167166
int(1)
@@ -170,11 +169,9 @@ array(3) {
170169
[2]=>
171170
int(3)
172171
}
173-
string(42) "settype(): Cannot convert to resource type"
174-
bool(false)
172+
Error: Cannot convert to resource type
175173
string(14) "another string"
176-
string(42) "settype(): Cannot convert to resource type"
177-
bool(false)
174+
Error: Cannot convert to resource type
178175
array(3) {
179176
[0]=>
180177
int(2)
@@ -183,29 +180,21 @@ array(3) {
183180
[2]=>
184181
int(4)
185182
}
186-
string(42) "settype(): Cannot convert to resource type"
187-
bool(false)
183+
Error: Cannot convert to resource type
188184
int(1)
189-
string(42) "settype(): Cannot convert to resource type"
190-
bool(false)
185+
Error: Cannot convert to resource type
191186
float(2)
192-
string(42) "settype(): Cannot convert to resource type"
193-
bool(false)
187+
Error: Cannot convert to resource type
194188
NULL
195-
string(42) "settype(): Cannot convert to resource type"
196-
bool(false)
197-
bool(false)
198-
string(42) "settype(): Cannot convert to resource type"
189+
Error: Cannot convert to resource type
199190
bool(false)
191+
Error: Cannot convert to resource type
200192
string(11) "some string"
201-
string(42) "settype(): Cannot convert to resource type"
202-
bool(false)
193+
Error: Cannot convert to resource type
203194
resource(%d) of type (Unknown)
204-
string(42) "settype(): Cannot convert to resource type"
205-
bool(false)
195+
Error: Cannot convert to resource type
206196
resource(%d) of type (stream)
207-
string(42) "settype(): Cannot convert to resource type"
208-
bool(false)
197+
Error: Cannot convert to resource type
209198
object(stdClass)#%d (0) {
210199
}
211200
bool(true)

ext/standard/type.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ PHP_FUNCTION(settype)
8282
zval_ptr_dtor(&tmp);
8383
}
8484
if (zend_string_equals_literal_ci(type, "resource")) {
85-
php_error_docref(NULL, E_WARNING, "Cannot convert to resource type");
85+
zend_value_error("Cannot convert to resource type");
8686
} else {
87-
php_error_docref(NULL, E_WARNING, "Invalid type");
87+
zend_value_error("Invalid type");
8888
}
89-
RETURN_FALSE;
89+
return;
9090
}
9191

9292
if (ptr == &tmp) {

0 commit comments

Comments
 (0)