php-src/ext/standard/tests/file/fscanf_error.phpt
Nikita Popov b10416a652 Deprecate passing null to non-nullable arg of internal function
This deprecates passing null to non-nullable scale arguments of
internal functions, with the eventual goal of making the behavior
consistent with userland functions, where null is never accepted
for non-nullable arguments.

This change is expected to cause quite a lot of fallout. In most
cases, calling code should be adjusted to avoid passing null. In
some cases, PHP should be adjusted to make some function arguments
nullable. I have already fixed a number of functions before landing
this, but feel free to file a bug if you encounter a function that
doesn't accept null, but probably should. (The rule of thumb for
this to be applicable is that the function must have special behavior
for 0 or "", which is distinct from the natural behavior of the
parameter.)

RFC: https://wiki.php.net/rfc/deprecate_null_to_scalar_internal_arg

Closes GH-6475.
2021-02-11 21:46:13 +01:00

69 lines
1.7 KiB
PHP

--TEST--
Test fscanf() function: error conditions
--FILE--
<?php
echo "*** Testing fscanf() for error conditions ***\n";
$file_path = __DIR__;
$filename = "$file_path/fscanf_error.tmp";
$file_handle = fopen($filename, 'w');
if ($file_handle == false)
exit("Error:failed to open file $filename");
fwrite($file_handle, "hello world");
fclose($file_handle);
// invalid file handle
try {
fscanf($file_handle, "%s");
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
// number of formats in format strings not matching the no of variables
$file_handle = fopen($filename, 'r');
if ($file_handle == false)
exit("Error:failed to open file $filename");
try {
fscanf($file_handle, "%d%s%f", $int_var, $string_var);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
fclose($file_handle);
// different invalid format strings
$invalid_formats = array("", "%", "%h", "%.", "%d%m");
// looping to use various invalid formats with fscanf()
foreach($invalid_formats as $format) {
$file_handle = fopen($filename, 'r');
if ($file_handle == false)
exit("Error:failed to open file $filename");
try {
var_dump(fscanf($file_handle, $format));
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
fclose($file_handle);
}
echo "\n*** Done ***";
?>
--CLEAN--
<?php
$file_path = __DIR__;
$filename = "$file_path/fscanf_error.tmp";
unlink($filename);
?>
--EXPECT--
*** Testing fscanf() for error conditions ***
fscanf(): supplied resource is not a valid File-Handle resource
Different numbers of variable names and field specifiers
array(0) {
}
Bad scan conversion character "
Bad scan conversion character "
Bad scan conversion character "."
Bad scan conversion character "m"
*** Done ***