]> granicus.if.org Git - php/commitdiff
Promote warnings to exceptions in string search related functions
authorMáté Kocsis <kocsismate@woohoolabs.com>
Wed, 20 Nov 2019 01:34:52 +0000 (02:34 +0100)
committerMáté Kocsis <kocsismate@woohoolabs.com>
Fri, 20 Dec 2019 15:43:40 +0000 (16:43 +0100)
GH-5004

15 files changed:
ext/iconv/tests/iconv_strpos.phpt
ext/mbstring/tests/bug43841.phpt
ext/mbstring/tests/bug45923.phpt
ext/standard/string.c
ext/standard/tests/strings/bug40754.phpt
ext/standard/tests/strings/stripos_error.phpt
ext/standard/tests/strings/stripos_variation11.phpt
ext/standard/tests/strings/stripos_variation7.phpt
ext/standard/tests/strings/strpos.phpt
ext/standard/tests/strings/strripos_offset.phpt
ext/standard/tests/strings/strrpos_negative_offset.phpt
ext/standard/tests/strings/strrpos_offset.phpt
ext/standard/tests/strings/strrpos_variation11.phpt
ext/standard/tests/strings/strrpos_variation7.phpt
ext/standard/tests/strings/substr_count_error.phpt

index 92492c7244af37c1bc67ac87fdef6e5335cb8dbc..9a1e5a77b6d375f7a15a7a8bf5020ce3cbc03ccb 100644 (file)
@@ -6,17 +6,16 @@ iconv_strpos()
 iconv.internal_charset=ISO-8859-1
 --FILE--
 <?php
-function my_error_handler($errno, $errmsg, $filename, $linenum)
-{
-       echo "$errno: $errmsg\n";
-}
-set_error_handler('my_error_handler');
 function foo($haystk, $needle, $offset, $to_charset = false, $from_charset = false)
 {
        if ($from_charset !== false) {
                $haystk = iconv($from_charset, $to_charset, $haystk);
        }
-       var_dump(strpos($haystk, $needle, $offset));
+       try {
+           var_dump(strpos($haystk, $needle, $offset));
+       } catch (ValueError $exception) {
+           echo $exception->getMessage() . "\n";
+       }
        if ($to_charset !== false) {
                var_dump(iconv_strpos($haystk, $needle, $offset, $to_charset));
        } else {
@@ -42,8 +41,7 @@ bool(false)
 bool(false)
 int(5)
 int(5)
-2: %s
-bool(false)
+Offset not contained in string
 bool(false)
 int(7)
 int(7)
index ab52eb51c94acdbc96a70a1d119bd2d17dace01b..5585ba39630464e37c24413b266d47808db353f5 100644 (file)
@@ -30,7 +30,11 @@ foreach ($offsets as $i) {
        echo "mb_strrpos:\n";
        var_dump(mb_strrpos('This is na English ta', 'a', $i));
        echo "strrpos:\n";
-       var_dump(strrpos('This is na English ta', 'a', $i));
+       try {
+           var_dump(strrpos('This is na English ta', 'a', $i));
+       } catch (ValueError $exception) {
+           echo $exception->getMessage() . "\n";
+       }
 }
 ?>
 --EXPECTF--
@@ -45,9 +49,7 @@ mb_strrpos:
 Warning: mb_strrpos(): Offset is greater than the length of haystack string in %s on line %d
 bool(false)
 strrpos:
-
-Warning: strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 -- Offset is -24 --
 Multibyte String:
@@ -60,9 +62,7 @@ mb_strrpos:
 Warning: mb_strrpos(): Offset is greater than the length of haystack string in %s on line %d
 bool(false)
 strrpos:
-
-Warning: strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 -- Offset is -13 --
 Multibyte String:
index 6fc8e1db0989c5b500487293a846b3bd792db7df..268917bbd87d014dc01a1a90cba8365785e5d291 100644 (file)
@@ -10,7 +10,11 @@ function section($func, $haystack, $needle)
        echo "\n------- $func -----------\n\n";
        foreach(array(0, 3, 6, 9, 11, 12, -1, -3, -6, -20) as $offset) {
                echo "> Offset: $offset\n";
-               var_dump($func($haystack,$needle,$offset));
+               try {
+                   var_dump($func($haystack,$needle,$offset));
+               } catch (ValueError $exception) {
+                   echo $exception->getMessage() . "\n";
+               }
        }
 }
 
@@ -40,9 +44,7 @@ bool(false)
 > Offset: 11
 bool(false)
 > Offset: 12
-
-Warning: strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 > Offset: -1
 bool(false)
 > Offset: -3
@@ -50,9 +52,7 @@ int(8)
 > Offset: -6
 int(8)
 > Offset: -20
-
-Warning: strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 ------- mb_strpos -----------
 
@@ -94,9 +94,7 @@ bool(false)
 > Offset: 11
 bool(false)
 > Offset: 12
-
-Warning: stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 > Offset: -1
 bool(false)
 > Offset: -3
@@ -104,9 +102,7 @@ int(8)
 > Offset: -6
 int(8)
 > Offset: -20
-
-Warning: stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 ------- mb_stripos -----------
 
@@ -148,9 +144,7 @@ bool(false)
 > Offset: 11
 bool(false)
 > Offset: 12
-
-Warning: strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 > Offset: -1
 int(8)
 > Offset: -3
@@ -158,9 +152,7 @@ int(8)
 > Offset: -6
 int(4)
 > Offset: -20
-
-Warning: strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 ------- mb_strrpos -----------
 
@@ -202,9 +194,7 @@ bool(false)
 > Offset: 11
 bool(false)
 > Offset: 12
-
-Warning: strripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 > Offset: -1
 int(8)
 > Offset: -3
@@ -212,9 +202,7 @@ int(8)
 > Offset: -6
 int(4)
 > Offset: -20
-
-Warning: strripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 ------- mb_strripos -----------
 
index 1bfbb7044b8d0acdbed96328f8c445ff2f7965c6..59228eac4a3c8404f02d8b558302bfb1fc698cc0 100644 (file)
@@ -1874,8 +1874,8 @@ PHP_FUNCTION(strpos)
                offset += (zend_long)ZSTR_LEN(haystack);
        }
        if (offset < 0 || (size_t)offset > ZSTR_LEN(haystack)) {
-               php_error_docref(NULL, E_WARNING, "Offset not contained in string");
-               RETURN_FALSE;
+               zend_value_error("Offset not contained in string");
+               return;
        }
 
        found = (char*)php_memnstr(ZSTR_VAL(haystack) + offset,
@@ -1910,8 +1910,8 @@ PHP_FUNCTION(stripos)
                offset += (zend_long)ZSTR_LEN(haystack);
        }
        if (offset < 0 || (size_t)offset > ZSTR_LEN(haystack)) {
-               php_error_docref(NULL, E_WARNING, "Offset not contained in string");
-               RETURN_FALSE;
+               zend_value_error("Offset not contained in string");
+               return;
        }
 
        if (ZSTR_LEN(needle) > ZSTR_LEN(haystack)) {
@@ -1952,15 +1952,15 @@ PHP_FUNCTION(strrpos)
 
        if (offset >= 0) {
                if ((size_t)offset > ZSTR_LEN(haystack)) {
-                       php_error_docref(NULL, E_WARNING, "Offset not contained in string");
-                       RETURN_FALSE;
+                       zend_value_error("Offset not contained in string");
+                       return;
                }
                p = ZSTR_VAL(haystack) + (size_t)offset;
                e = ZSTR_VAL(haystack) + ZSTR_LEN(haystack);
        } else {
-               if (offset < -INT_MAX || (size_t)(-offset) > ZSTR_LEN(haystack)) {
-                       php_error_docref(NULL, E_WARNING, "Offset not contained in string");
-                       RETURN_FALSE;
+               if (offset < -ZEND_LONG_MAX || (size_t)(-offset) > ZSTR_LEN(haystack)) {
+                       zend_value_error("Offset not contained in string");
+                       return;
                }
 
                p = ZSTR_VAL(haystack);
@@ -2002,16 +2002,16 @@ PHP_FUNCTION(strripos)
                char lowered;
                if (offset >= 0) {
                        if ((size_t)offset > ZSTR_LEN(haystack)) {
-                               php_error_docref(NULL, E_WARNING, "Offset not contained in string");
-                               RETURN_FALSE;
+                               zend_value_error("Offset not contained in string");
+                               return;
                        }
                        p = ZSTR_VAL(haystack) + (size_t)offset;
                        e = ZSTR_VAL(haystack) + ZSTR_LEN(haystack) - 1;
                } else {
                        p = ZSTR_VAL(haystack);
-                       if (offset < -INT_MAX || (size_t)(-offset) > ZSTR_LEN(haystack)) {
-                               php_error_docref(NULL, E_WARNING, "Offset not contained in string");
-                               RETURN_FALSE;
+                       if (offset < -ZEND_LONG_MAX || (size_t)(-offset) > ZSTR_LEN(haystack)) {
+                               zend_value_error("Offset not contained in string");
+                               return;
                        }
                        e = ZSTR_VAL(haystack) + (ZSTR_LEN(haystack) + (size_t)offset);
                }
@@ -2030,16 +2030,16 @@ PHP_FUNCTION(strripos)
        if (offset >= 0) {
                if ((size_t)offset > ZSTR_LEN(haystack)) {
                        zend_string_release_ex(haystack_dup, 0);
-                       php_error_docref(NULL, E_WARNING, "Offset not contained in string");
-                       RETURN_FALSE;
+                       zend_value_error("Offset not contained in string");
+                       return;
                }
                p = ZSTR_VAL(haystack_dup) + offset;
                e = ZSTR_VAL(haystack_dup) + ZSTR_LEN(haystack);
        } else {
-               if (offset < -INT_MAX || (size_t)(-offset) > ZSTR_LEN(haystack)) {
+               if (offset < -ZEND_LONG_MAX || (size_t)(-offset) > ZSTR_LEN(haystack)) {
                        zend_string_release_ex(haystack_dup, 0);
-                       php_error_docref(NULL, E_WARNING, "Offset not contained in string");
-                       RETURN_FALSE;
+                       zend_value_error("Offset not contained in string");
+                       return;
                }
 
                p = ZSTR_VAL(haystack_dup);
@@ -5533,8 +5533,8 @@ PHP_FUNCTION(substr_count)
                offset += (zend_long)haystack_len;
        }
        if ((offset < 0) || ((size_t)offset > haystack_len)) {
-               php_error_docref(NULL, E_WARNING, "Offset not contained in string");
-               RETURN_FALSE;
+               zend_value_error("Offset not contained in string");
+               return;
        }
        p += offset;
 
index d502bff829e27869113df5721909f1b70d24c081..9e409bf2711753ce4b6c720511a0cd277b1fc88c 100644 (file)
@@ -11,16 +11,52 @@ var_dump(substr_replace("abcde", "x", $v, $v));
 var_dump(strspn("abcde", "abc", $v, $v));
 var_dump(strcspn("abcde", "abc", $v, $v));
 
-var_dump(substr_count("abcde", "abc", $v, $v));
+try {
+    var_dump(substr_count("abcde", "abc", $v, $v));
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
+
 var_dump(substr_compare("abcde", "abc", $v, $v));
 
-var_dump(stripos("abcde", "abc", $v));
-var_dump(substr_count("abcde", "abc", $v, 1));
+try {
+    stripos("abcde", "abc", $v);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
+
+try {
+    substr_count("abcde", "abc", $v, 1);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
+
 var_dump(substr_count("abcde", "abc", 1, $v));
-var_dump(strpos("abcde", "abc", $v));
-var_dump(stripos("abcde", "abc", $v));
-var_dump(strrpos("abcde", "abc", $v));
-var_dump(strripos("abcde", "abc", $v));
+
+try {
+    strpos("abcde", "abc", $v);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
+
+try {
+    stripos("abcde", "abc", $v);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
+
+try {
+    strrpos("abcde", "abc", $v);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
+
+try {
+    strripos("abcde", "abc", $v);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
+
 var_dump(strncmp("abcde", "abc", $v));
 var_dump(chunk_split("abcde", $v, "abc"));
 var_dump(substr("abcde", $v, $v));
@@ -31,33 +67,19 @@ string(4) "bcde"
 string(6) "abcdex"
 bool(false)
 bool(false)
-
-Warning: substr_count(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 Warning: substr_compare(): The start position cannot exceed initial string length in %s on line %d
 bool(false)
-
-Warning: stripos(): Offset not contained in string in %s on line %d
-bool(false)
-
-Warning: substr_count(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
+Offset not contained in string
 
 Warning: substr_count(): Invalid length value in %s on line %d
 bool(false)
-
-Warning: strpos(): Offset not contained in string in %s on line %d
-bool(false)
-
-Warning: stripos(): Offset not contained in string in %s on line %d
-bool(false)
-
-Warning: strrpos(): Offset not contained in string in %s on line %d
-bool(false)
-
-Warning: strripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
 int(2)
 string(8) "abcdeabc"
 bool(false)
index 0420bb090b268bd8338dab1ae0574050985fdd1b..caa93298748abc496f83ffdf9528195e5e1339a0 100644 (file)
@@ -9,22 +9,28 @@ Test stripos() function : error conditions
 
 echo "*** Testing stripos() function: error conditions ***\n";
 
-echo "\n-- Offset beyond the end of the string --";
-var_dump( stripos("Hello World", "o", 12) );
+echo "\n-- Offset beyond the end of the string --\n";
+try {
+    stripos("Hello World", "o", 12);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 
-echo "\n-- Offset before the start of the string --";
-var_dump( stripos("Hello World", "o", -12) );
+echo "\n-- Offset before the start of the string --\n";
+try {
+    stripos("Hello World", "o", -12);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 
 echo "*** Done ***";
 ?>
---EXPECTF--
+--EXPECT--
 *** Testing stripos() function: error conditions ***
 
 -- Offset beyond the end of the string --
-Warning: stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 -- Offset before the start of the string --
-Warning: stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 *** Done ***
index 4f002ed0ca367813fd5aba57da9d2e688dcda4d7..7a9c9ff9a06cfcc9b99e32e8ebede50a70bf915b 100644 (file)
@@ -83,13 +83,13 @@ for($index = 0; $index < count($values); $index ++) {
   $haystack = $values[$index];
   try {
     var_dump( stripos($values[$index], $values[$index]) );
-  } catch (TypeError $e) {
-    echo $e->getMessage(), "\n";
+  } catch (Error $e) {
+    echo get_class($e) . ": " . $e->getMessage(), "\n";
   }
   try {
     var_dump( stripos($values[$index], $values[$index], 1) );
-  } catch (TypeError $e) {
-    echo $e->getMessage(), "\n";
+  } catch (Error $e) {
+    echo get_class($e) . ": " . $e->getMessage(), "\n";
   }
   $counter ++;
 }
@@ -126,70 +126,54 @@ bool(false)
 int(0)
 bool(false)
 -- Iteration 10 --
-stripos() expects parameter 1 to be string, array given
-stripos() expects parameter 1 to be string, array given
+TypeError: stripos() expects parameter 1 to be string, array given
+TypeError: stripos() expects parameter 1 to be string, array given
 -- Iteration 11 --
-stripos() expects parameter 1 to be string, array given
-stripos() expects parameter 1 to be string, array given
+TypeError: stripos() expects parameter 1 to be string, array given
+TypeError: stripos() expects parameter 1 to be string, array given
 -- Iteration 12 --
-stripos() expects parameter 1 to be string, array given
-stripos() expects parameter 1 to be string, array given
+TypeError: stripos() expects parameter 1 to be string, array given
+TypeError: stripos() expects parameter 1 to be string, array given
 -- Iteration 13 --
-stripos() expects parameter 1 to be string, array given
-stripos() expects parameter 1 to be string, array given
+TypeError: stripos() expects parameter 1 to be string, array given
+TypeError: stripos() expects parameter 1 to be string, array given
 -- Iteration 14 --
-stripos() expects parameter 1 to be string, array given
-stripos() expects parameter 1 to be string, array given
+TypeError: stripos() expects parameter 1 to be string, array given
+TypeError: stripos() expects parameter 1 to be string, array given
 -- Iteration 15 --
 int(0)
 bool(false)
 -- Iteration 16 --
 int(0)
-
-Warning: stripos(): Offset not contained in string in %s on line %d
-bool(false)
+ValueError: Offset not contained in string
 -- Iteration 17 --
 int(0)
 bool(false)
 -- Iteration 18 --
 int(0)
-
-Warning: stripos(): Offset not contained in string in %s on line %d
-bool(false)
+ValueError: Offset not contained in string
 -- Iteration 19 --
 int(0)
 bool(false)
 -- Iteration 20 --
 int(0)
-
-Warning: stripos(): Offset not contained in string in %s on line %d
-bool(false)
+ValueError: Offset not contained in string
 -- Iteration 21 --
 int(0)
-
-Warning: stripos(): Offset not contained in string in %s on line %d
-bool(false)
+ValueError: Offset not contained in string
 -- Iteration 22 --
 int(0)
-
-Warning: stripos(): Offset not contained in string in %s on line %d
-bool(false)
+ValueError: Offset not contained in string
 -- Iteration 23 --
 int(0)
-
-Warning: stripos(): Offset not contained in string in %s on line %d
-bool(false)
+ValueError: Offset not contained in string
 -- Iteration 24 --
-stripos() expects parameter 1 to be string, resource given
-stripos() expects parameter 1 to be string, resource given
+TypeError: stripos() expects parameter 1 to be string, resource given
+TypeError: stripos() expects parameter 1 to be string, resource given
 -- Iteration 25 --
 int(0)
-
-Warning: stripos(): Offset not contained in string in %s on line %d
-bool(false)
+ValueError: Offset not contained in string
 -- Iteration 26 --
 int(0)
-
-Warning: stripos(): Offset not contained in string in %s on line %d
-bool(false)
+ValueError: Offset not contained in string
 *** Done ***
index a21686876d31d4da24aa2decf3d5c9055e338287..d2ac6551dbe1d18465f6083d96ac02ec689727cc 100644 (file)
@@ -16,19 +16,22 @@ echo "-- With empty heredoc string --\n";
 $empty_string = <<<EOD
 EOD;
 var_dump( stripos($empty_string, "") );
-var_dump( stripos($empty_string, "", 1) );
+
+try {
+    stripos($empty_string, "", 1);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 var_dump( stripos($empty_string, FALSE) );
 var_dump( stripos($empty_string, NULL) );
 
 echo "*** Done ***";
 ?>
---EXPECTF--
+--EXPECT--
 *** Testing stripos() function: with heredoc strings ***
 -- With empty heredoc string --
 int(0)
-
-Warning: stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 int(0)
 int(0)
 *** Done ***
index 8e1a1a7472d18640676c3b83ca6993cb239f4648..2bc95a6c0a900c634d386d23a3155cf940e80c38 100644 (file)
Binary files a/ext/standard/tests/strings/strpos.phpt and b/ext/standard/tests/strings/strpos.phpt differ
index 7dc0ce0d61dd0839fe7e9d0a303c38f0dc074afa..4f4ab2bea4cbf3b5d312290d1a893af776a080c7 100644 (file)
@@ -9,25 +9,36 @@ try {
     echo $e->getMessage(), "\n";
 }
 
-var_dump(strripos(1024, 1024, -PHP_INT_MAX));
-var_dump(strripos(1024, "te", -PHP_INT_MAX));
-var_dump(strripos(1024, 1024, -PHP_INT_MAX-1));
-var_dump(strripos(1024, "te", -PHP_INT_MAX-1));
-
-echo "Done\n";
-?>
---EXPECTF--
-strripos() expects parameter 3 to be int, float given
+try {
+    strripos(1024, 1024, -PHP_INT_MAX);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 
-Warning: strripos(): Offset not contained in string in %s on line %d
-bool(false)
+try {
+    strripos(1024, "te", -PHP_INT_MAX);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 
-Warning: strripos(): Offset not contained in string in %s on line %d
-bool(false)
+try {
+    strripos(1024, 1024, -PHP_INT_MAX-1);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 
-Warning: strripos(): Offset not contained in string in %s on line %d
-bool(false)
+try {
+    strripos(1024, "te", -PHP_INT_MAX-1);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 
-Warning: strripos(): Offset not contained in string in %s on line %d
-bool(false)
+echo "Done\n";
+?>
+--EXPECT--
+strripos() expects parameter 3 to be int, float given
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
 Done
index 940c2274f4e7f1e9c586edf6a7d62c994d625378..17560f18f7fd65934c7563049973211b453b8128 100644 (file)
@@ -8,15 +8,22 @@ strr[i]pos() function with negative offset
        var_dump(strrpos("haystack", "ka", -1));
        var_dump(strrpos("haystack", 'a', -3));
        var_dump(strrpos("haystack", 'a', -4));
-       var_dump(@strrpos("haystack", 'h', -9));
-
+       try {
+           strrpos("haystack", 'h', -9);
+       } catch (ValueError $exception) {
+           echo $exception->getMessage() . "\n";
+       }
        var_dump(strripos("HAYSTHACk", 'ha', -9));
        var_dump(strripos("HAYSTACK", 'h', -8));
        var_dump(strripos("HAYSTACK", 'k', -1));
        var_dump(strripos("HAYSTACK", "ka", -1));
        var_dump(strripos("HAYSTACK", 'a', -3));
        var_dump(strripos("HAYSTACK", 'a', -4));
-       var_dump(@strripos("HAYSTACK", 'h', -9));
+       try {
+           strripos("HAYSTACK", 'h', -9);
+       } catch (ValueError $exception) {
+        echo $exception->getMessage() . "\n";
+       }
 ?>
 --EXPECT--
 int(0)
@@ -25,11 +32,11 @@ int(7)
 bool(false)
 int(5)
 int(1)
-bool(false)
+Offset not contained in string
 int(0)
 int(0)
 int(7)
 bool(false)
 int(5)
 int(1)
-bool(false)
+Offset not contained in string
index fdff39fdb2446d9689794a1f6d5c85938b4c326e..213cd24c6fdb78914771d6bffb533ef487f3e1d4 100644 (file)
@@ -9,25 +9,36 @@ try {
     echo $e->getMessage(), "\n";
 }
 
-var_dump(strrpos(1024, 1024, -PHP_INT_MAX));
-var_dump(strrpos(1024, "te", -PHP_INT_MAX));
-var_dump(strrpos(1024, 1024, -PHP_INT_MAX-1));
-var_dump(strrpos(1024, "te", -PHP_INT_MAX-1));
-
-echo "Done\n";
-?>
---EXPECTF--
-strrpos() expects parameter 3 to be int, float given
+try {
+    strrpos(1024, 1024, -PHP_INT_MAX);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 
-Warning: strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+try {
+    strrpos(1024, "te", -PHP_INT_MAX);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 
-Warning: strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+try {
+    strrpos(1024, 1024, -PHP_INT_MAX-1);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 
-Warning: strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+try {
+    strrpos(1024, "te", -PHP_INT_MAX-1);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 
-Warning: strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+echo "Done\n";
+?>
+--EXPECT--
+strrpos() expects parameter 3 to be int, float given
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
 Done
index 2fbe78e30acd09ad5d0df37845287f2987771fc3..2f8627d5ad240d42e4ba3a22eea5e0cb52ee6963 100644 (file)
@@ -83,13 +83,14 @@ for($index = 0; $index < count($values); $index ++) {
   $haystack = $values[$index];
   try {
     var_dump( strrpos($values[$index], $values[$index]) );
-  } catch (TypeError $e) {
-    echo $e->getMessage(), "\n";
+  } catch (Error $e) {
+    echo get_class($e) . ": " . $e->getMessage(), "\n";
   }
+
   try {
     var_dump( strrpos($values[$index], $values[$index], 1) );
-  } catch (TypeError $e) {
-    echo $e->getMessage(), "\n";
+  } catch (Error $e) {
+    echo get_class($e) . ": " . $e->getMessage(), "\n";
   }
   $counter ++;
 }
@@ -126,70 +127,54 @@ bool(false)
 int(0)
 bool(false)
 -- Iteration 10 --
-strrpos() expects parameter 1 to be string, array given
-strrpos() expects parameter 1 to be string, array given
+TypeError: strrpos() expects parameter 1 to be string, array given
+TypeError: strrpos() expects parameter 1 to be string, array given
 -- Iteration 11 --
-strrpos() expects parameter 1 to be string, array given
-strrpos() expects parameter 1 to be string, array given
+TypeError: strrpos() expects parameter 1 to be string, array given
+TypeError: strrpos() expects parameter 1 to be string, array given
 -- Iteration 12 --
-strrpos() expects parameter 1 to be string, array given
-strrpos() expects parameter 1 to be string, array given
+TypeError: strrpos() expects parameter 1 to be string, array given
+TypeError: strrpos() expects parameter 1 to be string, array given
 -- Iteration 13 --
-strrpos() expects parameter 1 to be string, array given
-strrpos() expects parameter 1 to be string, array given
+TypeError: strrpos() expects parameter 1 to be string, array given
+TypeError: strrpos() expects parameter 1 to be string, array given
 -- Iteration 14 --
-strrpos() expects parameter 1 to be string, array given
-strrpos() expects parameter 1 to be string, array given
+TypeError: strrpos() expects parameter 1 to be string, array given
+TypeError: strrpos() expects parameter 1 to be string, array given
 -- Iteration 15 --
 int(0)
 bool(false)
 -- Iteration 16 --
 int(0)
-
-Warning: strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+ValueError: Offset not contained in string
 -- Iteration 17 --
 int(0)
 bool(false)
 -- Iteration 18 --
 int(0)
-
-Warning: strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+ValueError: Offset not contained in string
 -- Iteration 19 --
 int(0)
 bool(false)
 -- Iteration 20 --
 int(0)
-
-Warning: strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+ValueError: Offset not contained in string
 -- Iteration 21 --
 int(0)
-
-Warning: strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+ValueError: Offset not contained in string
 -- Iteration 22 --
 int(0)
-
-Warning: strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+ValueError: Offset not contained in string
 -- Iteration 23 --
 int(0)
-
-Warning: strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+ValueError: Offset not contained in string
 -- Iteration 24 --
-strrpos() expects parameter 1 to be string, resource given
-strrpos() expects parameter 1 to be string, resource given
+TypeError: strrpos() expects parameter 1 to be string, resource given
+TypeError: strrpos() expects parameter 1 to be string, resource given
 -- Iteration 25 --
 int(0)
-
-Warning: strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+ValueError: Offset not contained in string
 -- Iteration 26 --
 int(0)
-
-Warning: strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+ValueError: Offset not contained in string
 *** Done ***
index d5aa8b561ccb5c59a9bb2089ba8d55a8c56f9bd5..9a02a4a0ec00fb9343408745ab730c919c79e86f 100644 (file)
@@ -16,19 +16,21 @@ echo "-- With empty heredoc string --\n";
 $empty_string = <<<EOD
 EOD;
 var_dump( strrpos($empty_string, "") );
-var_dump( strrpos($empty_string, "", 1) );
+try {
+    strrpos($empty_string, "", 1);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 var_dump( strrpos($empty_string, FALSE) );
 var_dump( strrpos($empty_string, NULL) );
 
 echo "*** Done ***";
 ?>
---EXPECTF--
+--EXPECT--
 *** Testing strrpos() function: with heredoc strings ***
 -- With empty heredoc string --
 int(0)
-
-Warning: strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 int(0)
 int(0)
 *** Done ***
index 246963d13b40e6becf67a6c4733786605a39a41a..9bdb3843ebce28af4418a6ff01ea92131fe73539 100644 (file)
@@ -7,10 +7,18 @@ echo "\n*** Testing error conditions ***\n";
 $str = 'abcdefghik';
 
 /* offset before start */
-var_dump(substr_count($str, "t", -20));
+try {
+    substr_count($str, "t", -20);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 
 /* offset > size of the string */
-var_dump(substr_count($str, "t", 25));
+try {
+    substr_count($str, "t", 25);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 
 /* Using offset and length to go beyond the size of the string:
    Warning message expected, as length+offset > length of string */
@@ -24,12 +32,8 @@ echo "Done\n";
 ?>
 --EXPECTF--
 *** Testing error conditions ***
-
-Warning: substr_count(): Offset not contained in string in %s on line %d
-bool(false)
-
-Warning: substr_count(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
+Offset not contained in string
 
 Warning: substr_count(): Invalid length value in %s on line %d
 bool(false)