| Conditions | 26 |
| Paths | 2022 |
| Total Lines | 120 |
| Code Lines | 66 |
| Lines | 3 |
| Ratio | 2.5 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 94 | public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { |
||
| 95 | if ( ! isset( self::$methods['all'] ) ) { |
||
| 96 | self::$methods['all'] = array_merge( self::$methods['cachable'], self::$methods['noncachable'] ); |
||
| 97 | |||
| 98 | WordPress_Sniff::$cacheGetFunctions = array_merge( |
||
| 99 | WordPress_Sniff::$cacheGetFunctions, |
||
| 100 | array_flip( $this->customCacheGetFunctions ) |
||
| 101 | ); |
||
| 102 | |||
| 103 | WordPress_Sniff::$cacheSetFunctions = array_merge( |
||
| 104 | WordPress_Sniff::$cacheSetFunctions, |
||
| 105 | array_flip( $this->customCacheSetFunctions ) |
||
| 106 | ); |
||
| 107 | |||
| 108 | WordPress_Sniff::$cacheDeleteFunctions = array_merge( |
||
| 109 | WordPress_Sniff::$cacheDeleteFunctions, |
||
| 110 | array_flip( $this->customCacheDeleteFunctions ) |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | |||
| 114 | $tokens = $phpcsFile->getTokens(); |
||
| 115 | |||
| 116 | // Check for $wpdb variable. |
||
| 117 | if ( '$wpdb' !== $tokens[ $stackPtr ]['content'] ) { |
||
| 118 | return; |
||
| 119 | } |
||
| 120 | |||
| 121 | $is_object_call = $phpcsFile->findNext( array( T_OBJECT_OPERATOR ), ( $stackPtr + 1 ), null, null, null, true ); |
||
| 122 | if ( false === $is_object_call ) { |
||
| 123 | return; // This is not a call to the wpdb object. |
||
| 124 | } |
||
| 125 | |||
| 126 | $methodPtr = $phpcsFile->findNext( array( T_WHITESPACE ), ( $is_object_call + 1 ), null, true, null, true ); |
||
| 127 | $method = $tokens[ $methodPtr ]['content']; |
||
| 128 | |||
| 129 | if ( ! isset( self::$methods['all'][ $method ] ) ) { |
||
| 130 | return; |
||
| 131 | } |
||
| 132 | |||
| 133 | $endOfStatement = $phpcsFile->findNext( array( T_SEMICOLON ), ( $stackPtr + 1 ), null, null, null, true ); |
||
| 134 | $endOfLineComment = ''; |
||
| 135 | $tokenCount = count( $tokens ); |
||
| 136 | for ( $i = ( $endOfStatement + 1 ); $i < $tokenCount; $i++ ) { |
||
| 137 | |||
| 138 | if ( $tokens[ $i ]['line'] !== $tokens[ $endOfStatement ]['line'] ) { |
||
| 139 | break; |
||
| 140 | } |
||
| 141 | |||
| 142 | if ( T_COMMENT === $tokens[ $i ]['code'] ) { |
||
| 143 | $endOfLineComment .= $tokens[ $i ]['content']; |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | $whitelisted_db_call = false; |
||
| 148 | if ( preg_match( '/db call\W*(?:ok|pass|clear|whitelist)/i', $endOfLineComment ) ) { |
||
| 149 | $whitelisted_db_call = true; |
||
| 150 | } |
||
| 151 | |||
| 152 | // Check for Database Schema Changes. |
||
| 153 | $_pos = $stackPtr; |
||
| 154 | while ( $_pos = $phpcsFile->findNext( array( T_CONSTANT_ENCAPSED_STRING, T_DOUBLE_QUOTED_STRING ), ( $_pos + 1 ), $endOfStatement, null, null, true ) ) { |
||
| 155 | if ( preg_match( '#\b(?:ALTER|CREATE|DROP)\b#i', $tokens[ $_pos ]['content'] ) > 0 ) { |
||
| 156 | $phpcsFile->addError( 'Attempting a database schema change is highly discouraged.', $_pos, 'SchemaChange' ); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | // Flag instance if not whitelisted. |
||
| 161 | if ( ! $whitelisted_db_call ) { |
||
| 162 | $phpcsFile->addWarning( 'Usage of a direct database call is discouraged.', $stackPtr, 'DirectQuery' ); |
||
| 163 | } |
||
| 164 | |||
| 165 | if ( ! isset( self::$methods['cachable'][ $method ] ) ) { |
||
| 166 | return $endOfStatement; |
||
| 167 | } |
||
| 168 | |||
| 169 | $whitelisted_cache = false; |
||
| 170 | $cached = $wp_cache_get = false; |
||
| 171 | if ( preg_match( '/cache\s+(?:ok|pass|clear|whitelist)/i', $endOfLineComment ) ) { |
||
| 172 | $whitelisted_cache = true; |
||
| 173 | } |
||
| 174 | if ( ! $whitelisted_cache && ! empty( $tokens[ $stackPtr ]['conditions'] ) ) { |
||
| 175 | $scope_function = $phpcsFile->getCondition( $stackPtr, T_FUNCTION ); |
||
| 176 | |||
| 177 | if ( $scope_function ) { |
||
| 178 | $scopeStart = $tokens[ $scope_function ]['scope_opener']; |
||
| 179 | $scopeEnd = $tokens[ $scope_function ]['scope_closer']; |
||
| 180 | |||
| 181 | for ( $i = ( $scopeStart + 1 ); $i < $scopeEnd; $i++ ) { |
||
| 182 | if ( T_STRING === $tokens[ $i ]['code'] ) { |
||
| 183 | |||
| 184 | if ( isset( WordPress_Sniff::$cacheDeleteFunctions[ $tokens[ $i ]['content'] ] ) ) { |
||
| 185 | |||
| 186 | if ( in_array( $method, array( 'query', 'update', 'replace', 'delete' ), true ) ) { |
||
| 187 | $cached = true; |
||
| 188 | break; |
||
| 189 | } |
||
| 190 | } elseif ( isset( WordPress_Sniff::$cacheGetFunctions[ $tokens[ $i ]['content'] ] ) ) { |
||
| 191 | |||
| 192 | $wp_cache_get = true; |
||
| 193 | |||
| 194 | } elseif ( isset( WordPress_Sniff::$cacheSetFunctions[ $tokens[ $i ]['content'] ] ) ) { |
||
| 195 | |||
| 196 | if ( $wp_cache_get ) { |
||
| 197 | $cached = true; |
||
| 198 | break; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | } |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | if ( ! $cached && ! $whitelisted_cache ) { |
||
| 207 | $message = 'Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set or wp_cache_delete.'; |
||
| 208 | $phpcsFile->addError( $message, $stackPtr, 'NoCaching' ); |
||
| 209 | } |
||
| 210 | |||
| 211 | return $endOfStatement; |
||
| 212 | |||
| 213 | } // end process() |
||
| 214 | |||
| 216 |