Skip to content
This repository has been archived by the owner on Jul 20, 2018. It is now read-only.

Commit

Permalink
Migrate DeprecatedParametersCheck to CodeCheckVisitor
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Dec 21, 2014
1 parent e4e302a commit 84579d1
Showing 1 changed file with 38 additions and 47 deletions.
85 changes: 38 additions & 47 deletions vip-scanner/checks/DeprecatedParametersCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,52 @@
* Checks for deprecated parameters.
*/

class DeprecatedParametersCheck extends BaseCheck {
class DeprecatedParametersCheck extends CodeCheckVisitor {

function check( $files ) {
$result = true;
$this->increment_check_count();
protected static $parameters = array(
'get_bloginfo' => array(
'home' => 'home_url()',
'url' => 'home_url()',
'wpurl' => 'site_url()',
'stylesheet_directory' => 'get_stylesheet_directory_uri()',
'template_directory' => 'get_template_directory_uri()',
'template_url' => 'get_template_directory_uri()',
'text_direction' => 'is_rtl()',
'feed_url' => "get_feed_link( 'feed' ), where feed is rss, rss2 or atom",
),
'bloginfo' => array(
'home' => 'echo esc_url( home_url() )',
'url' => 'echo esc_url( home_url() )',
'wpurl' => 'echo esc_url( site_url() )',
'stylesheet_directory' => 'echo esc_url( get_stylesheet_directory_uri() )',
'template_directory' => 'echo esc_url( get_template_directory_uri() )',
'template_url' => 'echo esc_url( get_template_directory_uri() )',
'text_direction' => 'is_rtl()',
'feed_url' => "echo esc_url( get_feed_link( 'feed' ) ), where feed is rss, rss2 or atom",
),
'get_option' => array(
'home' => 'home_url()',
'site_url' => 'site_url()',
)
);

$checks = array(
'get_bloginfo' => array(
'home' => 'home_url()',
'url' => 'home_url()',
'wpurl' => 'site_url()',
'stylesheet_directory' => 'get_stylesheet_directory_uri()',
'template_directory' => 'get_template_directory_uri()',
'template_url' => 'get_template_directory_uri()',
'text_direction' => 'is_rtl()',
'feed_url' => "get_feed_link( 'feed' ), where feed is rss, rss2 or atom",
),
'bloginfo' => array(
'home' => 'echo esc_url( home_url() )',
'url' => 'echo esc_url( home_url() )',
'wpurl' => 'echo esc_url( site_url() )',
'stylesheet_directory' => 'echo esc_url( get_stylesheet_directory_uri() )',
'template_directory' => 'echo esc_url( get_template_directory_uri() )',
'template_url' => 'echo esc_url( get_template_directory_uri() )',
'text_direction' => 'is_rtl()',
'feed_url' => "echo esc_url( get_feed_link( 'feed' ) ), where feed is rss, rss2 or atom",
),
'get_option' => array(
'home' => 'home_url()',
'site_url' => 'site_url()',
)
);

foreach ( $this->filter_files( $files, 'php' ) as $file_path => $file_content ) {

// Loop through all functions.
foreach ( $checks as $function => $data ) {

// Loop through the parameters and look for all function/parameter combinations.
foreach ( $data as $parameter => $replacement ) {

if ( preg_match( '/' . $function . '\(\s*("|\')' . $parameter . '("|\')\s*\)/', $file_content, $matches ) ) {
$error = trim( rtrim( $matches[0], '(' ) );
function check( PhpParser\Node $node ) {
if ( $node instanceof PhpParser\Node\Expr\FuncCall ) {
$name = $node->name->toString();
if ( array_key_exists( $name, self::$parameters ) ) {
$pars = self::$parameters[ $name ];
if ( ! empty( $node->args ) ) {
$value = $node->args[0]->value;
if ( $value instanceof PhpParser\Node\Scalar\String && array_key_exists( $value->value, $pars ) ) {
$message = 'The deprecated function parameter %1$s was found. Use %2$s instead.';
$this->add_error(
'deprecated',
'The deprecated function parameter <code>' . esc_html( $error ) . '</code> was found. Use <code>' . esc_html( $replacement ) . '</code> instead.',
BaseScanner::LEVEL_BLOCKER,
$this->get_filename( $file_path )
sprintf( $message, '<code>' . esc_html( $name . "( '" . $value->value . "' )" ) . '</code>' , '<code>' . esc_html( $pars[ $value->value ] ) . '</code>' ),
BaseScanner::LEVEL_BLOCKER
);
$result = false;
}
}
}
}

return $result;
}
}

0 comments on commit 84579d1

Please sign in to comment.