Skip to content

Commit

Permalink
Check for deprecated parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
grappler committed Feb 27, 2017
1 parent f2e97ba commit 002e7b4
Show file tree
Hide file tree
Showing 4 changed files with 482 additions and 0 deletions.
1 change: 1 addition & 0 deletions WordPress-Extra/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<exclude name="WordPress.WP.DiscouragedFunctions.create_function" />
</rule>
<rule ref="WordPress.WP.DeprecatedFunctions"/>
<rule ref="WordPress.WP.DeprecatedParameters"/>
<rule ref="WordPress.WP.AlternativeFunctions"/>
<rule ref="WordPress.WP.DiscouragedFunctions"/>

Expand Down
350 changes: 350 additions & 0 deletions WordPress/Sniffs/WP/DeprecatedParametersSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,350 @@
<?php
/**
* WordPress Coding Standard.
*
* @package WPCS\WordPressCodingStandards
* @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
* @license https://opensource.org/licenses/MIT MIT
*/

/**
* Check for usage of deprecated parameters in WP functions and sugests alternative based on the parameter passed.
*
* This sniff will throw an error when usage of deprecated parameters is
* detected if the parameter was deprecated before the minimum supported
* WP version; a warning otherwise.
* By default, it is set to presume that a project will support the current
* WP version and up to three releases before.
*
* @package WPCS\WordPressCodingStandards
*
* @since 0.11.0
*/
class WordPress_Sniffs_WP_DeprecatedParametersSniff extends WordPress_AbstractFunctionParameterSniff {

/**
* The group name for this group of functions.
*
* @since 0.11.0
*
* @var string
*/
protected $group_name = 'wp_deprecated_parameters';

/**
* Minimum WordPress version.
*
* This variable allows changing the minimum supported WP version used by
* this sniff by setting a property in a custom ruleset xml file.
*
* Example usage:
* <rule ref="WordPress.WP.DeprecatedParameters">
* <properties>
* <property name="minimum_supported_version" value="4.4"/>
* </properties>
* </rule>
*
* @since 0.11.0
*
* @var string WordPress version.
*/
public $minimum_supported_version = 4.4;

/**
* Array of function, argument, and default value for deprecated argument.
*
* The functions are ordered by the WordPress version the parameter was
* deprecated in. Was last updated in WordPress version 4.7.2.
*
* @since 0.11.0
*
* @var array Multidimensional array with parameter details.
*
* @type string Function name. {
* @type int target Parameter positions. {
* @type string|boolean|null Default value.
* @type int The WordPress version when deprecated.
* }
* }
*/
protected $target_functions = array(

'get_last_updated' => array(
1 => array(
'value' => '',
'version' => '3.0', // Was previously part of MU.
),
),
'convert_chars' => array(
2 => array(
'value' => '',
'version' => '0.71',
),
),
'comments_link' => array(
1 => array(
'value' => '',
'version' => '0.72',
),
2 => array(
'value' => '',
'version' => '1.3.0',
),
),
'comments_number' => array(
4 => array(
'value' => '',
'version' => '1.3',
),
),
'the_author' => array(
1 => array(
'value' => '',
'version' => '2.1',
),
2 => array(
'value' => true,
'version' => '1.5',
),
),
'wp_upload_bits' => array(
2 => array(
'value' => '',
'version' => '2.0',
),
),
'get_the_author' => array(
1 => array(
'value' => '',
'version' => '2.1',
),
),
'the_author_posts_link' => array(
1 => array(
'value' => '',
'version' => '2.1',
),
),
'add_option' => array(
3 => array(
'value' => '',
'version' => '2.3',
),
),
'get_user_option' => array(
3 => array(
'value' => '',
'version' => '2.3',
),
),
'the_attachment_link' => array(
3 => array(
'value' => false,
'version' => '2.5',
),
),
'trackback_rdf' => array(
1 => array(
'value' => '',
'version' => '2.5',
),
),
'trackback_url' => array(
1 => array(
'value' => true,
'version' => '2.5',
),
),
'xfn_check' => array(
3 => array(
'value' => '',
'version' => '2.5',
),
),
'wp_install' => array(
5 => array(
'value' => '',
'version' => '2.6',
),
),
'discover_pingback_server_uri' => array(
2 => array(
'value' => '',
'version' => '2.7',
),
),
'load_plugin_textdomain' => array(
2 => array(
'value' => false,
'version' => '2.7',
),
),
'wp_get_http_headers' => array(
2 => array(
'value' => false,
'version' => '2.7',
),
),
'safecss_filter_attr' => array(
2 => array(
'value' => '',
'version' => '2.8.1',
),
),
'wp_get_sidebars_widgets' => array(
1 => array(
'value' => true,
'version' => '2.8.1',
),
),
'get_delete_post_link' => array(
2 => array(
'value' => '',
'version' => '3.0',
),
),
'is_email' => array(
2 => array(
'value' => false,
'version' => '3.0',
),
),
'update_user_status' => array(
4 => array(
'value' => null,
'version' => '3.0.2',
),
),
'update_blog_option' => array(
4 => array(
'value' => null,
'version' => '3.1',
),
),
'update_blog_status' => array(
4 => array(
'value' => null,
'version' => '3.1',
),
),
'wp_notify_postauthor' => array(
2 => array(
'value' => null,
'version' => '3.8',
),
),
'wp_new_user_notification' => array(
2 => array(
'value' => null,
'version' => '4.3.1',
),
),
'get_wp_title_rss' => array(
1 => array(
'value' => '&#8211;',
'version' => '4.4',
),
),
'wp_title_rss' => array(
1 => array(
'value' => '&#8211;',
'version' => '4.4',
),
),
'unregister_setting' => array(
4 => array(
'value' => '',
'version' => '4.7',
),
),

); // End $target_functions.

/**
* Process the parameters of a matched function.
*
* @since 0.11.0
*
* @param int $stackPtr The position of the current token in the stack.
* @param array $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 ) {
$paramCount = count( $parameters );
foreach ( $this->target_functions[ $matched_content ] as $position => $parameter_args ) {

if ( $position > $paramCount ) {
break;
}

if ( ! isset( $parameters[ $position ] ) ) {
continue;
}

$is_dynamic_parameter = $this->phpcsFile->findNext(
array( T_CONSTANT_ENCAPSED_STRING, T_ARRAY, T_FALSE, T_TRUE, T_NULL, T_LNUMBER, T_WHITESPACE ),
$parameters[ $position ]['start'],
( $parameters[ $position ]['end'] + 1 ),
true,
null,
true
);

if ( $is_dynamic_parameter ) {
$this->phpcsFile->addWarning(
'The dynamic parameter [%s] at position #%s of %s() could possibly be deprecated.',
$stackPtr,
'PossibleDeprecatedParameter',
array(
$parameters[ $position ]['raw'],
$position,
$matched_content,
)
);
return;
}

switch ( $parameters[ $position ]['raw'] ) {
case 'true':
$matched_parameter = true;
break;
case 'false':
$matched_parameter = false;
break;
case 'null':
$matched_parameter = null;
break;
default:
$matched_parameter = $this->strip_quotes( $parameters[ $position ]['raw'] );
break;
}

if ( $parameter_args['value'] === $matched_parameter ) {
continue;
}

$message = 'The parameter [%s] at position #%s of %s() has been deprecated since WordPress version %s.';
$data = array(
$parameters[ $position ]['raw'],
$position,
$matched_content,
$parameter_args['version'],
);

if ( isset( $parameter_args['value'] ) && $position < $paramCount ) {
$message .= ' Use "%s" instead.';
$data[] = (string) $parameter_args['value'];
} else {
$message .= ' Instead do not pass the parameter.';
}

$is_error = version_compare( $parameter_args['version'], $this->minimum_supported_version, '<' );

$this->addMessage( $message, $stackPtr, $is_error, $matched_content . 'Found', $data, 0 );
} // End foreach().
} // End process_parameters().

}
Loading

0 comments on commit 002e7b4

Please sign in to comment.