Skip to content

Commit 93dac61

Browse files
committed
4.0 | Remove all CS/JS-specific code from sniffs
PHP_CodeSniffer 4.0 no longer support scanning of JS/CSS code. These sniffs all sniff both PHP files, as well as CSS and/or JS files. This commit removes any JS/CSS specific tests and removes the JS/CSS specific code from the sniffs. Follow up on 839 Closes 442
1 parent 8cd5160 commit 93dac61

10 files changed

Lines changed: 55 additions & 485 deletions

File tree

WordPressVIPMinimum/Sniffs/Security/MustacheSniff.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,17 @@
1212
use WordPressVIPMinimum\Sniffs\Sniff;
1313

1414
/**
15-
* Looks for instances of unescaped output for Mustache templating engine and Handlebars.js.
15+
* Looks for instances of unescaped output for Mustache templating engine and Handlebars.js within PHP code.
1616
*/
1717
class MustacheSniff extends Sniff {
1818

19-
/**
20-
* A list of tokenizers this sniff supports.
21-
*
22-
* @var string[]
23-
*/
24-
public $supportedTokenizers = [ 'JS', 'PHP' ];
25-
2619
/**
2720
* Returns an array of tokens this test wants to listen for.
2821
*
2922
* @return array<int|string>
3023
*/
3124
public function register() {
32-
$targets = Tokens::$textStringTokens;
33-
$targets[ T_STRING ] = T_STRING;
34-
35-
return $targets;
25+
return Tokens::$textStringTokens;
3626
}
3727

3828
/**

WordPressVIPMinimum/Sniffs/Security/TwigSniff.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,10 @@
1313
use WordPressVIPMinimum\Sniffs\Sniff;
1414

1515
/**
16-
* Looks for instances of unescaped output for Twig templating engine.
16+
* Looks for instances of unescaped output for Twig templating engine within PHP code.
1717
*/
1818
class TwigSniff extends Sniff {
1919

20-
/**
21-
* A list of tokenizers this sniff supports.
22-
*
23-
* @var string[]
24-
*/
25-
public $supportedTokenizers = [ 'JS', 'PHP' ];
26-
2720
/**
2821
* Returns an array of tokens this test wants to listen for.
2922
*

WordPressVIPMinimum/Sniffs/Security/UnderscorejsSniff.php

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use WordPressVIPMinimum\Sniffs\Sniff;
1414

1515
/**
16-
* Looks for instances of unescaped output for Underscore.js templating engine.
16+
* Looks for instances of unescaped output for Underscore.js templating engine within PHP code.
1717
*/
1818
class UnderscorejsSniff extends Sniff {
1919

@@ -40,24 +40,13 @@ class UnderscorejsSniff extends Sniff {
4040
*/
4141
const INTERPOLATE_KEYWORD_REGEX = '`(?:templateSettings\.interpolate|\.interpolate\s*=\s*/|interpolate\s*:\s*/)`';
4242

43-
/**
44-
* A list of tokenizers this sniff supports.
45-
*
46-
* @var string[]
47-
*/
48-
public $supportedTokenizers = [ 'JS', 'PHP' ];
49-
5043
/**
5144
* Returns an array of tokens this test wants to listen for.
5245
*
5346
* @return array<int|string>
5447
*/
5548
public function register() {
56-
$targets = Tokens::$textStringTokens;
57-
$targets[] = T_PROPERTY;
58-
$targets[] = T_STRING;
59-
60-
return $targets;
49+
return Tokens::$textStringTokens;
6150
}
6251

6352
/**
@@ -68,57 +57,6 @@ public function register() {
6857
* @return void
6958
*/
7059
public function process_token( $stackPtr ) {
71-
/*
72-
* Ignore Gruntfile.js files as they are configuration, not code.
73-
*/
74-
$file_name = TextStrings::stripQuotes( $this->phpcsFile->getFileName() );
75-
$file_name = strtolower( basename( $file_name ) );
76-
77-
if ( $file_name === 'gruntfile.js' ) {
78-
return;
79-
}
80-
81-
/*
82-
* Check for delimiter change in JS files.
83-
*/
84-
if ( $this->tokens[ $stackPtr ]['code'] === T_STRING
85-
|| $this->tokens[ $stackPtr ]['code'] === T_PROPERTY
86-
) {
87-
if ( $this->phpcsFile->tokenizerType !== 'JS' ) {
88-
// These tokens are only relevant for JS files.
89-
return;
90-
}
91-
92-
if ( $this->tokens[ $stackPtr ]['content'] !== 'interpolate' ) {
93-
return;
94-
}
95-
96-
// Check the context to prevent false positives.
97-
if ( $this->tokens[ $stackPtr ]['code'] === T_STRING ) {
98-
$prev = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true );
99-
if ( $prev === false || $this->tokens[ $prev ]['code'] !== T_OBJECT_OPERATOR ) {
100-
return;
101-
}
102-
103-
$prevPrev = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true );
104-
$next = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true );
105-
if ( ( $prevPrev === false
106-
|| $this->tokens[ $prevPrev ]['code'] !== T_STRING
107-
|| $this->tokens[ $prevPrev ]['content'] !== 'templateSettings' )
108-
&& ( $next === false
109-
|| $this->tokens[ $next ]['code'] !== T_EQUAL )
110-
) {
111-
return;
112-
}
113-
}
114-
115-
// Underscore.js delimiter change.
116-
$message = 'Found Underscore.js delimiter change notation.';
117-
$this->phpcsFile->addWarning( $message, $stackPtr, 'InterpolateFound' );
118-
119-
return;
120-
}
121-
12260
$content = TextStrings::stripQuotes( $this->tokens[ $stackPtr ]['content'] );
12361

12462
$match_count = preg_match_all( self::UNESCAPED_INTERPOLATE_REGEX, $content, $matches );
@@ -149,9 +87,7 @@ public function process_token( $stackPtr ) {
14987
}
15088
}
15189

152-
if ( $this->phpcsFile->tokenizerType !== 'JS'
153-
&& preg_match( self::INTERPOLATE_KEYWORD_REGEX, $content ) > 0
154-
) {
90+
if ( preg_match( self::INTERPOLATE_KEYWORD_REGEX, $content ) > 0 ) {
15591
// Underscore.js delimiter change.
15692
$message = 'Found Underscore.js delimiter change notation.';
15793
$this->phpcsFile->addWarning( $message, $stackPtr, 'InterpolateFound' );

WordPressVIPMinimum/Sniffs/Security/VuejsSniff.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,10 @@
1111
use WordPressVIPMinimum\Sniffs\Sniff;
1212

1313
/**
14-
* Looks for instances of unescaped output for Twig templating engine.
14+
* Looks for instances of unescaped output for Twig templating engine within PHP code.
1515
*/
1616
class VuejsSniff extends Sniff {
1717

18-
/**
19-
* A list of tokenizers this sniff supports.
20-
*
21-
* @var string[]
22-
*/
23-
public $supportedTokenizers = [ 'JS', 'PHP' ];
24-
2518
/**
2619
* Returns an array of tokens this test wants to listen for.
2720
*

WordPressVIPMinimum/Sniffs/UserExperience/AdminBarRemovalSniff.php

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,6 @@
2323
*/
2424
class AdminBarRemovalSniff extends AbstractFunctionParameterSniff {
2525

26-
/**
27-
* A list of tokenizers this sniff supports.
28-
*
29-
* @var array<string>
30-
*/
31-
public $supportedTokenizers = [
32-
'PHP',
33-
'CSS',
34-
];
35-
3626
/**
3727
* Whether or not the sniff only checks for removal of the admin bar
3828
* or any manipulation to the visibility of the admin bar.
@@ -126,9 +116,6 @@ public function register() {
126116

127117
$targets = $this->string_tokens;
128118

129-
// Add CSS style target.
130-
$targets[] = \T_STYLE;
131-
132119
// Set the target selectors regex only once.
133120
$selectors = array_map(
134121
'preg_quote',
@@ -157,15 +144,9 @@ public function register() {
157144
*/
158145
public function process_token( $stackPtr ) {
159146

160-
$file_name = $this->phpcsFile->getFilename();
161-
$file_extension = substr( strrchr( $file_name, '.' ), 1 );
147+
$file_name = $this->phpcsFile->getFilename();
162148

163-
if ( $file_extension === 'css' ) {
164-
if ( $this->tokens[ $stackPtr ]['code'] === \T_STYLE ) {
165-
$this->process_css_style( $stackPtr );
166-
return;
167-
}
168-
} elseif ( isset( $this->string_tokens[ $this->tokens[ $stackPtr ]['code'] ] ) ) {
149+
if ( isset( $this->string_tokens[ $this->tokens[ $stackPtr ]['code'] ] ) ) {
169150
/*
170151
* Set $in_style && $in_target_selector to false if it is the first time
171152
* this sniff is run on a file.
@@ -317,57 +298,6 @@ public function process_text_for_style( $stackPtr, $file_name ) {
317298
}
318299
}
319300

320-
/**
321-
* Processes this test for T_STYLE tokens in CSS files.
322-
*
323-
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
324-
*
325-
* @return void
326-
*/
327-
protected function process_css_style( $stackPtr ) {
328-
if ( ! isset( $this->target_css_properties[ $this->tokens[ $stackPtr ]['content'] ] ) ) {
329-
// Not one of the CSS properties we're interested in.
330-
return;
331-
}
332-
333-
$css_property = $this->target_css_properties[ $this->tokens[ $stackPtr ]['content'] ];
334-
335-
// Check if the CSS selector matches.
336-
$opener = $this->phpcsFile->findPrevious( \T_OPEN_CURLY_BRACKET, $stackPtr );
337-
if ( $opener !== false ) {
338-
for ( $i = ( $opener - 1 ); $i >= 0; $i-- ) {
339-
if ( isset( Tokens::$commentTokens[ $this->tokens[ $i ]['code'] ] )
340-
|| $this->tokens[ $i ]['code'] === \T_CLOSE_CURLY_BRACKET
341-
) {
342-
break;
343-
}
344-
}
345-
$start = ( $i + 1 );
346-
$selector = trim( GetTokensAsString::normal( $this->phpcsFile, $start, ( $opener - 1 ) ) );
347-
unset( $i );
348-
349-
foreach ( $this->target_css_selectors as $target_selector ) {
350-
if ( strpos( $selector, $target_selector ) !== false ) {
351-
$error = true;
352-
353-
if ( $this->remove_only === true ) {
354-
// Check the value of the CSS property.
355-
$valuePtr = $this->phpcsFile->findNext( [ \T_COLON, \T_WHITESPACE ], $stackPtr + 1, null, true );
356-
$value = $this->tokens[ $valuePtr ]['content'];
357-
$valid = $this->validate_css_property_value( $value, $css_property['type'], $css_property['value'] );
358-
if ( $valid === true ) {
359-
$error = false;
360-
}
361-
}
362-
363-
if ( $error === true ) {
364-
$this->addHidingDetectedError( $stackPtr );
365-
}
366-
}
367-
}
368-
}
369-
}
370-
371301
/**
372302
* Consolidated violation.
373303
*

WordPressVIPMinimum/Tests/Security/Gruntfile.js

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)