基础代码
This commit is contained in:
+66
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
/**
|
||||
* WordPress Coding Standard.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
* @link https://github.com/WordPress/WordPress-Coding-Standards
|
||||
* @license https://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
namespace WordPressCS\WordPress\Sniffs\PHP;
|
||||
|
||||
use WordPressCS\WordPress\AbstractFunctionRestrictionsSniff;
|
||||
|
||||
/**
|
||||
* Restrict the use of various development functions.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
*
|
||||
* @since 0.11.0
|
||||
* @since 0.13.0 Class name changed: this class is now namespaced.
|
||||
*/
|
||||
class DevelopmentFunctionsSniff extends AbstractFunctionRestrictionsSniff {
|
||||
|
||||
/**
|
||||
* Groups of functions to restrict.
|
||||
*
|
||||
* Example: groups => array(
|
||||
* 'lambda' => array(
|
||||
* 'type' => 'error' | 'warning',
|
||||
* 'message' => 'Use anonymous functions instead please!',
|
||||
* 'functions' => array( 'file_get_contents', 'create_function' ),
|
||||
* )
|
||||
* )
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getGroups() {
|
||||
return array(
|
||||
'error_log' => array(
|
||||
'type' => 'warning',
|
||||
'message' => '%s() found. Debug code should not normally be used in production.',
|
||||
'functions' => array(
|
||||
'error_log',
|
||||
'var_dump',
|
||||
'var_export',
|
||||
'print_r',
|
||||
'trigger_error',
|
||||
'set_error_handler',
|
||||
'debug_backtrace',
|
||||
'debug_print_backtrace',
|
||||
'wp_debug_backtrace_summary',
|
||||
),
|
||||
),
|
||||
|
||||
'prevent_path_disclosure' => array(
|
||||
'type' => 'warning',
|
||||
'message' => '%s() can lead to full path disclosure.',
|
||||
'functions' => array(
|
||||
'error_reporting',
|
||||
'phpinfo',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* WordPress Coding Standard.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
* @link https://github.com/WordPress/WordPress-Coding-Standards
|
||||
* @license https://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
namespace WordPressCS\WordPress\Sniffs\PHP;
|
||||
|
||||
use WordPressCS\WordPress\Sniff;
|
||||
use PHP_CodeSniffer\Util\Tokens;
|
||||
|
||||
/**
|
||||
* Disallow the use of short ternaries.
|
||||
*
|
||||
* @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#ternary-operator
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
*
|
||||
* @since 2.2.0
|
||||
*/
|
||||
class DisallowShortTernarySniff extends Sniff {
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @since 2.2.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register() {
|
||||
return array( \T_INLINE_THEN );
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @since 2.2.0
|
||||
*
|
||||
* @param int $stackPtr The position of the current token in the stack.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function process_token( $stackPtr ) {
|
||||
|
||||
$nextNonEmpty = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true );
|
||||
if ( false === $nextNonEmpty ) {
|
||||
// Live coding or parse error.
|
||||
return;
|
||||
}
|
||||
|
||||
if ( \T_INLINE_ELSE !== $this->tokens[ $nextNonEmpty ]['code'] ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->phpcsFile->addError(
|
||||
'Using short ternaries is not allowed',
|
||||
$stackPtr,
|
||||
'Found'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/**
|
||||
* WordPress Coding Standard.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
* @link https://github.com/WordPress/WordPress-Coding-Standards
|
||||
* @license https://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
namespace WordPressCS\WordPress\Sniffs\PHP;
|
||||
|
||||
use WordPressCS\WordPress\AbstractFunctionRestrictionsSniff;
|
||||
|
||||
/**
|
||||
* Discourages the use of various native PHP functions and suggests alternatives.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
*
|
||||
* @since 0.11.0
|
||||
* @since 0.13.0 Class name changed: this class is now namespaced.
|
||||
* @since 0.14.0 `create_function` was moved to the PHP.RestrictedFunctions sniff.
|
||||
*/
|
||||
class DiscouragedPHPFunctionsSniff extends AbstractFunctionRestrictionsSniff {
|
||||
|
||||
/**
|
||||
* Groups of functions to discourage.
|
||||
*
|
||||
* Example: groups => array(
|
||||
* 'lambda' => array(
|
||||
* 'type' => 'error' | 'warning',
|
||||
* 'message' => 'Use anonymous functions instead please!',
|
||||
* 'functions' => array( 'file_get_contents', 'create_function' ),
|
||||
* )
|
||||
* )
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getGroups() {
|
||||
return array(
|
||||
'serialize' => array(
|
||||
'type' => 'warning',
|
||||
'message' => '%s() found. Serialized data has known vulnerability problems with Object Injection. JSON is generally a better approach for serializing data. See https://www.owasp.org/index.php/PHP_Object_Injection',
|
||||
'functions' => array(
|
||||
'serialize',
|
||||
'unserialize',
|
||||
),
|
||||
),
|
||||
|
||||
'urlencode' => array(
|
||||
'type' => 'warning',
|
||||
'message' => '%s() should only be used when dealing with legacy applications rawurlencode() should now be used instead. See http://php.net/manual/en/function.rawurlencode.php and http://www.faqs.org/rfcs/rfc3986.html',
|
||||
'functions' => array(
|
||||
'urlencode',
|
||||
),
|
||||
),
|
||||
|
||||
'runtime_configuration' => array(
|
||||
'type' => 'warning',
|
||||
'message' => '%s() found. Changing configuration values at runtime is strongly discouraged.',
|
||||
'functions' => array(
|
||||
'error_reporting',
|
||||
'ini_restore',
|
||||
'apache_setenv',
|
||||
'putenv',
|
||||
'set_include_path',
|
||||
'restore_include_path',
|
||||
// This alias was DEPRECATED in PHP 5.3.0, and REMOVED as of PHP 7.0.0.
|
||||
'magic_quotes_runtime',
|
||||
// Warning This function was DEPRECATED in PHP 5.3.0, and REMOVED as of PHP 7.0.0.
|
||||
'set_magic_quotes_runtime',
|
||||
// Warning This function was removed from most SAPIs in PHP 5.3.0, and was removed from PHP-FPM in PHP 7.0.0.
|
||||
'dl',
|
||||
),
|
||||
),
|
||||
|
||||
'system_calls' => array(
|
||||
'type' => 'warning',
|
||||
'message' => '%s() found. PHP system calls are often disabled by server admins.',
|
||||
'functions' => array(
|
||||
'exec',
|
||||
'passthru',
|
||||
'proc_open',
|
||||
'shell_exec',
|
||||
'system',
|
||||
'popen',
|
||||
),
|
||||
),
|
||||
|
||||
'obfuscation' => array(
|
||||
'type' => 'warning',
|
||||
'message' => '%s() can be used to obfuscate code which is strongly discouraged. Please verify that the function is used for benign reasons.',
|
||||
'functions' => array(
|
||||
'base64_decode',
|
||||
'base64_encode',
|
||||
'convert_uudecode',
|
||||
'convert_uuencode',
|
||||
'str_rot13',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
/**
|
||||
* WordPress Coding Standard.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
* @link https://github.com/WordPress/WordPress-Coding-Standards
|
||||
* @license https://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
namespace WordPressCS\WordPress\Sniffs\PHP;
|
||||
|
||||
use WordPressCS\WordPress\AbstractFunctionRestrictionsSniff;
|
||||
|
||||
/**
|
||||
* Restricts the usage of extract().
|
||||
*
|
||||
* @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#dont-extract
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
*
|
||||
* @since 0.10.0 Previously this check was contained within the
|
||||
* `WordPress.VIP.RestrictedFunctions` sniff.
|
||||
* @since 0.13.0 Class name changed: this class is now namespaced.
|
||||
* @since 1.0.0 This sniff has been moved from the `Functions` category to the `PHP` category.
|
||||
*/
|
||||
class DontExtractSniff extends AbstractFunctionRestrictionsSniff {
|
||||
|
||||
/**
|
||||
* Groups of functions to restrict.
|
||||
*
|
||||
* Example: groups => array(
|
||||
* 'lambda' => array(
|
||||
* 'type' => 'error' | 'warning',
|
||||
* 'message' => 'Use anonymous functions instead please!',
|
||||
* 'functions' => array( 'file_get_contents', 'create_function' ),
|
||||
* )
|
||||
* )
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getGroups() {
|
||||
return array(
|
||||
|
||||
'extract' => array(
|
||||
'type' => 'error',
|
||||
'message' => '%s() usage is highly discouraged, due to the complexity and unintended issues it might cause.',
|
||||
'functions' => array(
|
||||
'extract',
|
||||
),
|
||||
),
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
/**
|
||||
* WordPress Coding Standard.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
* @link https://github.com/WordPress/WordPress-Coding-Standards
|
||||
* @license https://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
namespace WordPressCS\WordPress\Sniffs\PHP;
|
||||
|
||||
use WordPressCS\WordPress\AbstractFunctionParameterSniff;
|
||||
|
||||
/**
|
||||
* Detect use of the `ini_set()` function.
|
||||
*
|
||||
* - Won't throw notices for "safe" ini directives as listed in the whitelist.
|
||||
* - Throws errors for ini directives listed in the blacklist.
|
||||
* - A warning will be thrown in all other cases.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
*
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class IniSetSniff extends AbstractFunctionParameterSniff {
|
||||
|
||||
/**
|
||||
* Array of functions that must be checked.
|
||||
*
|
||||
* @since 2.1.0
|
||||
*
|
||||
* @var array Multidimensional array with parameter details.
|
||||
* $target_functions = array(
|
||||
* (string) Function name.
|
||||
* );
|
||||
*/
|
||||
protected $target_functions = array(
|
||||
'ini_set' => true,
|
||||
'ini_alter' => true,
|
||||
);
|
||||
|
||||
/**
|
||||
* Array of PHP configuration options that are allowed to be manipulated.
|
||||
*
|
||||
* @since 2.1.0
|
||||
*
|
||||
* @var array Multidimensional array with parameter details.
|
||||
* $whitelisted_options = array(
|
||||
* (string) option name. = array(
|
||||
* (string[]) 'valid_values' = array()
|
||||
* )
|
||||
* );
|
||||
*/
|
||||
protected $whitelisted_options = array(
|
||||
'auto_detect_line_endings' => array(),
|
||||
'highlight.bg' => array(),
|
||||
'highlight.comment' => array(),
|
||||
'highlight.default' => array(),
|
||||
'highlight.html' => array(),
|
||||
'highlight.keyword' => array(),
|
||||
'highlight.string' => array(),
|
||||
'short_open_tag' => array(
|
||||
'valid_values' => array( 'true', '1', 'on' ),
|
||||
),
|
||||
);
|
||||
|
||||
/**
|
||||
* Array of PHP configuration options that are not allowed to be manipulated.
|
||||
*
|
||||
* @since 2.1.0
|
||||
*
|
||||
* @var array Multidimensional array with parameter details.
|
||||
* $blacklisted_options = array(
|
||||
* (string) option name. = array(
|
||||
* (string[]) 'invalid_values' = array()
|
||||
* (string) 'message'
|
||||
* )
|
||||
* );
|
||||
*/
|
||||
protected $blacklisted_options = array(
|
||||
'bcmath.scale' => array(
|
||||
'message' => 'Use `bcscale()` instead.',
|
||||
),
|
||||
'display_errors' => array(
|
||||
'message' => 'Use `WP_DEBUG_DISPLAY` instead.',
|
||||
),
|
||||
'error_reporting' => array(
|
||||
'message' => 'Use `WP_DEBUG` instead.',
|
||||
),
|
||||
'filter.default' => array(
|
||||
'message' => 'Changing the option value can break other plugins. Use the filter flag constants when calling the Filter functions instead.',
|
||||
),
|
||||
'filter.default_flags' => array(
|
||||
'message' => 'Changing the option value can break other plugins. Use the filter flag constants when calling the Filter functions instead.',
|
||||
),
|
||||
'iconv.input_encoding' => array(
|
||||
'message' => 'PHP < 5.6 only - use `iconv_set_encoding()` instead.',
|
||||
),
|
||||
'iconv.internal_encoding' => array(
|
||||
'message' => 'PHP < 5.6 only - use `iconv_set_encoding()` instead.',
|
||||
),
|
||||
'iconv.output_encoding' => array(
|
||||
'message' => 'PHP < 5.6 only - use `iconv_set_encoding()` instead.',
|
||||
),
|
||||
'ignore_user_abort' => array(
|
||||
'message' => 'Use `ignore_user_abort()` instead.',
|
||||
),
|
||||
'log_errors' => array(
|
||||
'message' => 'Use `WP_DEBUG_LOG` instead.',
|
||||
),
|
||||
'max_execution_time' => array(
|
||||
'message' => 'Use `set_time_limit()` instead.',
|
||||
),
|
||||
'memory_limit' => array(
|
||||
'message' => 'Use `wp_raise_memory_limit()` or hook into the filters in that function.',
|
||||
),
|
||||
'short_open_tag' => array(
|
||||
'invalid_values' => array( 'false', '0', 'off' ),
|
||||
'message' => 'Turning off short_open_tag is prohibited as it can break other plugins.',
|
||||
),
|
||||
);
|
||||
|
||||
/**
|
||||
* Process the parameter of a matched function.
|
||||
*
|
||||
* Errors if an option is found in the blacklist. Warns as
|
||||
* 'risky' when the option is not found in the whitelist.
|
||||
*
|
||||
* @since 2.1.0
|
||||
*
|
||||
* @param int $stackPtr The position of the current token in the stack.
|
||||
* @param string $group_name The name of the group which was matched.
|
||||
* @param string $matched_content The token content (function name) which was matched.
|
||||
* @param array $parameters Array with information about the parameters.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function process_parameters( $stackPtr, $group_name, $matched_content, $parameters ) {
|
||||
$option_name = $this->strip_quotes( $parameters[1]['raw'] );
|
||||
$option_value = $this->strip_quotes( $parameters[2]['raw'] );
|
||||
if ( isset( $this->whitelisted_options[ $option_name ] ) ) {
|
||||
$whitelisted_option = $this->whitelisted_options[ $option_name ];
|
||||
if ( ! isset( $whitelisted_option['valid_values'] ) || in_array( strtolower( $option_value ), $whitelisted_option['valid_values'], true ) ) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $this->blacklisted_options[ $option_name ] ) ) {
|
||||
$blacklisted_option = $this->blacklisted_options[ $option_name ];
|
||||
if ( ! isset( $blacklisted_option['invalid_values'] ) || in_array( strtolower( $option_value ), $blacklisted_option['invalid_values'], true ) ) {
|
||||
$this->phpcsFile->addError(
|
||||
'%s(%s, %s) found. %s',
|
||||
$stackPtr,
|
||||
$this->string_to_errorcode( $option_name . '_Blacklisted' ),
|
||||
array(
|
||||
$matched_content,
|
||||
$parameters[1]['raw'],
|
||||
$parameters[2]['raw'],
|
||||
$blacklisted_option['message'],
|
||||
)
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$this->phpcsFile->addWarning(
|
||||
'%s(%s, %s) found. Changing configuration values at runtime is strongly discouraged.',
|
||||
$stackPtr,
|
||||
'Risky',
|
||||
array(
|
||||
$matched_content,
|
||||
$parameters[1]['raw'],
|
||||
$parameters[2]['raw'],
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
+239
@@ -0,0 +1,239 @@
|
||||
<?php
|
||||
/**
|
||||
* WordPress Coding Standard.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
* @link https://github.com/WordPress/WordPress-Coding-Standards
|
||||
* @license https://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
namespace WordPressCS\WordPress\Sniffs\PHP;
|
||||
|
||||
use WordPressCS\WordPress\Sniff;
|
||||
use PHP_CodeSniffer\Util\Tokens;
|
||||
|
||||
/**
|
||||
* Discourage the use of the PHP error silencing operator.
|
||||
*
|
||||
* This sniff allows the error operator to be used with a select list
|
||||
* of whitelisted functions, as no amount of error checking can prevent
|
||||
* PHP from throwing errors when those functions are used.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
*
|
||||
* @since 1.1.0
|
||||
*/
|
||||
class NoSilencedErrorsSniff extends Sniff {
|
||||
|
||||
/**
|
||||
* Number of tokens to display in the error message to show
|
||||
* the error silencing context.
|
||||
*
|
||||
* @since 1.1.0
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $context_length = 6;
|
||||
|
||||
/**
|
||||
* Whether or not the `$function_whitelist` should be used.
|
||||
*
|
||||
* Defaults to true.
|
||||
*
|
||||
* This property only affects whether the standard function whitelist is
|
||||
* used. The custom whitelist, if set, will always be respected.
|
||||
*
|
||||
* @since 1.1.0
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $use_default_whitelist = true;
|
||||
|
||||
/**
|
||||
* User defined whitelist.
|
||||
*
|
||||
* Allows users to pass a list of additional functions to whitelist
|
||||
* from their custom ruleset.
|
||||
*
|
||||
* @since 1.1.0
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $custom_whitelist = array();
|
||||
|
||||
/**
|
||||
* PHP native function whitelist.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @since 1.1.0
|
||||
*
|
||||
* @var array <string function name> => <bool true>
|
||||
*/
|
||||
protected $function_whitelist = 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,
|
||||
|
||||
// Miscellaneous other functions.
|
||||
'imagecreatefromstring' => true,
|
||||
'parse_url' => true, // Pre-PHP 5.3.3 an E_WARNING was thrown when URL parsing failed.
|
||||
'unserialize' => true,
|
||||
);
|
||||
|
||||
/**
|
||||
* Tokens which are regarded as empty for the purpose of determining
|
||||
* the name of the called function.
|
||||
*
|
||||
* This property is set from within the register() method.
|
||||
*
|
||||
* @since 1.1.0
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $empty_tokens = array();
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @since 1.1.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register() {
|
||||
$this->empty_tokens = Tokens::$emptyTokens;
|
||||
$this->empty_tokens[ \T_NS_SEPARATOR ] = \T_NS_SEPARATOR;
|
||||
$this->empty_tokens[ \T_BITWISE_AND ] = \T_BITWISE_AND;
|
||||
|
||||
return array(
|
||||
\T_ASPERAND,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @since 1.1.0
|
||||
*
|
||||
* @param int $stackPtr The position of the current token in the stack.
|
||||
*/
|
||||
public function process_token( $stackPtr ) {
|
||||
// Handle the user-defined custom function whitelist.
|
||||
$this->custom_whitelist = $this->merge_custom_array( $this->custom_whitelist, array(), false );
|
||||
$this->custom_whitelist = array_map( 'strtolower', $this->custom_whitelist );
|
||||
|
||||
/*
|
||||
* Check if the error silencing is done for one of the whitelisted functions.
|
||||
*
|
||||
* @internal The function call name determination is done even when there is no whitelist active
|
||||
* to allow the metrics to be more informative.
|
||||
*/
|
||||
$next_non_empty = $this->phpcsFile->findNext( $this->empty_tokens, ( $stackPtr + 1 ), null, true, null, true );
|
||||
if ( false !== $next_non_empty && \T_STRING === $this->tokens[ $next_non_empty ]['code'] ) {
|
||||
$has_parenthesis = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $next_non_empty + 1 ), null, true, null, true );
|
||||
if ( false !== $has_parenthesis && \T_OPEN_PARENTHESIS === $this->tokens[ $has_parenthesis ]['code'] ) {
|
||||
$function_name = strtolower( $this->tokens[ $next_non_empty ]['content'] );
|
||||
if ( ( true === $this->use_default_whitelist
|
||||
&& isset( $this->function_whitelist[ $function_name ] ) === true )
|
||||
|| ( ! empty( $this->custom_whitelist )
|
||||
&& in_array( $function_name, $this->custom_whitelist, true ) === true )
|
||||
) {
|
||||
$this->phpcsFile->recordMetric( $stackPtr, 'Error silencing', 'whitelisted function call: ' . $function_name );
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->context_length = (int) $this->context_length;
|
||||
$context_length = $this->context_length;
|
||||
if ( $this->context_length <= 0 ) {
|
||||
$context_length = 2;
|
||||
}
|
||||
|
||||
// Prepare the "Found" string to display.
|
||||
$end_of_statement = $this->phpcsFile->findEndOfStatement( $stackPtr, \T_COMMA );
|
||||
if ( ( $end_of_statement - $stackPtr ) < $context_length ) {
|
||||
$context_length = ( $end_of_statement - $stackPtr );
|
||||
}
|
||||
$found = $this->phpcsFile->getTokensAsString( $stackPtr, $context_length );
|
||||
$found = str_replace( array( "\t", "\n", "\r" ), ' ', $found ) . '...';
|
||||
|
||||
$error_msg = 'Silencing errors is strongly discouraged. Use proper error checking instead.';
|
||||
$data = array();
|
||||
if ( $this->context_length > 0 ) {
|
||||
$error_msg .= ' Found: %s';
|
||||
$data[] = $found;
|
||||
}
|
||||
|
||||
$this->phpcsFile->addWarning(
|
||||
$error_msg,
|
||||
$stackPtr,
|
||||
'Discouraged',
|
||||
$data
|
||||
);
|
||||
|
||||
if ( isset( $function_name ) ) {
|
||||
$this->phpcsFile->recordMetric( $stackPtr, 'Error silencing', '@' . $function_name );
|
||||
} else {
|
||||
$this->phpcsFile->recordMetric( $stackPtr, 'Error silencing', $found );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
/**
|
||||
* WordPress Coding Standard.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
* @link https://github.com/WordPress/WordPress-Coding-Standards
|
||||
* @license https://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
namespace WordPressCS\WordPress\Sniffs\PHP;
|
||||
|
||||
use WordPressCS\WordPress\AbstractFunctionRestrictionsSniff;
|
||||
|
||||
/**
|
||||
* Perl compatible regular expressions (PCRE, preg_ functions) should be used in preference
|
||||
* to their POSIX counterparts.
|
||||
*
|
||||
* @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#regular-expressions
|
||||
* @link http://php.net/manual/en/ref.regex.php
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
*
|
||||
* @since 0.10.0 Previously this check was contained within the
|
||||
* `WordPress.VIP.RestrictedFunctions` and the
|
||||
* `WordPress.PHP.DiscouragedPHPFunctions` sniffs.
|
||||
* @since 0.13.0 Class name changed: this class is now namespaced.
|
||||
*/
|
||||
class POSIXFunctionsSniff extends AbstractFunctionRestrictionsSniff {
|
||||
|
||||
/**
|
||||
* Groups of functions to restrict.
|
||||
*
|
||||
* Example: groups => array(
|
||||
* 'lambda' => array(
|
||||
* 'type' => 'error' | 'warning',
|
||||
* 'message' => 'Use anonymous functions instead please!',
|
||||
* 'functions' => array( 'file_get_contents', 'create_function' ),
|
||||
* )
|
||||
* )
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getGroups() {
|
||||
return array(
|
||||
'ereg' => array(
|
||||
'type' => 'error',
|
||||
'message' => '%s() has been deprecated since PHP 5.3 and removed in PHP 7.0, please use preg_match() instead.',
|
||||
'functions' => array(
|
||||
'ereg',
|
||||
'eregi',
|
||||
'sql_regcase',
|
||||
),
|
||||
),
|
||||
|
||||
'ereg_replace' => array(
|
||||
'type' => 'error',
|
||||
'message' => '%s() has been deprecated since PHP 5.3 and removed in PHP 7.0, please use preg_replace() instead.',
|
||||
'functions' => array(
|
||||
'ereg_replace',
|
||||
'eregi_replace',
|
||||
),
|
||||
),
|
||||
|
||||
'split' => array(
|
||||
'type' => 'error',
|
||||
'message' => '%s() has been deprecated since PHP 5.3 and removed in PHP 7.0, please use explode(), str_split() or preg_split() instead.',
|
||||
'functions' => array(
|
||||
'split',
|
||||
'spliti',
|
||||
),
|
||||
),
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
/**
|
||||
* WordPress Coding Standard.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
* @link https://github.com/WordPress/WordPress-Coding-Standards
|
||||
* @license https://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
namespace WordPressCS\WordPress\Sniffs\PHP;
|
||||
|
||||
use WordPressCS\WordPress\AbstractFunctionParameterSniff;
|
||||
|
||||
/**
|
||||
* Flag calling preg_quote() without the second ($delimiter) parameter.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class PregQuoteDelimiterSniff extends AbstractFunctionParameterSniff {
|
||||
|
||||
/**
|
||||
* The group name for this group of functions.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group_name = 'preg_quote';
|
||||
|
||||
/**
|
||||
* List of functions this sniff should examine.
|
||||
*
|
||||
* @link http://php.net/preg_quote
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @var array <string function_name> => <bool>
|
||||
*/
|
||||
protected $target_functions = array(
|
||||
'preg_quote' => true,
|
||||
);
|
||||
|
||||
/**
|
||||
* Process the parameters of a matched function.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param int $stackPtr The position of the current token in the stack.
|
||||
* @param string $group_name The name of the group which was matched.
|
||||
* @param string $matched_content The token content (function name) which was matched.
|
||||
* @param array $parameters Array with information about the parameters.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function process_parameters( $stackPtr, $group_name, $matched_content, $parameters ) {
|
||||
if ( \count( $parameters ) > 1 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->phpcsFile->addWarning(
|
||||
'Passing the $delimiter as the second parameter to preg_quote() is strongly recommended.',
|
||||
$stackPtr,
|
||||
'Missing'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* WordPress Coding Standard.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
* @link https://github.com/WordPress/WordPress-Coding-Standards
|
||||
* @license https://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
namespace WordPressCS\WordPress\Sniffs\PHP;
|
||||
|
||||
use WordPressCS\WordPress\AbstractFunctionRestrictionsSniff;
|
||||
|
||||
/**
|
||||
* Forbids the use of various native PHP functions and suggests alternatives.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
*
|
||||
* @since 0.14.0
|
||||
*/
|
||||
class RestrictedPHPFunctionsSniff extends AbstractFunctionRestrictionsSniff {
|
||||
|
||||
/**
|
||||
* Groups of functions to forbid.
|
||||
*
|
||||
* Example: groups => array(
|
||||
* 'lambda' => array(
|
||||
* 'type' => 'error' | 'warning',
|
||||
* 'message' => 'Use anonymous functions instead please!',
|
||||
* 'functions' => array( 'file_get_contents', 'create_function' ),
|
||||
* )
|
||||
* )
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getGroups() {
|
||||
return array(
|
||||
'create_function' => array(
|
||||
'type' => 'error',
|
||||
'message' => '%s() is deprecated as of PHP 7.2, please use full fledged functions or anonymous functions instead.',
|
||||
'functions' => array(
|
||||
'create_function',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/**
|
||||
* WordPress Coding Standard.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
* @link https://github.com/WordPress/WordPress-Coding-Standards
|
||||
* @license https://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
namespace WordPressCS\WordPress\Sniffs\PHP;
|
||||
|
||||
use WordPressCS\WordPress\Sniff;
|
||||
|
||||
/**
|
||||
* Enforces Strict Comparison checks, based upon Squiz code.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
*
|
||||
* @since 0.4.0
|
||||
* @since 0.13.0 Class name changed: this class is now namespaced.
|
||||
*
|
||||
* Last synced with base class ?[unknown date]? at commit ?[unknown commit]?.
|
||||
* It is currently unclear whether this sniff is actually based on Squiz code on whether the above
|
||||
* reference to it is a copy/paste oversight.
|
||||
* @link Possibly: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Squiz/Sniffs/Operators/ComparisonOperatorUsageSniff.php
|
||||
*/
|
||||
class StrictComparisonsSniff extends Sniff {
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register() {
|
||||
return array(
|
||||
\T_IS_EQUAL,
|
||||
\T_IS_NOT_EQUAL,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @param int $stackPtr The position of the current token in the stack.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function process_token( $stackPtr ) {
|
||||
|
||||
if ( ! $this->has_whitelist_comment( 'loose comparison', $stackPtr ) ) {
|
||||
$error = 'Found: ' . $this->tokens[ $stackPtr ]['content'] . '. Use strict comparisons (=== or !==).';
|
||||
$this->phpcsFile->addWarning( $error, $stackPtr, 'LooseComparison' );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/**
|
||||
* WordPress Coding Standard.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
* @link https://github.com/WordPress/WordPress-Coding-Standards
|
||||
* @license https://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
namespace WordPressCS\WordPress\Sniffs\PHP;
|
||||
|
||||
use WordPressCS\WordPress\AbstractFunctionParameterSniff;
|
||||
|
||||
/**
|
||||
* Flag calling in_array(), array_search() and array_keys() without true as the third parameter.
|
||||
*
|
||||
* @link https://vip.wordpress.com/documentation/vip-go/code-review-blockers-warnings-notices/#using-in_array-without-strict-parameter
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
*
|
||||
* @since 0.9.0
|
||||
* @since 0.10.0 - This sniff not only checks for `in_array()`, but also `array_search()`
|
||||
* and `array_keys()`.
|
||||
* - The sniff no longer needlessly extends the `ArrayAssignmentRestrictionsSniff`
|
||||
* class which it didn't use.
|
||||
* @since 0.11.0 Refactored to extend the new WordPressCS native `AbstractFunctionParameterSniff` class.
|
||||
* @since 0.13.0 Class name changed: this class is now namespaced.
|
||||
*/
|
||||
class StrictInArraySniff extends AbstractFunctionParameterSniff {
|
||||
|
||||
/**
|
||||
* The group name for this group of functions.
|
||||
*
|
||||
* @since 0.11.0
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group_name = 'strict';
|
||||
|
||||
/**
|
||||
* List of array functions to which a $strict parameter can be passed.
|
||||
*
|
||||
* The $strict parameter is the third and last parameter for each of these functions.
|
||||
*
|
||||
* The array_keys() function only requires the $strict parameter when the optional
|
||||
* second parameter $search has been set.
|
||||
*
|
||||
* @link http://php.net/in-array
|
||||
* @link http://php.net/array-search
|
||||
* @link http://php.net/array-keys
|
||||
*
|
||||
* @since 0.10.0
|
||||
* @since 0.11.0 Renamed from $array_functions to $target_functions.
|
||||
*
|
||||
* @var array <string function_name> => <bool always needed ?>
|
||||
*/
|
||||
protected $target_functions = array(
|
||||
'in_array' => true,
|
||||
'array_search' => true,
|
||||
'array_keys' => false,
|
||||
);
|
||||
|
||||
/**
|
||||
* Process the parameters of a matched function.
|
||||
*
|
||||
* @since 0.11.0
|
||||
*
|
||||
* @param int $stackPtr The position of the current token in the stack.
|
||||
* @param string $group_name The name of the group which was matched.
|
||||
* @param string $matched_content The token content (function name) which was matched.
|
||||
* @param array $parameters Array with information about the parameters.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function process_parameters( $stackPtr, $group_name, $matched_content, $parameters ) {
|
||||
// Check if the strict check is actually needed.
|
||||
if ( false === $this->target_functions[ $matched_content ] ) {
|
||||
if ( \count( $parameters ) === 1 ) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// We're only interested in the third parameter.
|
||||
if ( false === isset( $parameters[3] ) || 'true' !== strtolower( $parameters[3]['raw'] ) ) {
|
||||
$errorcode = 'MissingTrueStrict';
|
||||
|
||||
/*
|
||||
* Use a different error code when `false` is found to allow for excluding
|
||||
* the warning as this will be a conscious choice made by the dev.
|
||||
*/
|
||||
if ( isset( $parameters[3] ) && 'false' === strtolower( $parameters[3]['raw'] ) ) {
|
||||
$errorcode = 'FoundNonStrictFalse';
|
||||
}
|
||||
|
||||
$this->phpcsFile->addWarning(
|
||||
'Not using strict comparison for %s; supply true for third argument.',
|
||||
( isset( $parameters[3]['start'] ) ? $parameters[3]['start'] : $parameters[1]['start'] ),
|
||||
$errorcode,
|
||||
array( $matched_content )
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
/**
|
||||
* WordPress Coding Standard.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
* @link https://github.com/WordPress/WordPress-Coding-Standards
|
||||
* @license https://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
namespace WordPressCS\WordPress\Sniffs\PHP;
|
||||
|
||||
use WordPressCS\WordPress\Sniff;
|
||||
|
||||
/**
|
||||
* Verifies the correct usage of type cast keywords.
|
||||
*
|
||||
* Type casts should be:
|
||||
* - normalized, i.e. (float) not (real).
|
||||
*
|
||||
* Additionally, the use of the (unset) and (binary) casts is discouraged.
|
||||
*
|
||||
* @link https://make.wordpress.org/core/handbook/best-practices/....
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
*
|
||||
* @since 1.2.0
|
||||
* @since 2.0.0 No longer checks that type casts are lowercase or short form.
|
||||
* Relevant PHPCS native sniffs have been included in the rulesets instead.
|
||||
*/
|
||||
class TypeCastsSniff extends Sniff {
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register() {
|
||||
return array(
|
||||
\T_DOUBLE_CAST,
|
||||
\T_UNSET_CAST,
|
||||
\T_STRING_CAST,
|
||||
\T_BINARY_CAST,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @param int $stackPtr The position of the current token in the stack.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function process_token( $stackPtr ) {
|
||||
|
||||
$token_code = $this->tokens[ $stackPtr ]['code'];
|
||||
$typecast = str_replace( ' ', '', $this->tokens[ $stackPtr ]['content'] );
|
||||
$typecast_lc = strtolower( $typecast );
|
||||
|
||||
switch ( $token_code ) {
|
||||
case \T_DOUBLE_CAST:
|
||||
if ( '(float)' !== $typecast_lc ) {
|
||||
$fix = $this->phpcsFile->addFixableError(
|
||||
'Normalized type keywords must be used; expected "(float)" but found "%s"',
|
||||
$stackPtr,
|
||||
'DoubleRealFound',
|
||||
array( $typecast )
|
||||
);
|
||||
|
||||
if ( true === $fix ) {
|
||||
$this->phpcsFile->fixer->replaceToken( $stackPtr, '(float)' );
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case \T_UNSET_CAST:
|
||||
$this->phpcsFile->addWarning(
|
||||
'Using the "(unset)" cast is strongly discouraged. Use the "unset()" language construct or assign "null" as the value to the variable instead.',
|
||||
$stackPtr,
|
||||
'UnsetFound'
|
||||
);
|
||||
break;
|
||||
|
||||
case \T_STRING_CAST:
|
||||
case \T_BINARY_CAST:
|
||||
if ( \T_STRING_CAST === $token_code && '(binary)' !== $typecast_lc ) {
|
||||
break;
|
||||
}
|
||||
|
||||
$this->phpcsFile->addWarning(
|
||||
'Using binary casting is strongly discouraged. Found: "%s"',
|
||||
$stackPtr,
|
||||
'BinaryFound',
|
||||
array( $typecast )
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
/**
|
||||
* WordPress Coding Standard.
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
* @link https://github.com/WordPress/WordPress-Coding-Standards
|
||||
* @license https://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
namespace WordPressCS\WordPress\Sniffs\PHP;
|
||||
|
||||
use WordPressCS\WordPress\Sniff;
|
||||
use PHP_CodeSniffer\Util\Tokens;
|
||||
|
||||
/**
|
||||
* Enforces Yoda conditional statements.
|
||||
*
|
||||
* @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#yoda-conditions
|
||||
*
|
||||
* @package WPCS\WordPressCodingStandards
|
||||
*
|
||||
* @since 0.3.0
|
||||
* @since 0.12.0 This class now extends the WordPressCS native `Sniff` class.
|
||||
* @since 0.13.0 Class name changed: this class is now namespaced.
|
||||
*/
|
||||
class YodaConditionsSniff extends Sniff {
|
||||
|
||||
/**
|
||||
* The tokens that indicate the start of a condition.
|
||||
*
|
||||
* @since 0.12.0
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $condition_start_tokens;
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register() {
|
||||
|
||||
$starters = Tokens::$booleanOperators;
|
||||
$starters += Tokens::$assignmentTokens;
|
||||
$starters[ \T_CASE ] = \T_CASE;
|
||||
$starters[ \T_RETURN ] = \T_RETURN;
|
||||
$starters[ \T_INLINE_THEN ] = \T_INLINE_THEN;
|
||||
$starters[ \T_INLINE_ELSE ] = \T_INLINE_ELSE;
|
||||
$starters[ \T_SEMICOLON ] = \T_SEMICOLON;
|
||||
$starters[ \T_OPEN_PARENTHESIS ] = \T_OPEN_PARENTHESIS;
|
||||
|
||||
$this->condition_start_tokens = $starters;
|
||||
|
||||
return array(
|
||||
\T_IS_EQUAL,
|
||||
\T_IS_NOT_EQUAL,
|
||||
\T_IS_IDENTICAL,
|
||||
\T_IS_NOT_IDENTICAL,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @param int $stackPtr The position of the current token in the stack.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function process_token( $stackPtr ) {
|
||||
|
||||
$start = $this->phpcsFile->findPrevious( $this->condition_start_tokens, $stackPtr, null, false, null, true );
|
||||
|
||||
$needs_yoda = false;
|
||||
|
||||
// Note: going backwards!
|
||||
for ( $i = $stackPtr; $i > $start; $i-- ) {
|
||||
|
||||
// Ignore whitespace.
|
||||
if ( isset( Tokens::$emptyTokens[ $this->tokens[ $i ]['code'] ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If this is a variable or array, we've seen all we need to see.
|
||||
if ( \T_VARIABLE === $this->tokens[ $i ]['code']
|
||||
|| \T_CLOSE_SQUARE_BRACKET === $this->tokens[ $i ]['code']
|
||||
) {
|
||||
$needs_yoda = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// If this is a function call or something, we are OK.
|
||||
if ( \T_CLOSE_PARENTHESIS === $this->tokens[ $i ]['code'] ) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $needs_yoda ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if this is a var to var comparison, e.g.: if ( $var1 == $var2 ).
|
||||
$next_non_empty = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true );
|
||||
|
||||
if ( isset( Tokens::$castTokens[ $this->tokens[ $next_non_empty ]['code'] ] ) ) {
|
||||
$next_non_empty = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $next_non_empty + 1 ), null, true );
|
||||
}
|
||||
|
||||
if ( \in_array( $this->tokens[ $next_non_empty ]['code'], array( \T_SELF, \T_PARENT, \T_STATIC ), true ) ) {
|
||||
$next_non_empty = $this->phpcsFile->findNext(
|
||||
( Tokens::$emptyTokens + array( \T_DOUBLE_COLON => \T_DOUBLE_COLON ) ),
|
||||
( $next_non_empty + 1 ),
|
||||
null,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
if ( \T_VARIABLE === $this->tokens[ $next_non_empty ]['code'] ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->phpcsFile->addError( 'Use Yoda Condition checks, you must.', $stackPtr, 'NotYoda' );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user