|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * WordPress Coding Standard. |
| 4 | + * |
| 5 | + * @package WPCS\WordPressCodingStandards |
| 6 | + * @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards |
| 7 | + * @license https://opensource.org/licenses/MIT MIT |
| 8 | + */ |
| 9 | + |
| 10 | +namespace WordPressCS\WordPress\Sniffs\PHP; |
| 11 | + |
| 12 | +use WordPressCS\WordPress\AbstractFunctionParameterSniff; |
| 13 | + |
| 14 | +/** |
| 15 | + * Detect use of the `ini_set()` function. |
| 16 | + * |
| 17 | + * - Won't throw notices for "safe" ini directives as listed in the whitelist. |
| 18 | + * - Throws errors for ini directives listed in the blacklist. |
| 19 | + * - A warning will be thrown in all other cases. |
| 20 | + * |
| 21 | + * @package WPCS\WordPressCodingStandards |
| 22 | + * |
| 23 | + * @since 2.1.0 |
| 24 | + */ |
| 25 | +class IniSetSniff extends AbstractFunctionParameterSniff { |
| 26 | + |
| 27 | + /** |
| 28 | + * Array of functions that must be checked. |
| 29 | + * |
| 30 | + * @since 2.1.0 |
| 31 | + * |
| 32 | + * @var array Multidimensional array with parameter details. |
| 33 | + * $target_functions = array( |
| 34 | + * (string) Function name. |
| 35 | + * ); |
| 36 | + */ |
| 37 | + protected $target_functions = array( |
| 38 | + 'ini_set' => true, |
| 39 | + 'ini_alter' => true, |
| 40 | + ); |
| 41 | + |
| 42 | + /** |
| 43 | + * Array of PHP configuration options that are allowed to be manipulated. |
| 44 | + * |
| 45 | + * @since 2.1.0 |
| 46 | + * |
| 47 | + * @var array Multidimensional array with parameter details. |
| 48 | + * $whitelisted_options = array( |
| 49 | + * (string) option name. = array( |
| 50 | + * (string[]) 'valid_values' = array() |
| 51 | + * ) |
| 52 | + * ); |
| 53 | + */ |
| 54 | + protected $whitelisted_options = array( |
| 55 | + 'auto_detect_line_endings' => array(), |
| 56 | + 'highlight.bg' => array(), |
| 57 | + 'highlight.comment' => array(), |
| 58 | + 'highlight.default' => array(), |
| 59 | + 'highlight.html' => array(), |
| 60 | + 'highlight.keyword' => array(), |
| 61 | + 'highlight.string' => array(), |
| 62 | + 'short_open_tag' => array( |
| 63 | + 'valid_values' => array( 'true', '1', 'on' ), |
| 64 | + ), |
| 65 | + ); |
| 66 | + |
| 67 | + /** |
| 68 | + * Array of PHP configuration options that are not allowed to be manipulated. |
| 69 | + * |
| 70 | + * @since 2.1.0 |
| 71 | + * |
| 72 | + * @var array Multidimensional array with parameter details. |
| 73 | + * $blacklisted_options = array( |
| 74 | + * (string) option name. = array( |
| 75 | + * (string[]) 'invalid_values' = array() |
| 76 | + * (string) 'message' |
| 77 | + * ) |
| 78 | + * ); |
| 79 | + */ |
| 80 | + protected $blacklisted_options = array( |
| 81 | + 'bcmath.scale' => array( |
| 82 | + 'message' => 'Use `bcscale()` instead.', |
| 83 | + ), |
| 84 | + 'display_errors' => array( |
| 85 | + 'message' => 'Use `WP_DEBUG_DISPLAY` instead.', |
| 86 | + ), |
| 87 | + 'error_reporting' => array( |
| 88 | + 'message' => 'Use `WP_DEBUG` instead.', |
| 89 | + ), |
| 90 | + 'filter.default' => array( |
| 91 | + 'message' => 'Changing the option value can break other plugins. Use the filter flag constants when calling the Filter functions instead.', |
| 92 | + ), |
| 93 | + 'filter.default_flags' => array( |
| 94 | + 'message' => 'Changing the option value can break other plugins. Use the filter flag constants when calling the Filter functions instead.', |
| 95 | + ), |
| 96 | + 'iconv.input_encoding' => array( |
| 97 | + 'message' => 'PHP < 5.6 only - use `iconv_set_encoding()` instead.', |
| 98 | + ), |
| 99 | + 'iconv.internal_encoding' => array( |
| 100 | + 'message' => 'PHP < 5.6 only - use `iconv_set_encoding()` instead.', |
| 101 | + ), |
| 102 | + 'iconv.output_encoding' => array( |
| 103 | + 'message' => 'PHP < 5.6 only - use `iconv_set_encoding()` instead.', |
| 104 | + ), |
| 105 | + 'ignore_user_abort' => array( |
| 106 | + 'message' => 'Use `ignore_user_abort()` instead.', |
| 107 | + ), |
| 108 | + 'log_errors' => array( |
| 109 | + 'message' => 'Use `WP_DEBUG_LOG` instead.', |
| 110 | + ), |
| 111 | + 'max_execution_time' => array( |
| 112 | + 'message' => 'Use `set_time_limit()` instead.', |
| 113 | + ), |
| 114 | + 'memory_limit' => array( |
| 115 | + 'message' => 'Use `wp_raise_memory_limit()` or hook into the filters in that function.', |
| 116 | + ), |
| 117 | + 'short_open_tag' => array( |
| 118 | + 'invalid_values' => array( 'false', '0', 'off' ), |
| 119 | + 'message' => 'Turning off short_open_tag is prohibited as it can break other plugins.', |
| 120 | + ), |
| 121 | + ); |
| 122 | + |
| 123 | + /** |
| 124 | + * Process the parameter of a matched function. |
| 125 | + * |
| 126 | + * Errors if an option is found in the blacklist. Warns as |
| 127 | + * 'risky' when the option is not found in the whitelist. |
| 128 | + * |
| 129 | + * @since 2.1.0 |
| 130 | + * |
| 131 | + * @param int $stackPtr The position of the current token in the stack. |
| 132 | + * @param array $group_name The name of the group which was matched. |
| 133 | + * @param string $matched_content The token content (function name) which was matched. |
| 134 | + * @param array $parameters Array with information about the parameters. |
| 135 | + * |
| 136 | + * @return void |
| 137 | + */ |
| 138 | + public function process_parameters( $stackPtr, $group_name, $matched_content, $parameters ) { |
| 139 | + $option_name = $this->strip_quotes( $parameters[1]['raw'] ); |
| 140 | + $option_value = $this->strip_quotes( $parameters[2]['raw'] ); |
| 141 | + if ( isset( $this->whitelisted_options[ $option_name ] ) ) { |
| 142 | + $whitelisted_option = $this->whitelisted_options[ $option_name ]; |
| 143 | + if ( ! isset( $whitelisted_option['valid_values'] ) || in_array( strtolower( $option_value ), $whitelisted_option['valid_values'], true ) ) { |
| 144 | + return; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + if ( isset( $this->blacklisted_options[ $option_name ] ) ) { |
| 149 | + $blacklisted_option = $this->blacklisted_options[ $option_name ]; |
| 150 | + if ( ! isset( $blacklisted_option['invalid_values'] ) || in_array( strtolower( $option_value ), $blacklisted_option['invalid_values'], true ) ) { |
| 151 | + $this->phpcsFile->addError( |
| 152 | + '%s(%s, %s) found. %s', |
| 153 | + $stackPtr, |
| 154 | + $this->string_to_errorcode( $option_name . '_Blacklisted' ), |
| 155 | + array( |
| 156 | + $matched_content, |
| 157 | + $parameters[1]['raw'], |
| 158 | + $parameters[2]['raw'], |
| 159 | + $blacklisted_option['message'], |
| 160 | + ) |
| 161 | + ); |
| 162 | + return; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + $this->phpcsFile->addWarning( |
| 167 | + '%s(%s, %s) found. Changing configuration values at runtime is strongly discouraged.', |
| 168 | + $stackPtr, |
| 169 | + 'Risky', |
| 170 | + array( |
| 171 | + $matched_content, |
| 172 | + $parameters[1]['raw'], |
| 173 | + $parameters[2]['raw'], |
| 174 | + ) |
| 175 | + ); |
| 176 | + } |
| 177 | +} |
0 commit comments