Skip to content

Commit 130ae2f

Browse files
committed
Promote warnings to error and use standard message for SPL DIR
1 parent 943ebfa commit 130ae2f

16 files changed

+143
-119
lines changed

ext/spl/spl_directory.c

Lines changed: 58 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ static inline void spl_filesystem_object_get_file_name(spl_filesystem_object *in
206206
case SPL_FS_INFO:
207207
case SPL_FS_FILE:
208208
if (!intern->file_name) {
209-
php_error_docref(NULL, E_ERROR, "Object not initialized");
209+
zend_throw_error(NULL, "Object not initialized");
210210
}
211211
break;
212212
case SPL_FS_DIR:
@@ -290,7 +290,7 @@ static int spl_filesystem_file_open(spl_filesystem_object *intern, int use_inclu
290290
if (Z_TYPE(tmp) == IS_TRUE) {
291291
intern->u.file.open_mode = NULL;
292292
intern->file_name = NULL;
293-
zend_throw_exception_ex(spl_ce_LogicException, 0, "Cannot use SplFileObject with directories");
293+
zend_throw_error(NULL, "Cannot use SplFileObject with directories");
294294
return FAILURE;
295295
}
296296

@@ -428,18 +428,20 @@ void spl_filesystem_info_set_filename(spl_filesystem_object *intern, char *path,
428428
intern->_path = estrndup(path, intern->_path_len);
429429
} /* }}} */
430430

431-
static spl_filesystem_object *spl_filesystem_object_create_info(spl_filesystem_object *source, char *file_path, size_t file_path_len, int use_copy, zend_class_entry *ce, zval *return_value) /* {{{ */
431+
static spl_filesystem_object *spl_filesystem_object_create_info(spl_filesystem_object *source,
432+
char *file_path, size_t file_path_len, int use_copy, zend_class_entry *ce, zval *return_value) /* {{{ */
432433
{
433434
spl_filesystem_object *intern;
434435
zval arg1;
435436
zend_error_handling error_handling;
436437

437438
if (!file_path || !file_path_len) {
438439
#if defined(PHP_WIN32)
439-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot create SplFileInfo for empty path");
440+
zend_value_error("Path cannot be empty");
440441
if (file_path && !use_copy) {
441442
efree(file_path);
442443
}
444+
return NULL;
443445
#else
444446
if (file_path && !use_copy) {
445447
efree(file_path);
@@ -714,18 +716,18 @@ void spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAMETERS, zend_long cto
714716
flags |= SPL_FILE_DIR_UNIXPATHS;
715717
}
716718
if (parsed == FAILURE) {
717-
return;
719+
RETURN_THROWS();
718720
}
719721

720722
if (!len) {
721-
zend_value_error("Directory name must not be empty");
723+
zend_argument_value_error(1, "cannot be empty");
722724
return;
723725
}
724726

725727
intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
726728
if (intern->_path) {
727729
/* object is already initialized */
728-
php_error_docref(NULL, E_WARNING, "Directory object is already initialized");
730+
zend_throw_error(NULL, "Directory object is already initialized");
729731
return;
730732
}
731733
intern->flags = flags;
@@ -850,7 +852,7 @@ SPL_METHOD(DirectoryIterator, seek)
850852
valid = zend_is_true(&retval);
851853
zval_ptr_dtor(&retval);
852854
if (!valid) {
853-
zend_value_error("Seek position " ZEND_LONG_FMT " is out of range", pos);
855+
zend_argument_value_error(1, "position " ZEND_LONG_FMT " is out of range", pos);
854856
RETURN_THROWS();
855857
}
856858
zend_call_method_with_0_params(Z_OBJ_P(ZEND_THIS), Z_OBJCE_P(ZEND_THIS), &intern->u.dir.func_next, "next", NULL);
@@ -1239,21 +1241,18 @@ SPL_METHOD(SplFileInfo, getLinkTarget)
12391241
spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
12401242
ssize_t ret;
12411243
char buff[MAXPATHLEN];
1242-
zend_error_handling error_handling;
12431244

12441245
if (zend_parse_parameters_none() == FAILURE) {
12451246
RETURN_THROWS();
12461247
}
12471248

1248-
zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
1249-
12501249
if (intern->file_name == NULL) {
12511250
spl_filesystem_object_get_file_name(intern);
12521251
}
12531252
#if defined(PHP_WIN32) || HAVE_SYMLINK
12541253
if (intern->file_name == NULL) {
1255-
php_error_docref(NULL, E_WARNING, "Empty filename");
1256-
RETURN_FALSE;
1254+
zend_value_error("Filename cannot be empty");
1255+
RETURN_THROWS();
12571256
} else if (!IS_ABSOLUTE_PATH(intern->file_name, intern->file_name_len)) {
12581257
char expanded_path[MAXPATHLEN];
12591258
if (!expand_filepath_with_mode(intern->file_name, expanded_path, NULL, 0, CWD_EXPAND )) {
@@ -1277,8 +1276,6 @@ SPL_METHOD(SplFileInfo, getLinkTarget)
12771276

12781277
RETVAL_STRINGL(buff, ret);
12791278
}
1280-
1281-
zend_restore_error_handling(&error_handling);
12821279
}
12831280
/* }}} */
12841281

@@ -1605,7 +1602,7 @@ SPL_METHOD(GlobIterator, count)
16051602
RETURN_LONG(php_glob_stream_get_count(intern->u.dir.dirp, NULL));
16061603
} else {
16071604
/* should not happen */
1608-
php_error_docref(NULL, E_ERROR, "GlobIterator lost glob state");
1605+
zend_throw_error(NULL, "GlobIterator lost glob state");
16091606
}
16101607
}
16111608
/* }}} */
@@ -2180,7 +2177,7 @@ static int spl_filesystem_file_read_line(zval * this_ptr, spl_filesystem_object
21802177
static void spl_filesystem_file_rewind(zval * this_ptr, spl_filesystem_object *intern) /* {{{ */
21812178
{
21822179
if(!intern->u.file.stream) {
2183-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2180+
zend_throw_error(NULL, "Object not initialized");
21842181
return;
21852182
}
21862183
if (-1 == php_stream_rewind(intern->u.file.stream)) {
@@ -2312,7 +2309,7 @@ SPL_METHOD(SplFileObject, eof)
23122309
}
23132310

23142311
if(!intern->u.file.stream) {
2315-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2312+
zend_throw_error(NULL, "Object not initialized");
23162313
RETURN_THROWS();
23172314
}
23182315

@@ -2350,7 +2347,7 @@ SPL_METHOD(SplFileObject, fgets)
23502347
}
23512348

23522349
if(!intern->u.file.stream) {
2353-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2350+
zend_throw_error(NULL, "Object not initialized");
23542351
RETURN_THROWS();
23552352
}
23562353

@@ -2371,7 +2368,7 @@ SPL_METHOD(SplFileObject, current)
23712368
}
23722369

23732370
if(!intern->u.file.stream) {
2374-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2371+
zend_throw_error(NULL, "Object not initialized");
23752372
RETURN_THROWS();
23762373
}
23772374

@@ -2460,7 +2457,7 @@ SPL_METHOD(SplFileObject, setMaxLineLen)
24602457
}
24612458

24622459
if (max_len < 0) {
2463-
zend_value_error("Maximum line length must be greater than or equal zero");
2460+
zend_argument_value_error(1, "must be greater than or equal to 0");
24642461
RETURN_THROWS();
24652462
}
24662463

@@ -2523,37 +2520,37 @@ SPL_METHOD(SplFileObject, fgetcsv)
25232520
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sss", &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == SUCCESS) {
25242521

25252522
if(!intern->u.file.stream) {
2526-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2523+
zend_throw_error(NULL, "Object not initialized");
25272524
RETURN_THROWS();
25282525
}
25292526

25302527
switch(ZEND_NUM_ARGS())
25312528
{
25322529
case 3:
25332530
if (esc_len > 1) {
2534-
php_error_docref(NULL, E_WARNING, "escape must be empty or a single character");
2535-
RETURN_FALSE;
2531+
zend_argument_value_error(3, "must be empty or a single character");
2532+
RETURN_THROWS();
25362533
}
25372534
if (esc_len == 0) {
25382535
escape = PHP_CSV_NO_ESCAPE;
25392536
} else {
25402537
escape = (unsigned char) esc[0];
25412538
}
2542-
/* no break */
2539+
/* explicit fallthrough */
25432540
case 2:
25442541
if (e_len != 1) {
2545-
php_error_docref(NULL, E_WARNING, "enclosure must be a character");
2546-
RETURN_FALSE;
2542+
zend_argument_value_error(2, "must be a character");
2543+
RETURN_THROWS();
25472544
}
25482545
enclosure = enclo[0];
2549-
/* no break */
2546+
/* explicit fallthrough */
25502547
case 1:
25512548
if (d_len != 1) {
2552-
php_error_docref(NULL, E_WARNING, "delimiter must be a character");
2553-
RETURN_FALSE;
2549+
zend_argument_value_error(1, "must be a character");
2550+
RETURN_THROWS();
25542551
}
25552552
delimiter = delim[0];
2556-
/* no break */
2553+
/* explicit fallthrough */
25572554
case 0:
25582555
break;
25592556
}
@@ -2586,24 +2583,24 @@ SPL_METHOD(SplFileObject, fputcsv)
25862583
escape = (unsigned char) esc[0];
25872584
break;
25882585
default:
2589-
php_error_docref(NULL, E_WARNING, "escape must be empty or a single character");
2590-
RETURN_FALSE;
2586+
zend_argument_value_error(3, "must be empty or a single character");
2587+
RETURN_THROWS();
25912588
}
2592-
/* no break */
2589+
/* explicit fallthrough */
25932590
case 3:
25942591
if (e_len != 1) {
2595-
php_error_docref(NULL, E_WARNING, "enclosure must be a character");
2596-
RETURN_FALSE;
2592+
zend_argument_value_error(3, "must be a character");
2593+
RETURN_THROWS();
25972594
}
25982595
enclosure = enclo[0];
2599-
/* no break */
2596+
/* explicit fallthrough */
26002597
case 2:
26012598
if (d_len != 1) {
2602-
php_error_docref(NULL, E_WARNING, "delimiter must be a character");
2603-
RETURN_FALSE;
2599+
zend_argument_value_error(2, "must be a character");
2600+
RETURN_THROWS();
26042601
}
26052602
delimiter = delim[0];
2606-
/* no break */
2603+
/* explicit fallthrough */
26072604
case 1:
26082605
case 0:
26092606
break;
@@ -2639,24 +2636,24 @@ SPL_METHOD(SplFileObject, setCsvControl)
26392636
escape = (unsigned char) esc[0];
26402637
break;
26412638
default:
2642-
php_error_docref(NULL, E_WARNING, "escape must be empty or a single character");
2643-
RETURN_FALSE;
2639+
zend_argument_value_error(3, "must be empty or a single character");
2640+
RETURN_THROWS();
26442641
}
2645-
/* no break */
2642+
/* explicit fallthrough */
26462643
case 2:
26472644
if (e_len != 1) {
2648-
php_error_docref(NULL, E_WARNING, "enclosure must be a character");
2649-
RETURN_FALSE;
2645+
zend_argument_value_error(2, "must be a character");
2646+
RETURN_THROWS();
26502647
}
26512648
enclosure = enclo[0];
2652-
/* no break */
2649+
/* explicit fallthrough */
26532650
case 1:
26542651
if (d_len != 1) {
2655-
php_error_docref(NULL, E_WARNING, "delimiter must be a character");
2656-
RETURN_FALSE;
2652+
zend_argument_value_error(1, "must be a character");
2653+
RETURN_THROWS();
26572654
}
26582655
delimiter = delim[0];
2659-
/* no break */
2656+
/* explicit fallthrough */
26602657
case 0:
26612658
break;
26622659
}
@@ -2705,7 +2702,7 @@ SPL_METHOD(SplFileObject, fflush)
27052702
spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
27062703

27072704
if(!intern->u.file.stream) {
2708-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2705+
zend_throw_error(NULL, "Object not initialized");
27092706
RETURN_THROWS();
27102707
}
27112708

@@ -2720,7 +2717,7 @@ SPL_METHOD(SplFileObject, ftell)
27202717
zend_long ret;
27212718

27222719
if(!intern->u.file.stream) {
2723-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2720+
zend_throw_error(NULL, "Object not initialized");
27242721
RETURN_THROWS();
27252722
}
27262723

@@ -2745,7 +2742,7 @@ SPL_METHOD(SplFileObject, fseek)
27452742
}
27462743

27472744
if(!intern->u.file.stream) {
2748-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2745+
zend_throw_error(NULL, "Object not initialized");
27492746
RETURN_THROWS();
27502747
}
27512748

@@ -2762,7 +2759,7 @@ SPL_METHOD(SplFileObject, fgetc)
27622759
int result;
27632760

27642761
if(!intern->u.file.stream) {
2765-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2762+
zend_throw_error(NULL, "Object not initialized");
27662763
RETURN_THROWS();
27672764
}
27682765

@@ -2790,7 +2787,7 @@ SPL_METHOD(SplFileObject, fpassthru)
27902787
spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
27912788

27922789
if(!intern->u.file.stream) {
2793-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2790+
zend_throw_error(NULL, "Object not initialized");
27942791
RETURN_THROWS();
27952792
}
27962793

@@ -2804,7 +2801,7 @@ SPL_METHOD(SplFileObject, fscanf)
28042801
spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
28052802

28062803
if(!intern->u.file.stream) {
2807-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2804+
zend_throw_error(NULL, "Object not initialized");
28082805
RETURN_THROWS();
28092806
}
28102807

@@ -2830,7 +2827,7 @@ SPL_METHOD(SplFileObject, fwrite)
28302827
}
28312828

28322829
if(!intern->u.file.stream) {
2833-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2830+
zend_throw_error(NULL, "Object not initialized");
28342831
RETURN_THROWS();
28352832
}
28362833

@@ -2864,13 +2861,13 @@ SPL_METHOD(SplFileObject, fread)
28642861
}
28652862

28662863
if(!intern->u.file.stream) {
2867-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2864+
zend_throw_error(NULL, "Object not initialized");
28682865
RETURN_THROWS();
28692866
}
28702867

28712868
if (length <= 0) {
2872-
php_error_docref(NULL, E_WARNING, "Length parameter must be greater than 0");
2873-
RETURN_FALSE;
2869+
zend_argument_value_error(1, "must be greater than 0");
2870+
RETURN_THROWS();
28742871
}
28752872

28762873
str = php_stream_read_to_str(intern->u.file.stream, length);
@@ -2897,7 +2894,7 @@ SPL_METHOD(SplFileObject, ftruncate)
28972894
}
28982895

28992896
if(!intern->u.file.stream) {
2900-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2897+
zend_throw_error(NULL, "Object not initialized");
29012898
RETURN_THROWS();
29022899
}
29032900

@@ -2920,7 +2917,7 @@ SPL_METHOD(SplFileObject, seek)
29202917
RETURN_THROWS();
29212918
}
29222919
if(!intern->u.file.stream) {
2923-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2920+
zend_throw_error(NULL, "Object not initialized");
29242921
RETURN_THROWS();
29252922
}
29262923

ext/spl/tests/DirectoryIterator_empty_constructor.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ Havard Eide <nucleuz@gmail.com>
77
<?php
88

99
try {
10-
new DirectoryIterator("");
10+
new DirectoryIterator('');
1111
} catch (\ValueError $ex) {
1212
echo $ex->getMessage() . PHP_EOL;
1313
}
1414

1515
?>
1616
--EXPECT--
17-
Directory name must not be empty
17+
DirectoryIterator::__construct(): Argument #1 ($path) cannot be empty

0 commit comments

Comments
 (0)