Skip to content

Commit cbeabe9

Browse files
authored
Merge pull request #93 from Automattic/add-more-restricted-contants
Add more resticted constants
2 parents def8412 + 457b03e commit cbeabe9

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

WordPressVIPMinimum/Sniffs/Constants/ConstantRestrictionsSniff.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ class ConstantRestrictionsSniff implements \PHP_CodeSniffer_Sniff {
2727
'A8C_PROXIED_REQUEST',
2828
);
2929

30+
/**
31+
* List of restricted constant declarations.
32+
*
33+
* @var array
34+
*/
35+
public $restrictedConstantDeclaration = array(
36+
'JETPACK_DEV_DEBUG',
37+
'WP_CRON_CONTROL_SECRET',
38+
);
39+
3040
/**
3141
* Returns an array of tokens this test wants to listen for.
3242
*
@@ -57,12 +67,12 @@ public function process( File $phpcsFile, $stackPtr ) {
5767
$constantName = trim( $tokens[ $stackPtr ]['content'], "\"'" );
5868
}
5969

60-
if ( false === in_array( $constantName, $this->restrictedConstantNames, true ) ) {
70+
if ( false === in_array( $constantName, $this->restrictedConstantNames, true ) && false === in_array( $constantName, $this->restrictedConstantDeclaration, true ) ) {
6171
// Not the constant we are looking for.
6272
return;
6373
}
6474

65-
if ( T_STRING === $tokens[ $stackPtr ]['code'] ) {
75+
if ( T_STRING === $tokens[ $stackPtr ]['code'] && true === in_array( $constantName, $this->restrictedConstantNames, true ) ) {
6676
$phpcsFile->addWarning( sprintf( 'Code is touching the %s constant. Make sure it\'s used appropriately.', $constantName ), $stackPtr, 'ConstantRestrictions' );
6777
return;
6878
}
@@ -92,7 +102,7 @@ public function process( File $phpcsFile, $stackPtr ) {
92102
if ( true === in_array( $tokens[ $previous ]['code'], Tokens::$functionNameTokens, true ) ) {
93103
if ( 'define' === $tokens[ $previous ]['content'] ) {
94104
$phpcsFile->addError( sprintf( 'The definition of %s constant is prohibited. Please use a different name.', $constantName ), $previous, 'ConstantRestrictions' );
95-
} else {
105+
} elseif ( true === in_array( $constantName, $this->restrictedConstantNames, true ) ) {
96106
$phpcsFile->addWarning( sprintf( 'Code is touching the %s constant. Make sure it\'s used appropriately.', $constantName ), $previous, 'ConstantRestrictions' );
97107
}
98108
}

WordPressVIPMinimum/Tests/Constants/ConstantRestrictionsUnitTest.inc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,16 @@ if ( defined( 'A8C_PROXIED_REQUEST' ) && true === constant( 'A8C_PROXIED_REQUEST
88

99
}
1010

11-
define( 'A8C_PROXIED_REQUEST', false ); // Bad. Should never try to define this.
11+
define( 'A8C_PROXIED_REQUEST', false ); // Bad. Should never attempt to define this.
12+
13+
define( 'JETPACK_DEV_DEBUG', true ); // Bad. Should never attempt to define this.
14+
15+
define( 'WP_CRON_CONTROL_SECRET', true ); // Bad. Should never attempt to define this.
16+
17+
if ( defined( 'JETPACK_DEV_DEBUG' ) ) { // Okay. Can touch.
18+
19+
}
20+
21+
if ( constant( 'WP_CRON_CONTROL_SECRET' ) ) { // Okay. Can touch.
22+
23+
}

WordPressVIPMinimum/Tests/Constants/ConstantRestrictionsUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class ConstantRestrictionsUnitTest extends AbstractSniffUnitTest {
2424
public function getErrorList() {
2525
return array(
2626
11 => 1,
27+
13 => 1,
28+
15 => 1,
2729
);
2830
}
2931

0 commit comments

Comments
 (0)