Skip to content

Commit 85d7477

Browse files
committed
Switch to new helper functions
1 parent d1770a1 commit 85d7477

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

HM/Sniffs/Performance/SlowMetaQuerySniff.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use PHP_CodeSniffer\Files\File;
66
use PHP_CodeSniffer\Util\Tokens;
7+
use PHPCSUtils\Utils\Arrays;
8+
use PHPCSUtils\Utils\MessageHelper;
9+
use PHPCSUtils\Utils\TextStrings;
710
use WordPressCS\WordPress\AbstractArrayAssignmentRestrictionsSniff;
811

912
/**
@@ -104,7 +107,8 @@ protected function check_meta_query_item( int $array_open ) {
104107
$array_open_token = $this->tokens[ $array_open ];
105108
if ( $array_open_token['code'] !== T_ARRAY && $array_open_token['code'] !== T_OPEN_SHORT_ARRAY ) {
106109
// Dynamic value, we can't check.
107-
$this->addMessage(
110+
MessageHelper::addMessage(
111+
$this->phpcsFile,
108112
'meta_query is dynamic, cannot be checked.',
109113
$array_open,
110114
'warning',
@@ -114,7 +118,7 @@ protected function check_meta_query_item( int $array_open ) {
114118
return;
115119
}
116120

117-
$array_bounds = $this->find_array_open_close( $array_open );
121+
$array_bounds = Arrays::getOpenClose( $this->phpcsFile, $array_open );
118122
$elements = $this->get_array_indices( $array_bounds['opener'], $array_bounds['closer'] );
119123

120124
// Is this a "first-order" query?
@@ -138,7 +142,7 @@ protected function check_meta_query_item( int $array_open ) {
138142

139143
foreach ( $elements as $element ) {
140144
if ( isset( $element['index_start'] ) ) {
141-
$index = $this->strip_quotes( $this->tokens[ $element['index_start'] ]['content'] );
145+
$index = TextStrings::stripQuotes( $this->tokens[ $element['index_start'] ]['content'] );
142146
if ( strtolower( $index ) === 'relation' ) {
143147
// Skip 'relation' element.
144148
continue;
@@ -176,7 +180,7 @@ protected function get_static_value_for_element( array $element ) : ?string {
176180
return static::DYNAMIC_VALUE;
177181
}
178182

179-
return $this->strip_quotes( $this->tokens[ $value_start ]['content'] );
183+
return TextStrings::stripQuotes( $this->tokens[ $value_start ]['content'] );
180184
}
181185

182186
/**
@@ -208,7 +212,7 @@ protected function find_key_in_array( array $elements, string $array_key ) : ?ar
208212
continue;
209213
}
210214

211-
$index = $this->strip_quotes( $this->tokens[ $start ]['content'] );
215+
$index = TextStrings::stripQuotes( $this->tokens[ $start ]['content'] );
212216
if ( $index !== $array_key ) {
213217
// Not the item we want, skip.
214218
continue;
@@ -270,15 +274,17 @@ protected function check_compare_value( string $compare, int $stackPtr = null )
270274
}
271275

272276
if ( $compare === static::DYNAMIC_VALUE ) {
273-
$this->addMessage(
277+
MessageHelper::addMessage(
278+
$this->phpcsFile,
274279
'meta_query is using a dynamic comparison; this cannot be checked automatically, and may be non-performant.',
275280
$stackPtr,
276281
'warning',
277282
'dynamic_compare'
278283
);
279284
} elseif ( $compare !== 'EXISTS' && $compare !== 'NOT EXISTS' ) {
280285
// Add a message ourselves.
281-
$this->addMessage(
286+
MessageHelper::addMessage(
287+
$this->phpcsFile,
282288
'meta_query is using %s comparison, which is non-performant.',
283289
$stackPtr,
284290
'warning',

HM/Sniffs/Performance/SlowOrderBySniff.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace HM\Sniffs\Performance;
44

5+
use PHPCSUtils\Utils\MessageHelper;
56
use WordPressCS\WordPress\AbstractArrayAssignmentRestrictionsSniff;
67

78
/**
@@ -61,7 +62,8 @@ public function callback( $key, $val, $line, $group ) {
6162
case 'rand':
6263
case 'meta_value':
6364
case 'meta_value_num':
64-
$this->addMessage(
65+
MessageHelper::addMessage(
66+
$this->phpcsFile,
6567
'Ordering query results by %s is not performant.',
6668
$this->stackPtr,
6769
'warning',

HM/Sniffs/Security/ValidatedSanitizedInputSniff.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use HM\Sniffs\ExtraSniffCode;
66
use PHP_CodeSniffer\Files\File as PhpcsFile;
7+
use PHPCSUtils\Utils\TextStrings;
8+
use WordPressCS\WordPress\Helpers\VariableHelper;
79
use WordPressCS\WordPress\Sniffs\Security\ValidatedSanitizedInputSniff as WPCSValidatedSanitizedInputSniff;
810

911
class ValidatedSanitizedInputSniff extends WPCSValidatedSanitizedInputSniff {
@@ -71,7 +73,7 @@ public function process_token( $stackPtr ) {
7173
* @return bool True if this is a $_SERVER variable and is safe, false to run regular checks.
7274
*/
7375
protected function check_server_variable( $stackPtr ) {
74-
$key = $this->get_array_access_key( $stackPtr );
76+
$key = VariableHelper::get_array_access_key( $this->phpcsFile, $stackPtr );
7577

7678
// Find the next non-whitespace token.
7779
$open_bracket = $this->phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), null, true );
@@ -94,7 +96,7 @@ protected function check_server_variable( $stackPtr ) {
9496
}
9597

9698
// Constant string, check if it's allowed.
97-
$key = $this->strip_quotes( $this->tokens[ $index_token ]['content'] );
99+
$key = TextStrings::stripQuotes( $this->tokens[ $index_token ]['content'] );
98100
if ( ! in_array( $key, $this->allowedServerKeys, true ) ) {
99101
// Unsafe key, requires sanitising.
100102
return false;

0 commit comments

Comments
 (0)