Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WPCS 3.0: Fix incompatibilities by using PHPCSUtils #734

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace WordPressVIPMinimum\Sniffs;

use WordPressVIPMinimum\Sniffs\Sniff;
GaryJones marked this conversation as resolved.
Show resolved Hide resolved
use PHPCSUtils\Utils\MessageHelper;
GaryJones marked this conversation as resolved.
Show resolved Hide resolved

/**
* Restricts usage of some variables.
Expand Down Expand Up @@ -200,11 +200,13 @@ public function process_token( $stackPtr ) {
continue;
}

$this->addMessage(
$code = MessageHelper::stringToErrorcode( $groupName . '_' . $match[1] );
MessageHelper::addMessage(
$this->phpcsFile,
$group['message'],
$stackPtr,
$group['type'] === 'error',
$this->string_to_errorcode( $groupName . '_' . $match[1] ),
$code,
[ $var ]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

namespace WordPressVIPMinimum\Sniffs\Constants;

use WordPressVIPMinimum\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\PassedParameters;
use WordPressVIPMinimum\Sniffs\Sniff;

/**
* Sniff for properly using constant name when checking whether a constant is defined.
Expand Down Expand Up @@ -55,7 +56,7 @@ public function process_token( $stackPtr ) {
return;
}

$param = $this->get_function_call_parameter( $stackPtr, 1 );
$param = PassedParameters::getParameter( $this->phpcsFile, $stackPtr, 1, 'constant_name' );
if ( $param === false ) {
// Target parameter not found.
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace WordPressVIPMinimum\Sniffs\Functions;

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\TextStrings;
use WordPressVIPMinimum\Sniffs\Sniff;

/**
Expand Down Expand Up @@ -139,7 +140,7 @@ private function collect_variables() {
* If we reached the end of the loop and the $value_ptr was set, we know for sure
* this was a plain text string variable assignment.
*/
$current_var_value = $this->strip_quotes( $this->tokens[ $value_ptr ]['content'] );
$current_var_value = TextStrings::stripQuotes( $this->tokens[ $value_ptr ]['content'] );

if ( isset( $this->disallowed_functions[ $current_var_value ] ) === false ) {
// Text string is not one of the ones we're looking for.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

namespace WordPressVIPMinimum\Sniffs\Hooks;

use WordPressVIPMinimum\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\Arrays;
use WordPressVIPMinimum\Sniffs\Sniff;

/**
* This sniff validates that filters always return a value
Expand Down Expand Up @@ -94,7 +95,7 @@ public function process_token( $stackPtr ) {
*/
private function processArray( $stackPtr ) {

$open_close = $this->find_array_open_close( $stackPtr );
$open_close = Arrays::getOpenClose( $this->phpcsFile, $stackPtr );
if ( $open_close === false ) {
return;
}
Expand Down
5 changes: 3 additions & 2 deletions WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

namespace WordPressVIPMinimum\Sniffs\Hooks;

use WordPressVIPMinimum\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\Arrays;
use WordPressVIPMinimum\Sniffs\Sniff;

/**
* This sniff validates a proper usage of pre_get_posts action callback.
Expand Down Expand Up @@ -95,7 +96,7 @@ public function process_token( $stackPtr ) {
*/
private function processArray( $stackPtr ) {

$open_close = $this->find_array_open_close( $stackPtr );
$open_close = Arrays::getOpenClose( $this->phpcsFile, $stackPtr );
if ( $open_close === false ) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace WordPressVIPMinimum\Sniffs\Performance;

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\TextStrings;
use WordPressCS\WordPress\AbstractFunctionParameterSniff;

/**
Expand Down Expand Up @@ -147,7 +148,7 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
}

if ( $this->tokens[ $i ]['code'] === T_CONSTANT_ENCAPSED_STRING ) {
$content = $this->strip_quotes( $this->tokens[ $i ]['content'] );
$content = TextStrings::stripQuotes( $this->tokens[ $i ]['content'] );
if ( is_numeric( $content ) === true ) {
$tokensAsString .= $content;
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

namespace WordPressVIPMinimum\Sniffs\Security;

use WordPressVIPMinimum\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\TextStrings;
use WordPressVIPMinimum\Sniffs\Sniff;

/**
* Checks whether proper escaping function is used.
Expand Down Expand Up @@ -195,7 +196,7 @@ public function process_token( $stackPtr ) {

$content = $this->tokens[ $html ]['content'];
if ( isset( Tokens::$stringTokens[ $this->tokens[ $html ]['code'] ] ) === true ) {
$content = Sniff::strip_quotes( $content );
$content = TextStrings::stripQuotes( $content );
}

$escaping_type = $this->escaping_functions[ $function_name ];
Expand Down
5 changes: 3 additions & 2 deletions WordPressVIPMinimum/Sniffs/Security/UnderscorejsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace WordPressVIPMinimum\Sniffs\Security;

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\TextStrings;
use WordPressVIPMinimum\Sniffs\Sniff;

/**
Expand Down Expand Up @@ -72,7 +73,7 @@ public function process_token( $stackPtr ) {
/*
* Ignore Gruntfile.js files as they are configuration, not code.
*/
$file_name = $this->strip_quotes( $this->phpcsFile->getFileName() );
$file_name = TextStrings::stripQuotes( $this->phpcsFile->getFileName() );
$file_name = strtolower( basename( $file_name ) );

if ( $file_name === 'gruntfile.js' ) {
Expand Down Expand Up @@ -120,7 +121,7 @@ public function process_token( $stackPtr ) {
return;
}

$content = $this->strip_quotes( $this->tokens[ $stackPtr ]['content'] );
$content = TextStrings::stripQuotes( $this->tokens[ $stackPtr ]['content'] );

$match_count = preg_match_all( self::UNESCAPED_INTERPOLATE_REGEX, $content, $matches );
if ( $match_count > 0 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace WordPressVIPMinimum\Sniffs\UserExperience;

use PHPCSUtils\Utils\TextStrings;
use WordPressCS\WordPress\AbstractFunctionParameterSniff;
use PHP_CodeSniffer\Util\Tokens;

Expand Down Expand Up @@ -207,13 +208,13 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
break;

case 'add_filter':
$filter_name = $this->strip_quotes( $parameters[1]['raw'] );
$filter_name = TextStrings::stripQuotes( $parameters[1]['raw'] );
if ( $filter_name !== 'show_admin_bar' ) {
break;
}

$error = true;
if ( $this->remove_only === true && isset( $parameters[2]['raw'] ) && $this->strip_quotes( $parameters[2]['raw'] ) === '__return_true' ) {
if ( $this->remove_only === true && isset( $parameters[2]['raw'] ) && TextStrings::stripQuotes( $parameters[2]['raw'] ) === '__return_true' ) {
$error = false;
}
break;
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"require": {
"php": ">=5.4",
"dealerdirect/phpcodesniffer-composer-installer": "^0.4.1 || ^0.5 || ^0.6.2 || ^0.7 || ^1.0",
"phpcsstandards/phpcsutils": "^1.0",
"sirbrillig/phpcs-variable-analysis": "^2.11.1",
"squizlabs/php_codesniffer": "^3.7.1",
"wp-coding-standards/wpcs": "^2.3"
Expand Down