Skip to content

Commit 682ed63

Browse files
authored
Merge pull request #90 from Automattic/phpcs3-compat
PHPCS 3.x compatibility changes
2 parents b5bba7a + c75381f commit 682ed63

33 files changed

+450
-281
lines changed

.travis.yml

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,86 @@
11
sudo: false
22

3+
dist: trusty
4+
5+
cache:
6+
apt: true
7+
38
language:
49
- php
510

611
php:
7-
- 5.2
8-
- 5.3
912
- 5.4
1013
- 5.5
1114
- 5.6
1215
- 7.0
1316
- 7.1
17+
- nightly
1418

1519
env:
16-
- PHPCS_BRANCH=master
17-
- PHPCS_BRANCH=2.8.1
20+
# `master` is now 3.x.
21+
- PHPCS_BRANCH=master LINT=1
22+
# Lowest tagged release in the 2.x series with which WPCS is compatible.
23+
- PHPCS_BRANCH=2.9.0
1824

1925
matrix:
2026
fast_finish: true
2127
include:
22-
# Run PHPCS against WPCS. I just picked to run it against 5.5.
23-
- php: 5.5
28+
# Run PHPCS against WPCS. I just picked to run it against 7.0.
29+
- php: 7.0
2430
env: PHPCS_BRANCH=master SNIFF=1
25-
# Run against PHPCS 3.0. I just picked to run it against 5.6.
26-
- php: 5.6
27-
env: PHPCS_BRANCH=3.0
31+
addons:
32+
apt:
33+
packages:
34+
- libxml2-utils
35+
2836
# Run against HHVM and PHP nightly.
2937
- php: hhvm
3038
sudo: required
3139
dist: trusty
3240
group: edge
33-
env: PHPCS_BRANCH=master
34-
- php: nightly
35-
env: PHPCS_BRANCH=master
36-
# Run custom integration test. I just picked to run it against 7.0.
41+
env: PHPCS_BRANCH=master LINT=1
42+
43+
# Test PHP 5.3 only against PHPCS 2.x as PHPCS 3.x has a minimum requirement of PHP 5.4.
44+
- php: 5.3
45+
env: PHPCS_BRANCH=2.9 LINT=1
46+
dist: precise
47+
# Test PHP 5.3 with short_open_tags set to On (is Off by default)
48+
- php: 5.3
49+
env: PHPCS_BRANCH=2.9.0 SHORT_OPEN_TAGS=true
50+
dist: precise
51+
# Run custom integration test. I just picked to it agains PHP 7.0.
3752
- php: 7.0
3853
env: PHPCS_BRANCH=master INTEGRATION_TEST=1
54+
3955
allow_failures:
4056
# Allow failures for unstable builds.
4157
- php: nightly
4258
- php: hhvm
43-
- env: PHPCS_BRANCH=3.0
44-
4559

46-
before_script:
47-
- export PATH="$HOME/.composer/vendor/bin:$PATH"
60+
before_install:
61+
- export XMLLINT_INDENT=" "
4862
- export PHPCS_DIR=/tmp/phpcs
4963
- export WPCS_DIR=/tmp/wpcs
50-
- export PHPCS_BIN=$(if [[ $PHPCS_BRANCH == 3.0 ]]; then echo $PHPCS_DIR/bin/phpcs; else echo $PHPCS_DIR/scripts/phpcs; fi)
64+
- export PHPUNIT_DIR=/tmp/phpunit
65+
- export PHPCS_BIN=$(if [[ $PHPCS_BRANCH == master ]]; then echo $PHPCS_DIR/bin/phpcs; else echo $PHPCS_DIR/scripts/phpcs; fi)
5166
- mkdir -p $PHPCS_DIR && git clone --depth 1 https://github.com/squizlabs/PHP_CodeSniffer.git -b $PHPCS_BRANCH $PHPCS_DIR
5267
- mkdir -p $WPCS_DIR && git clone --depth 1 https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git -b master $WPCS_DIR
53-
- ln -s $WPCS_DIR/WordPress $PHPCS_DIR/CodeSniffer/Standards/WordPress
54-
- ln -s $WPCS_DIR/WordPress-Core $PHPCS_DIR/CodeSniffer/Standards/WordPress-Core
55-
- ln -s $WPCS_DIR/WordPress-Docs $PHPCS_DIR/CodeSniffer/Standards/WordPress-Docs
56-
- ln -s $WPCS_DIR/WordPress-Extra $PHPCS_DIR/CodeSniffer/Standards/WordPress-Extra
57-
- ln -s $WPCS_DIR/WordPress-VIP $PHPCS_DIR/CodeSniffer/Standards/WordPress-VIP
58-
- $PHPCS_BIN --config-set installed_paths $(pwd)
59-
- |
60-
if [[ ${TRAVIS_PHP_VERSION:0:2} == "7." ]]; then
61-
composer global require "phpunit/phpunit=5.7.*"
62-
elif [[ ${TRAVIS_PHP_VERSION:0:3} != "5.2" ]]; then
63-
composer global require "phpunit/phpunit=4.8.*"
64-
fi;
65-
- phpunit --version
68+
- $PHPCS_BIN --config-set installed_paths $(pwd),$WPCS_DIR
69+
# Download PHPUnit 5.x for builds on PHP 7, nightly and HHVM as the PHPCS
70+
# test suite is currently not compatible with PHPUnit 6.x.
71+
# Fixed at a very specific PHPUnit version which is also compatible with HHVM.
72+
- if [[ ${TRAVIS_PHP_VERSION:0:2} != "5." ]]; then wget -P $PHPUNIT_DIR https://phar.phpunit.de/phpunit-5.7.17.phar && chmod +x $PHPUNIT_DIR/phpunit-5.7.17.phar; fi
73+
# Selectively adjust the ini values for the build image to test ini value dependent sniff features.
74+
- if [[ "$SHORT_OPEN_TAGS" == "true" ]]; then echo "short_open_tag = On" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi
6675

6776
script:
68-
- if find . -name "*.php" -exec php -l {} \; | grep "^[Parse error|Fatal error]"; then exit 1; fi;
69-
- phpunit --filter WordPressVIPMinimum /tmp/phpcs/tests/AllTests.php
77+
# Lint the PHP files against parse errors.
78+
- if [[ "$LINT" == "1" ]]; then if find . -name "*.php" -exec php -l {} \; | grep "^[Parse error|Fatal error]"; then exit 1; fi; fi
79+
# Run the unit tests.
80+
- if [[ ${TRAVIS_PHP_VERSION:0:2} == "5." && ${PHPCS_BRANCH:0:2} == "2." ]]; then phpunit --bootstrap $WPCS_DIR/Test/bootstrap.php --filter WordPressVIPMinimum $WPCS_DIR/Test/AllTests.php; fi
81+
- if [[ ${TRAVIS_PHP_VERSION:0:2} == "5." && ${PHPCS_BRANCH:0:2} != "2." ]]; then phpunit --bootstrap $WPCS_DIR/Test/bootstrap.php --filter WordPressVIPMinimum $PHPCS_DIR/tests/AllTests.php; fi
82+
- if [[ ${TRAVIS_PHP_VERSION:0:2} != "5." && ${PHPCS_BRANCH:0:2} == "2." ]]; then php $PHPUNIT_DIR/phpunit-5.7.17.phar --bootstrap $WPCS_DIR/Test/bootstrap.php --filter WordPressVIPMinimum $WPCS_DIR/Test/AllTests.php; fi
83+
- if [[ ${TRAVIS_PHP_VERSION:0:2} != "5." && ${PHPCS_BRANCH:0:2} != "2." ]]; then php $PHPUNIT_DIR/phpunit-5.7.17.phar --bootstrap $WPCS_DIR/Test/bootstrap.php --filter WordPressVIPMinumum $PHPCS_DIR/tests/AllTests.php; fi
7084
# WordPress Coding Standards.
7185
# @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
7286
# @link http://pear.php.net/package/PHP_CodeSniffer/
@@ -76,5 +90,10 @@ script:
7690
# -n flag: Do not print warnings. (shortcut for --warning-severity=0)
7791
# --standard: Use WordPress as the standard.
7892
# --extensions: Only sniff PHP files.
79-
- if [[ "$SNIFF" == "1" ]]; then $PHPCS_DIR/scripts/phpcs -p -s -n . --standard=./bin/phpcs.xml --extensions=php; fi
93+
- if [[ "$SNIFF" == "1" ]]; then $PHPCS_BIN . --standard=./bin/phpcs.xml --runtime-set ignore_warnings_on_exit 1; fi
94+
# Validate the xml files.
95+
# @link http://xmlsoft.org/xmllint.html
96+
- if [[ "$SNIFF" == "1" ]]; then xmllint --noout ./*/ruleset.xml; fi
97+
# Check the code-style consistency of the xml files.
98+
- if [[ "$SNIFF" == "1" ]]; then diff -B --tabsize=4 ./WordPressVIPMinimum/ruleset.xml <(xmllint --format "./WordPressVIPMinimum/ruleset.xml"); fi
8099
- if [[ "$INTEGRATION_TEST" == "1" ]]; then php ./ruleset_test.php; fi

WordPressVIPMinimum/Sniffs/Actions/PreGetPostsSniff.php

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
* @package VIPCS\WordPressVIPMinimum
66
*/
77

8+
namespace WordPressVIPMinimum\Sniffs\Actions;
9+
10+
use PHP_CodeSniffer_File as File;
11+
use PHP_CodeSniffer_Tokens as Tokens;
12+
813
/**
914
* This sniff validates a propper usage of pre_get_posts action callback
1015
*
1116
* It looks for cases when the WP_Query object is being modified without checking for WP_Query::is_main_query().
1217
*
1318
* @package VIPCS\WordPressVIPMinimum
1419
*/
15-
class WordPressVIPminimum_Sniffs_Actions_PreGetPostsSniff implements PHP_CodeSniffer_Sniff {
20+
class PreGetPostsSniff implements \PHP_CodeSniffer_Sniff {
1621

1722
/**
1823
* The tokens of the phpcsFile.
@@ -34,21 +39,21 @@ class WordPressVIPminimum_Sniffs_Actions_PreGetPostsSniff implements PHP_CodeSni
3439
* @return array(int)
3540
*/
3641
public function register() {
37-
return PHP_CodeSniffer_Tokens::$functionNameTokens;
42+
return Tokens::$functionNameTokens;
3843

3944
}//end register()
4045

4146

4247
/**
4348
* Processes the tokens that this sniff is interested in.
4449
*
45-
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
46-
* @param int $stackPtr The position in the stack where
47-
* the token was found.
50+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
51+
* @param int $stackPtr The position in the stack where
52+
* the token was found.
4853
*
4954
* @return void
5055
*/
51-
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
56+
public function process( File $phpcsFile, $stackPtr ) {
5257

5358
$this->_tokens = $phpcsFile->getTokens();
5459

@@ -62,7 +67,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
6267
}
6368

6469
$actionNamePtr = $this->_phpcsFile->findNext(
65-
array_merge( PHP_CodeSniffer_Tokens::$emptyTokens, array( T_OPEN_PARENTHESIS ) ), // types.
70+
array_merge( Tokens::$emptyTokens, array( T_OPEN_PARENTHESIS ) ), // types.
6671
$stackPtr + 1, // start.
6772
null, // end.
6873
true, // exclude.
@@ -81,7 +86,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
8186
}
8287

8388
$callbackPtr = $this->_phpcsFile->findNext(
84-
array_merge( PHP_CodeSniffer_Tokens::$emptyTokens, array( T_COMMA ) ), // types.
89+
array_merge( Tokens::$emptyTokens, array( T_COMMA ) ), // types.
8590
$actionNamePtr + 1, // start.
8691
null, // end.
8792
true, // exclude.
@@ -96,9 +101,9 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
96101

97102
if ( 'PHPCS_T_CLOSURE' === $this->_tokens[ $callbackPtr ]['code'] ) {
98103
$this->processClosure( $callbackPtr );
99-
} else if ( 'T_ARRAY' === $this->_tokens[ $callbackPtr ]['type'] ) {
104+
} elseif ( 'T_ARRAY' === $this->_tokens[ $callbackPtr ]['type'] ) {
100105
$this->processArray( $callbackPtr );
101-
} else if ( true === in_array( $this->_tokens[ $callbackPtr ]['code'], PHP_CodeSniffer_Tokens::$stringTokens ) ) {
106+
} elseif ( true === in_array( $this->_tokens[ $callbackPtr ]['code'], Tokens::$stringTokens, true ) ) {
102107
$this->processString( $callbackPtr );
103108
}
104109

@@ -112,7 +117,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
112117
private function processArray( $stackPtr ) {
113118

114119
$previous = $this->_phpcsFile->findPrevious(
115-
PHP_CodeSniffer_Tokens::$emptyTokens, // types
120+
Tokens::$emptyTokens, // types
116121
$this->_tokens[ $stackPtr ]['parenthesis_closer'] - 1, // start.
117122
null, // end.
118123
true, // exclude.
@@ -134,7 +139,7 @@ private function processString( $stackPtr ) {
134139
$callbackFunctionName = substr( $this->_tokens[ $stackPtr ]['content'], 1, -1 );
135140

136141
$callbackFunctionPtr = $this->_phpcsFile->findNext(
137-
PHP_CodeSniffer_Tokens::$functionNameTokens, // types.
142+
Tokens::$functionNameTokens, // types.
138143
0, // start.
139144
null, // end.
140145
false, // exclude.
@@ -242,12 +247,12 @@ private function processFunctionBody( $stackPtr, $variableName ) {
242247
if ( $this->isEarlyMainQueryCheck( $wpQueryVarUsed ) ) {
243248
return;
244249
}
245-
} else if ( $this->isInsideIfConditonal( $wpQueryVarUsed ) ) {
250+
} elseif ( $this->isInsideIfConditonal( $wpQueryVarUsed ) ) {
246251
if ( ! $this->isParentConditionalCheckingMainQuery( $wpQueryVarUsed ) ) {
247-
$this->_phpcsFile->addWarning( 'Main WP_Query is being modified without $query->is_main_query() check. Needs manual inspection.', $wpQueryVarUsed );
252+
$this->_phpcsFile->addWarning( 'Main WP_Query is being modified without $query->is_main_query() check. Needs manual inspection.', $wpQueryVarUsed, 'PreGetPosts' );
248253
}
249-
} else if ( $this->isWPQueryMethodCall( $wpQueryVarUsed, 'set' ) ) {
250-
$this->_phpcsFile->addWarning( 'Main WP_Query is being modified without $query->is_main_query() check. Needs manual inspection.', $wpQueryVarUsed );
254+
} elseif ( $this->isWPQueryMethodCall( $wpQueryVarUsed, 'set' ) ) {
255+
$this->_phpcsFile->addWarning( 'Main WP_Query is being modified without $query->is_main_query() check. Needs manual inspection.', $wpQueryVarUsed, 'PreGetPosts' );
251256
}
252257
$findStart = $wpQueryVarUsed + 1;
253258
}
@@ -316,7 +321,7 @@ private function isEarlyMainQueryCheck( $stackPtr ) {
316321
}
317322

318323
$nestedParenthesisEnd = array_shift( $this->_tokens[ $stackPtr ]['nested_parenthesis'] );
319-
if ( true === in_array( 'PHPCS_T_CLOSURE', $this->_tokens[ $stackPtr ]['conditions'] ) ) {
324+
if ( true === in_array( 'PHPCS_T_CLOSURE', $this->_tokens[ $stackPtr ]['conditions'], true ) ) {
320325
$nestedParenthesisEnd = array_shift( $this->_tokens[ $stackPtr ]['nested_parenthesis'] );
321326
}
322327

@@ -346,7 +351,7 @@ private function isEarlyMainQueryCheck( $stackPtr ) {
346351
*/
347352
private function isWPQueryMethodCall( $stackPtr, $method = null ) {
348353
$next = $this->_phpcsFile->findNext(
349-
PHP_CodeSniffer_Tokens::$emptyTokens, // types.
354+
Tokens::$emptyTokens, // types.
350355
$stackPtr + 1, // start.
351356
null, // end.
352357
true, // exclude.
@@ -363,7 +368,7 @@ private function isWPQueryMethodCall( $stackPtr, $method = null ) {
363368
}
364369

365370
$next = $this->_phpcsFile->findNext(
366-
PHP_CodeSniffer_Tokens::$emptyTokens, // types.
371+
Tokens::$emptyTokens, // types.
367372
$next + 1, // start.
368373
null, // end.
369374
true, // exclude.
@@ -372,7 +377,7 @@ private function isWPQueryMethodCall( $stackPtr, $method = null ) {
372377
);
373378

374379
if ( $next &&
375-
true === in_array( $this->_tokens[ $next ]['code'], PHP_CodeSniffer_Tokens::$functionNameTokens, true ) &&
380+
true === in_array( $this->_tokens[ $next ]['code'], Tokens::$functionNameTokens, true ) &&
376381
$method === $this->_tokens[ $next ]['content']
377382
) {
378383
return true;

WordPressVIPMinimum/Sniffs/Cache/CacheValueOverrideSniff.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
* @package VIPCS\WordPressVIPMinimum
66
*/
77

8+
namespace WordPressVIPMinimum\Sniffs\Cache;
9+
10+
use PHP_CodeSniffer_Sniff as PHPCS_Sniff;
11+
use PHP_CodeSniffer_File as File;
12+
use PHP_CodeSniffer_Tokens as Tokens;
13+
814
/**
915
* This sniff enforces checking the return value of a function before passing it to anoher one.
1016
*
@@ -16,7 +22,7 @@
1622
*
1723
* @package VIPCS\WordPressVIPMinimum
1824
*/
19-
class WordPressVIPminimum_Sniffs_Cache_CacheValueOverrideSniff implements PHP_CodeSniffer_Sniff {
25+
class CacheValueOverrideSniff implements PHPCS_Sniff {
2026

2127
/**
2228
* Tokens of the file.
@@ -31,21 +37,21 @@ class WordPressVIPminimum_Sniffs_Cache_CacheValueOverrideSniff implements PHP_Co
3137
* @return array(int)
3238
*/
3339
public function register() {
34-
return PHP_CodeSniffer_Tokens::$functionNameTokens;
40+
return Tokens::$functionNameTokens;
3541

3642
}//end register()
3743

3844

3945
/**
4046
* Processes the tokens that this sniff is interested in.
4147
*
42-
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
43-
* @param int $stackPtr The position in the stack where
44-
* the token was found.
48+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
49+
* @param int $stackPtr The position in the stack where
50+
* the token was found.
4551
*
4652
* @return void
4753
*/
48-
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
54+
public function process( File $phpcsFile, $stackPtr ) {
4955

5056
$this->_tokens = $phpcsFile->getTokens();
5157
$tokens = $phpcsFile->getTokens();
@@ -74,24 +80,24 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
7480
$variableName = $variableToken['content'];
7581

7682
// Find the next non-empty token.
77-
$openBracket = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true );
83+
$openBracket = $phpcsFile->findNext( Tokens::$emptyTokens, ($stackPtr + 1), null, true );
7884

7985
// Find the closing bracket.
8086
$closeBracket = $tokens[ $openBracket ]['parenthesis_closer'];
8187

8288
$nextVariableOccurrence = $phpcsFile->findNext( T_VARIABLE, ($closeBracket + 1), null, false, $variableName, false );
8389

84-
$rightAfterNextVariableOccurence = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($nextVariableOccurrence + 1), null, true, null, true );
90+
$rightAfterNextVariableOccurence = $phpcsFile->findNext( Tokens::$emptyTokens, ($nextVariableOccurrence + 1), null, true, null, true );
8591

8692
if ( T_EQUAL !== $tokens[ $rightAfterNextVariableOccurence ]['code'] ) {
8793
// Not a value override.
8894
return;
8995
}
9096

91-
$valueAfterEqualSign = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($rightAfterNextVariableOccurence + 1), null, true, null, true );
97+
$valueAfterEqualSign = $phpcsFile->findNext( Tokens::$emptyTokens, ($rightAfterNextVariableOccurence + 1), null, true, null, true );
9298

9399
if ( T_FALSE === $tokens[ $valueAfterEqualSign ]['code'] ) {
94-
$phpcsFile->addError( sprintf( 'Obtained cached value in %s is being overriden. Disabling caching?', $variableName ), $nextVariableOccurrence );
100+
$phpcsFile->addError( sprintf( 'Obtained cached value in %s is being overriden. Disabling caching?', $variableName ), $nextVariableOccurrence, 'CacheValueOverride' );
95101
}
96102

97103
} //end Process()
@@ -108,20 +114,20 @@ private function isFunctionCall( $stackPtr ) {
108114
$tokens = $this->_tokens;
109115
$phpcsFile = $this->_phpcsFile;
110116

111-
if ( false === in_array( $tokens[ $stackPtr ]['code'], PHP_CodeSniffer_Tokens::$functionNameTokens ) ) {
117+
if ( false === in_array( $tokens[ $stackPtr ]['code'], Tokens::$functionNameTokens, true ) ) {
112118
return false;
113119
}
114120

115121
// Find the next non-empty token.
116-
$openBracket = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true );
122+
$openBracket = $phpcsFile->findNext( Tokens::$emptyTokens, ($stackPtr + 1), null, true );
117123

118124
if ( T_OPEN_PARENTHESIS !== $tokens[ $openBracket ]['code'] ) {
119125
// Not a function call.
120126
return false;
121127
}
122128

123129
// Find the previous non-empty token.
124-
$search = PHP_CodeSniffer_Tokens::$emptyTokens;
130+
$search = Tokens::$emptyTokens;
125131
$search[] = T_BITWISE_AND;
126132
$previous = $phpcsFile->findPrevious( $search, ($stackPtr - 1), null, true );
127133
if ( T_FUNCTION === $tokens[ $previous ]['code'] ) {
@@ -145,7 +151,7 @@ private function isVariableAssignment( $stackPtr ) {
145151
$phpcsFile = $this->_phpcsFile;
146152

147153
// Find the previous non-empty token.
148-
$search = PHP_CodeSniffer_Tokens::$emptyTokens;
154+
$search = Tokens::$emptyTokens;
149155
$search[] = T_BITWISE_AND;
150156
$previous = $phpcsFile->findPrevious( $search, ($stackPtr - 1), null, true );
151157

0 commit comments

Comments
 (0)