Skip to content

Commit 37b610d

Browse files
committed
Addnig sniff for flagging loading of Zoninator of version >= 0.8
Zoninator of v >= 0.8 requires WordPress core REST API, which has to be loaded by `wpcom_vip_load_wp_rest_api()` before the plugin itself. This should remind the reviewers to make sure the core's REST API is being properly loaded.
1 parent 7c42465 commit 37b610d

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* WordPressVIPMinimum Coding Standard.
4+
*
5+
* @package VIPCS\WordPressVIPMinimum
6+
*/
7+
8+
namespace WordPressVIPMinimum\Sniffs\Plugins;
9+
10+
use PHP_CodeSniffer_File as File;
11+
use PHP_CodeSniffer_Tokens as Tokens;
12+
13+
/**
14+
* This sniff reminds the developers to check whether the WordPress Core REST API is enabled
15+
* along with loading v0.8 and above.
16+
*/
17+
class ZoninatorSniff implements \PHP_CodeSniffer_Sniff {
18+
19+
/**
20+
* Returns the token types that this sniff is interested in.
21+
*
22+
* @return array(int)
23+
*/
24+
public function register() {
25+
return Tokens::$functionNameTokens;
26+
27+
}//end register()
28+
29+
30+
/**
31+
* Processes the tokens that this sniff is interested in.
32+
*
33+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
34+
* @param int $stackPtr The position in the stack where
35+
* the token was found.
36+
*
37+
* @return void
38+
*/
39+
public function process( File $phpcsFile, $stackPtr ) {
40+
41+
$tokens = $phpcsFile->getTokens();
42+
$phpcsFile = $phpcsFile;
43+
44+
if ( 'wpcom_vip_load_plugin' !== $tokens[ $stackPtr ]['content'] ) {
45+
return;
46+
}
47+
48+
$openBracket = $phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true );
49+
50+
if ( T_OPEN_PARENTHESIS !== $tokens[ $openBracket ]['code'] ) {
51+
// Not a function call.
52+
return ;
53+
}
54+
55+
$plugin_name = $phpcsFile->findNext( Tokens::$emptyTokens, ( $openBracket + 1 ), null, true );
56+
57+
if ( 'zoninator' !== $this->remove_wrapping_quotation_marks( $tokens[ $plugin_name ]['content'] ) ) {
58+
return;
59+
}
60+
61+
$comma = $phpcsFile->findNext( Tokens::$emptyTokens, ( $plugin_name + 1 ), null, true );
62+
63+
if ( ! $comma || 'PHPCS_T_COMMA' !== $tokens[ $comma ]['code'] ) {
64+
// We are loading the default version.
65+
return;
66+
}
67+
68+
$folder = $phpcsFile->findNext( Tokens::$emptyTokens, ( $comma + 1 ), null, true );
69+
70+
$comma = $phpcsFile->findNext( Tokens::$emptyTokens, ( $folder + 1 ), null, true );
71+
72+
if ( ! $comma || 'PHPCS_T_COMMA' !== $tokens[ $comma ]['code'] ) {
73+
// We are loading the default version.
74+
return;
75+
}
76+
77+
$version = $phpcsFile->findNext( Tokens::$emptyTokens, ( $comma + 1 ), null, true );
78+
$version = $this->remove_wrapping_quotation_marks( $tokens[ $version ]['content'] );
79+
80+
if ( true === version_compare( $version, '0.8', '>=' ) ) {
81+
$phpcsFile->addWarning( 'Zoninator of version >= v0.8 requires WordPress core REST API. Please, make sure the `wpcom_vip_load_wp_rest_api()` is being called on all sites loading this file.', $stackPtr, 'Zoninator' );
82+
}
83+
84+
}//end process()
85+
86+
public function remove_wrapping_quotation_marks( $string ) {
87+
return trim( str_replace( '"', "'", $string ), "'" );
88+
}
89+
90+
}//end class
91+
92+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
wpcom_vip_load_plugin( 'zoninator', 'plugins', '0.8' ); // NOK.
4+
wpcom_vip_load_plugin( "zoninator", 'plugins', '0.8' ); // NOK.
5+
wpcom_vip_load_plugin( 'zoninator', 'plugins', '0.9' ); // NOK.
6+
wpcom_vip_load_plugin( 'zoninator', 'plugins', '1.9' ); // NOK.
7+
8+
wpcom_vip_load_plugin( 'zoninator', 'plugins', '0.7' ); // OK.
9+
wpcom_vip_load_plugin( 'zoninator', 'plugins', '0.6' ); // OK.
10+
wpcom_vip_load_plugin( 'zoninator', 'plugins', '0.6' ); // OK.
11+
12+
wpcom_vip_load_plugin( 'zoninator', 'plugins' ); // OK.
13+
wpcom_vip_load_plugin( 'zoninator' ); // OK.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Unit test class for WordPressVIPMinimum Coding Standard.
4+
*
5+
* @package VIPCS\WordPressVIPMinimum
6+
*/
7+
8+
namespace WordPressVIPMinimum\Tests\Plugins;
9+
10+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
11+
12+
/**
13+
* Unit test class for the CheckReturnValue sniff.
14+
*
15+
* @package VIPCS\WordPressVIPMinimum
16+
*/
17+
class ZoninatorUnitTest extends AbstractSniffUnitTest {
18+
19+
/**
20+
* Returns the lines where errors should occur.
21+
*
22+
* @return array <int line number> => <int number of errors>
23+
*/
24+
public function getErrorList() {
25+
return array();
26+
}
27+
28+
/**
29+
* Returns the lines where warnings should occur.
30+
*
31+
* @return array <int line number> => <int number of warnings>
32+
*/
33+
public function getWarningList() {
34+
return array(
35+
3 => 1,
36+
4 => 1,
37+
5 => 1,
38+
6 => 1,
39+
);
40+
41+
}
42+
43+
} // End class.

0 commit comments

Comments
 (0)