Skip to content

Commit

Permalink
Sniffs: switch over to using utilities from PHPCSUtils [2]
Browse files Browse the repository at this point in the history
Switch over replacing function calls to WPCS utility functions with calls to the improved versions of the same in PHPCSUtils.
These WPCS native utility functions no longer exist in WPCS 3.0.0 as WPCS is now also using PHPCSUtils.
  • Loading branch information
jrfnl committed Nov 3, 2023
1 parent f6fbe7b commit d1d6dc6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
3 changes: 2 additions & 1 deletion Yoast/Sniffs/NamingConventions/ObjectNameDepthSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace YoastCS\Yoast\Sniffs\NamingConventions;

use PHPCSUtils\Utils\Namespaces;
use PHPCSUtils\Utils\ObjectDeclarations;
use WordPressCS\WordPress\Sniff as WPCS_Sniff;

Expand Down Expand Up @@ -74,7 +75,7 @@ public function register() {
public function process_token( $stackPtr ) {

// Check whether we are in a namespace or not.
if ( $this->determine_namespace( $stackPtr ) === '' ) {
if ( Namespaces::determineNamespace( $this->phpcsFile, $stackPtr ) === '' ) {
return;
}

Expand Down
5 changes: 3 additions & 2 deletions Yoast/Sniffs/NamingConventions/ValidHookNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace YoastCS\Yoast\Sniffs\NamingConventions;

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\TextStrings;
use WordPressCS\WordPress\Sniffs\NamingConventions\ValidHookNameSniff as WPCS_ValidHookNameSniff;
use YoastCS\Yoast\Utils\CustomPrefixesTrait;

Expand Down Expand Up @@ -118,7 +119,7 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p

if ( isset( Tokens::$stringTokens[ $this->tokens[ $first_non_empty ]['code'] ] ) ) {
$this->prefix_quote_style = $this->tokens[ $first_non_empty ]['content'][0];
$content = \trim( $this->strip_quotes( $this->tokens[ $first_non_empty ]['content'] ) );
$content = \trim( TextStrings::stripQuotes( $this->tokens[ $first_non_empty ]['content'] ) );

foreach ( $this->validated_prefixes as $prefix ) {
if ( \strpos( $prefix, '\\' ) === false
Expand Down Expand Up @@ -296,7 +297,7 @@ public function verify_yoast_hook_name( $stackPtr, $parameters ) {
* Check the hook name depth.
*/
$hook_ptr = $first_non_empty; // If no other tokens were found, the first non empty will be the hook name.
$hook_name = $this->strip_quotes( $this->tokens[ $hook_ptr ]['content'] );
$hook_name = TextStrings::stripQuotes( $this->tokens[ $hook_ptr ]['content'] );
$hook_name = \substr( $hook_name, \strlen( $this->found_prefix ) );

$parts = \explode( '_', $hook_name );
Expand Down
12 changes: 7 additions & 5 deletions Yoast/Sniffs/Tools/BrainMonkeyRaceConditionSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\FunctionDeclarations;
use PHPCSUtils\Utils\PassedParameters;
use PHPCSUtils\Utils\TextStrings;
use WordPressCS\WordPress\Sniff;

/**
Expand Down Expand Up @@ -83,21 +85,21 @@ public function process_token( $stackPtr ) {
}

// Check that this is an expect() for one of the hook functions.
$params = $this->get_function_call_parameters( $stackPtr );
if ( empty( $params ) ) {
$param = PassedParameters::getParameter( $this->phpcsFile, $stackPtr, 1, 'function_name' );
if ( empty( $param ) ) {
return;
}

$expected = Tokens::$emptyTokens;
$expected[] = \T_CONSTANT_ENCAPSED_STRING;

$hasUnexpected = $this->phpcsFile->findNext( $expected, $params[1]['start'], ( $params[1]['end'] + 1 ), true );
$hasUnexpected = $this->phpcsFile->findNext( $expected, $param['start'], ( $param['end'] + 1 ), true );
if ( $hasUnexpected !== false ) {
return;
}

$text = $this->phpcsFile->findNext( Tokens::$emptyTokens, $params[1]['start'], ( $params[1]['end'] + 1 ), true );
$textContent = $this->strip_quotes( $this->tokens[ $text ]['content'] );
$text = $this->phpcsFile->findNext( Tokens::$emptyTokens, $param['start'], ( $param['end'] + 1 ), true );
$textContent = TextStrings::stripQuotes( $this->tokens[ $text ]['content'] );
if ( $textContent !== 'apply_filters' && $textContent !== 'do_action' ) {
return;
}
Expand Down
13 changes: 8 additions & 5 deletions Yoast/Sniffs/Yoast/AlternativeFunctionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace YoastCS\Yoast\Sniffs\Yoast;

use PHPCSUtils\Utils\MessageHelper;
use PHPCSUtils\Utils\Namespaces;
use PHPCSUtils\Utils\PassedParameters;
use WordPressCS\WordPress\AbstractFunctionRestrictionsSniff;

/**
Expand Down Expand Up @@ -49,7 +52,7 @@ public function process_matched_token( $stackPtr, $group_name, $matched_content
$fixable = true;
$message = $this->groups[ $group_name ]['message'];
$is_error = ( $this->groups[ $group_name ]['type'] === 'error' );
$error_code = $this->string_to_errorcode( $group_name . '_' . $matched_content );
$error_code = MessageHelper::stringToErrorcode( $group_name . '_' . $matched_content );
$data = [
$matched_content,
$replacement,
Expand All @@ -65,7 +68,7 @@ public function process_matched_token( $stackPtr, $group_name, $matched_content
* The function `WPSEO_Utils:format_json_encode()` is only a valid alternative
* when only the first parameter is passed.
*/
if ( $this->get_function_call_parameter_count( $stackPtr ) !== 1 ) {
if ( PassedParameters::getParameterCount( $this->phpcsFile, $stackPtr ) !== 1 ) {
$fixable = false;
$error_code .= 'WithAdditionalParams';
}
Expand All @@ -74,13 +77,13 @@ public function process_matched_token( $stackPtr, $group_name, $matched_content
}

if ( $fixable === false ) {
$this->addMessage( $message, $stackPtr, $is_error, $error_code, $data );
MessageHelper::addMessage( $this->phpcsFile, $message, $stackPtr, $is_error, $error_code, $data );
return;
}

$fix = $this->addFixableMessage( $message, $stackPtr, $is_error, $error_code, $data );
$fix = MessageHelper::addFixableMessage( $this->phpcsFile, $message, $stackPtr, $is_error, $error_code, $data );
if ( $fix === true ) {
$namespaced = $this->determine_namespace( $stackPtr );
$namespaced = Namespaces::determineNamespace( $this->phpcsFile, $stackPtr );

if ( empty( $namespaced ) || empty( $replacement ) ) {
$this->phpcsFile->fixer->replaceToken( $stackPtr, $replacement );
Expand Down

0 comments on commit d1d6dc6

Please sign in to comment.