|
| 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 | + /** |
| 87 | + * Removes the quotation marks around T_CONSTANT_ENCAPSED_STRING. |
| 88 | + * |
| 89 | + * @param string $string T_CONSTANT_ENCAPSED_STRING containing wrapping quotation marks. |
| 90 | + * |
| 91 | + * @return string String w/o wrapping quotation marks. |
| 92 | + */ |
| 93 | + public function remove_wrapping_quotation_marks( $string ) { |
| 94 | + return trim( str_replace( '"', "'", $string ), "'" ); |
| 95 | + } |
| 96 | + |
| 97 | +}//end class |
| 98 | + |
| 99 | + |
0 commit comments