Skip to content

Commit bd0219c

Browse files
committed
Convert some SPL Exceptions to normal Errors in php_spl.c
1 parent 30168d8 commit bd0219c

File tree

5 files changed

+35
-47
lines changed

5 files changed

+35
-47
lines changed

ext/spl/php_spl.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -523,44 +523,45 @@ PHP_FUNCTION(spl_autoload_register)
523523
if (Z_TYPE_P(zcallable) == IS_ARRAY) {
524524
if (!obj_ptr && alfi.func_ptr && !(alfi.func_ptr->common.fn_flags & ZEND_ACC_STATIC)) {
525525
if (do_throw) {
526-
zend_throw_exception_ex(spl_ce_LogicException, 0, "Passed array specifies a non static method but no object (%s)", error);
526+
zend_type_error("Passed array specifies a non static method but no object (%s)", error);
527527
}
528528
if (error) {
529529
efree(error);
530530
}
531531
zend_string_release_ex(func_name, 0);
532-
RETURN_FALSE;
532+
return;
533533
} else if (do_throw) {
534-
zend_throw_exception_ex(spl_ce_LogicException, 0, "Passed array does not specify %s %smethod (%s)", alfi.func_ptr ? "a callable" : "an existing", !obj_ptr ? "static " : "", error);
534+
zend_type_error("Passed array does not specify %s %smethod (%s)",
535+
alfi.func_ptr ? "a callable" : "an existing", !obj_ptr ? "static " : "", error);
535536
}
536537
if (error) {
537538
efree(error);
538539
}
539540
zend_string_release_ex(func_name, 0);
540-
RETURN_FALSE;
541+
return;
541542
} else if (Z_TYPE_P(zcallable) == IS_STRING) {
542543
if (do_throw) {
543-
zend_throw_exception_ex(spl_ce_LogicException, 0, "Function '%s' not %s (%s)", ZSTR_VAL(func_name), alfi.func_ptr ? "callable" : "found", error);
544+
zend_type_error("Function '%s' not %s (%s)", ZSTR_VAL(func_name), alfi.func_ptr ? "callable" : "found", error);
544545
}
545546
if (error) {
546547
efree(error);
547548
}
548549
zend_string_release_ex(func_name, 0);
549-
RETURN_FALSE;
550+
return;
550551
} else {
551552
if (do_throw) {
552-
zend_throw_exception_ex(spl_ce_LogicException, 0, "Illegal value passed (%s)", error);
553+
zend_type_error("Expected callable as first argument (%s)", error);
553554
}
554555
if (error) {
555556
efree(error);
556557
}
557558
zend_string_release_ex(func_name, 0);
558-
RETURN_FALSE;
559+
return;
559560
}
560561
} else if (fcc.function_handler->type == ZEND_INTERNAL_FUNCTION &&
561562
fcc.function_handler->internal_function.handler == zif_spl_autoload_call) {
562563
if (do_throw) {
563-
zend_throw_exception_ex(spl_ce_LogicException, 0, "Function spl_autoload_call() cannot be registered");
564+
zend_throw_error(NULL, "Function spl_autoload_call() cannot be registered");
564565
}
565566
if (error) {
566567
efree(error);
@@ -688,14 +689,14 @@ PHP_FUNCTION(spl_autoload_unregister)
688689
}
689690

690691
if (!zend_is_callable_ex(zcallable, NULL, IS_CALLABLE_CHECK_SYNTAX_ONLY, &func_name, &fcc, &error)) {
691-
zend_throw_exception_ex(spl_ce_LogicException, 0, "Unable to unregister invalid function (%s)", error);
692+
zend_throw_error(NULL, "Unable to unregister invalid function (%s)", error);
692693
if (error) {
693694
efree(error);
694695
}
695696
if (func_name) {
696697
zend_string_release_ex(func_name, 0);
697698
}
698-
RETURN_FALSE;
699+
return;
699700
}
700701
obj_ptr = fcc.object;
701702
if (error) {

ext/spl/tests/spl_autoload_001.phpt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,10 @@ var_dump(class_exists("TestClass", true));
6464

6565
echo "===NOFUNCTION===\n";
6666

67-
try
68-
{
69-
spl_autoload_register("unavailable_autoload_function");
70-
}
71-
catch(Exception $e)
72-
{
73-
echo 'Exception: ' . $e->getMessage() . "\n";
67+
try {
68+
spl_autoload_register("unavailable_autoload_function");
69+
} catch (\TypeError $ex) {
70+
echo $ex->getMessage() . PHP_EOL;
7471
}
7572

7673
?>
@@ -103,4 +100,4 @@ TestFunc2(TestClass)
103100
%stestclass.class.inc
104101
bool(true)
105102
===NOFUNCTION===
106-
Exception: Function 'unavailable_autoload_function' not found (function 'unavailable_autoload_function' not found or invalid function name)
103+
Function 'unavailable_autoload_function' not found (function 'unavailable_autoload_function' not found or invalid function name)

ext/spl/tests/spl_autoload_005.phpt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@ class MyAutoLoader {
1919
}
2020
}
2121

22-
try
23-
{
22+
try {
2423
spl_autoload_register(array('MyAutoLoader', 'autoLoad'), true);
25-
}
26-
catch(Exception $e)
27-
{
28-
echo 'Exception: ' . $e->getMessage() . "\n";
24+
} catch (\TypeError $ex) {
25+
echo $ex->getMessage() . PHP_EOL;
2926
}
3027

3128
// and
@@ -46,7 +43,7 @@ catch(Exception $e)
4643

4744
?>
4845
--EXPECT--
49-
Exception: Passed array specifies a non static method but no object (non-static method MyAutoLoader::autoLoad() cannot be called statically)
46+
Passed array specifies a non static method but no object (non-static method MyAutoLoader::autoLoad() cannot be called statically)
5047
MyAutoLoader::autoLoad(TestClass)
5148
MyAutoLoader::autoThrow(TestClass)
5249
Exception: Unavailable

ext/spl/tests/spl_autoload_007.phpt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,13 @@ $funcs = array(
4040
foreach($funcs as $idx => $func)
4141
{
4242
if ($idx) echo "\n";
43-
try
44-
{
45-
var_dump($func);
46-
spl_autoload_register($func);
47-
echo "ok\n";
48-
}
49-
catch (Exception $e)
50-
{
51-
echo $e->getMessage() . "\n";
52-
}
43+
try {
44+
var_dump($func);
45+
spl_autoload_register($func);
46+
echo "ok\n";
47+
} catch (\TypeError $ex) {
48+
echo $ex->getMessage() . PHP_EOL;
49+
}
5350
}
5451

5552
?>

ext/spl/tests/spl_autoload_008.phpt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,17 @@ foreach($funcs as $idx => $func)
4242
{
4343
echo "====$idx====\n";
4444

45-
try
46-
{
45+
try {
4746
var_dump($func);
4847
spl_autoload_register($func);
49-
if (count(spl_autoload_functions()))
50-
{
48+
if (count(spl_autoload_functions())) {
5149
echo "registered\n";
5250

5351
var_dump(class_exists("NoExistingTestClass", true));
5452
}
55-
}
56-
catch (Exception $e)
57-
{
58-
echo get_class($e) . ": " . $e->getMessage() . "\n";
59-
}
53+
} catch (\TypeError|\Exception $e) {
54+
echo get_class($e) . ': ' . $e->getMessage() . PHP_EOL;
55+
}
6056

6157
spl_autoload_unregister($func);
6258
var_dump(count(spl_autoload_functions()));
@@ -78,7 +74,7 @@ Exception: Bla
7874
int(0)
7975
====2====
8076
string(22) "MyAutoLoader::dynaLoad"
81-
LogicException: Function 'MyAutoLoader::dynaLoad' not callable (non-static method MyAutoLoader::dynaLoad() cannot be called statically)
77+
TypeError: Function 'MyAutoLoader::dynaLoad' not callable (non-static method MyAutoLoader::dynaLoad() cannot be called statically)
8278
int(0)
8379
====3====
8480
array(2) {
@@ -98,7 +94,7 @@ array(2) {
9894
[1]=>
9995
string(8) "dynaLoad"
10096
}
101-
LogicException: Passed array specifies a non static method but no object (non-static method MyAutoLoader::dynaLoad() cannot be called statically)
97+
TypeError: Passed array specifies a non static method but no object (non-static method MyAutoLoader::dynaLoad() cannot be called statically)
10298
int(0)
10399
====5====
104100
array(2) {

0 commit comments

Comments
 (0)