Skip to content

Commit ccd9da0

Browse files
committed
Convert some SPL Exceptions to normal Errors in php_spl.c
1 parent 98ad4d3 commit ccd9da0

File tree

5 files changed

+62
-49
lines changed

5 files changed

+62
-49
lines changed

ext/spl/php_spl.c

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -523,50 +523,76 @@ 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+
533+
if (do_throw) {
534+
RETURN_THROWS();
535+
} else {
536+
RETURN_FALSE;
537+
}
533538
} 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);
539+
zend_type_error("Passed array does not specify %s %smethod (%s)",
540+
alfi.func_ptr ? "a callable" : "an existing", !obj_ptr ? "static " : "", error);
535541
}
536542
if (error) {
537543
efree(error);
538544
}
539545
zend_string_release_ex(func_name, 0);
540-
RETURN_FALSE;
546+
547+
if (do_throw) {
548+
RETURN_THROWS();
549+
} else {
550+
RETURN_FALSE;
551+
}
541552
} else if (Z_TYPE_P(zcallable) == IS_STRING) {
542553
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);
554+
zend_type_error("Function '%s' not %s (%s)", ZSTR_VAL(func_name), alfi.func_ptr ? "callable" : "found", error);
544555
}
545556
if (error) {
546557
efree(error);
547558
}
548559
zend_string_release_ex(func_name, 0);
549-
RETURN_FALSE;
560+
561+
if (do_throw) {
562+
RETURN_THROWS();
563+
} else {
564+
RETURN_FALSE;
565+
}
550566
} else {
551567
if (do_throw) {
552-
zend_throw_exception_ex(spl_ce_LogicException, 0, "Illegal value passed (%s)", error);
568+
zend_type_error("Expected callable as first argument (%s)", error);
553569
}
554570
if (error) {
555571
efree(error);
556572
}
557573
zend_string_release_ex(func_name, 0);
558-
RETURN_FALSE;
574+
575+
if (do_throw) {
576+
RETURN_THROWS();
577+
} else {
578+
RETURN_FALSE;
579+
}
559580
}
560581
} else if (fcc.function_handler->type == ZEND_INTERNAL_FUNCTION &&
561582
fcc.function_handler->internal_function.handler == zif_spl_autoload_call) {
562583
if (do_throw) {
563-
zend_throw_exception_ex(spl_ce_LogicException, 0, "Function spl_autoload_call() cannot be registered");
584+
zend_throw_error(NULL, "Function spl_autoload_call() cannot be registered");
564585
}
565586
if (error) {
566587
efree(error);
567588
}
568589
zend_string_release_ex(func_name, 0);
569-
RETURN_FALSE;
590+
591+
if (do_throw) {
592+
RETURN_THROWS();
593+
} else {
594+
RETURN_FALSE;
595+
}
570596
}
571597
alfi.ce = fcc.calling_scope;
572598
alfi.func_ptr = fcc.function_handler;
@@ -688,14 +714,14 @@ PHP_FUNCTION(spl_autoload_unregister)
688714
}
689715

690716
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);
717+
zend_throw_error(NULL, "Unable to unregister invalid function (%s)", error);
692718
if (error) {
693719
efree(error);
694720
}
695721
if (func_name) {
696722
zend_string_release_ex(func_name, 0);
697723
}
698-
RETURN_FALSE;
724+
RETURN_THROWS();
699725
}
700726
obj_ptr = fcc.object;
701727
if (error) {

ext/spl/tests/spl_autoload_001.phpt

Lines changed: 4 additions & 7 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-
{
67+
try {
6968
spl_autoload_register("unavailable_autoload_function");
70-
}
71-
catch(Exception $e)
72-
{
73-
echo 'Exception: ' . $e->getMessage() . "\n";
69+
} catch (\TypeError $e) {
70+
echo $e->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: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@ class MyAutoLoader {
1919
}
2020
}
2121

22-
try
23-
{
24-
spl_autoload_register(array('MyAutoLoader', 'autoLoad'), true);
25-
}
26-
catch(Exception $e)
27-
{
28-
echo 'Exception: ' . $e->getMessage() . "\n";
22+
try {
23+
spl_autoload_register(array('MyAutoLoader', 'autoLoad'), true);
24+
} catch (\TypeError $ex) {
25+
echo $e->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: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,13 @@ $funcs = array(
3939

4040
foreach($funcs as $idx => $func)
4141
{
42-
if ($idx) echo "\n";
43-
try
44-
{
42+
if ($idx) echo "\n";
43+
try {
4544
var_dump($func);
4645
spl_autoload_register($func);
4746
echo "ok\n";
48-
}
49-
catch (Exception $e)
50-
{
51-
echo $e->getMessage() . "\n";
47+
} catch (\TypeError $e) {
48+
echo $e->getMessage() . PHP_EOL;
5249
}
5350
}
5451

ext/spl/tests/spl_autoload_008.phpt

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

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

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

6157
spl_autoload_unregister($func);
@@ -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)