NoSilencedErrorsSniff
extends Sniff
in package
Discourage the use of the PHP error silencing operator.
This sniff allows the error operator to be used with a select list of functions, as no amount of error checking can prevent PHP from throwing errors when those functions are used.
Tags
Table of Contents
Properties
- $context_length : int
- Number of tokens to display in the error message to show the error silencing context.
- $customAllowedFunctionsList : array<string|int, string>
- User defined function list.
- $usePHPFunctionsList : bool
- Whether or not the `$allowedFunctionsList` should be used.
- $allowedFunctionsList : array<string, true>
- PHP native functions allow list.
- $phpcsFile : File
- The current file being sniffed.
- $tokens : array<string|int, mixed>
- The list of tokens in the current file being sniffed.
- $empty_tokens : array<int|string, int|string>
- Tokens which are regarded as empty for the purpose of determining the name of the called function.
Methods
- process() : int|void
- Set sniff properties and hand off to child class for processing of the token.
- process_token() : int|void
- Processes this test, when one of its tokens is encountered.
- register() : array<string|int, mixed>
- Returns an array of tokens this test wants to listen for.
Properties
$context_length
Number of tokens to display in the error message to show the error silencing context.
public
int
$context_length
= 6
Tags
$customAllowedFunctionsList
User defined function list.
public
array<string|int, string>
$customAllowedFunctionsList
= array()
Allows users to pass a list of additional functions for which to allow the use of the silence operator. This list can be set in a custom ruleset.
Tags
$usePHPFunctionsList
Whether or not the `$allowedFunctionsList` should be used.
public
bool
$usePHPFunctionsList
= true
Defaults to true.
This property only affects whether the standard function list is used. The custom allowed functions list, if set, will always be respected.
Tags
$allowedFunctionsList
PHP native functions allow list.
protected
array<string, true>
$allowedFunctionsList
= array(
// Directory extension.
'chdir' => true,
'opendir' => true,
'scandir' => true,
// File extension.
'file_exists' => true,
'file_get_contents' => true,
'file' => true,
'fileatime' => true,
'filectime' => true,
'filegroup' => true,
'fileinode' => true,
'filemtime' => true,
'fileowner' => true,
'fileperms' => true,
'filesize' => true,
'filetype' => true,
'fopen' => true,
'is_dir' => true,
'is_executable' => true,
'is_file' => true,
'is_link' => true,
'is_readable' => true,
'is_writable' => true,
'is_writeable' => true,
'lstat' => true,
'mkdir' => true,
'move_uploaded_file' => true,
'readfile' => true,
'readlink' => true,
'rename' => true,
'rmdir' => true,
'stat' => true,
'unlink' => true,
// FTP extension.
'ftp_chdir' => true,
'ftp_login' => true,
'ftp_rename' => true,
// Stream extension.
'stream_select' => true,
'stream_set_chunk_size' => true,
// Zlib extension.
'deflate_add' => true,
'deflate_init' => true,
'inflate_add' => true,
'inflate_init' => true,
'readgzfile' => true,
// LibXML extension.
'libxml_disable_entity_loader' => true,
// PHP 8.0 deprecation warning, but function call still needed in select cases.
// Miscellaneous other functions.
'imagecreatefromstring' => true,
'imagecreatefromwebp' => true,
'parse_url' => true,
// Pre-PHP 5.3.3 an E_WARNING was thrown when URL parsing failed.
'unserialize' => true,
)
Errors caused by calls to any of these native PHP functions are allowed to be silenced as file system permissions and such can cause E_WARNINGs to be thrown which cannot be prevented via error checking.
Note: only calls to global functions - in contrast to class methods - are taken into account.
Only functions for which the PHP manual annotates that an error will be thrown on failure are accepted into this list.
Key is function name, value irrelevant.
Tags
$phpcsFile
The current file being sniffed.
protected
File
$phpcsFile
Tags
$tokens
The list of tokens in the current file being sniffed.
protected
array<string|int, mixed>
$tokens
Tags
$empty_tokens
Tokens which are regarded as empty for the purpose of determining the name of the called function.
private
array<int|string, int|string>
$empty_tokens
= array()
This property is set from within the register() method.
Tags
Methods
process()
Set sniff properties and hand off to child class for processing of the token.
public
process(File $phpcsFile, int $stackPtr) : int|void
Parameters
- $phpcsFile : File
-
The file being scanned.
- $stackPtr : int
-
The position of the current token in the stack passed in $tokens.
Tags
Return values
int|void —Integer stack pointer to skip forward or void to continue normal file processing.
process_token()
Processes this test, when one of its tokens is encountered.
public
process_token(int $stackPtr) : int|void
Parameters
- $stackPtr : int
-
The position of the current token in the stack.
Tags
Return values
int|void —Integer stack pointer to skip forward or void to continue normal file processing.
register()
Returns an array of tokens this test wants to listen for.
public
register() : array<string|int, mixed>