Skip to content

Commit 7a4c300

Browse files
committed
Add more resticted constants
This commit adds JETPACK_DEV_DEBUG and WP_CRON_CONTROL_SECRET constants to the list of restricted ones. In contrast to A8C_PROXIED_REQUEST constant, those two are good to be worked with, just should not be defined. This new use-case prompted changes in the ConstantRestrictionsSniff itself - new conditions have been added to the code. Unit tests have been ammended accordingly.
1 parent 092e55d commit 7a4c300

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

WordPressVIPMinimum/Sniffs/Constants/ConstantRestrictionsSniff.php

Lines changed: 15 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,14 @@ 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 )
71+
&& false == in_array( $constantName, $this->restrictedConstantDeclaration, true )
72+
) {
6173
// Not the constant we are looking for.
6274
return;
6375
}
6476

65-
if ( T_STRING === $tokens[ $stackPtr ]['code'] ) {
77+
if ( T_STRING === $tokens[ $stackPtr ]['code'] && true === in_array( $constantName, $this->restrictedConstantNames, true ) ) {
6678
$phpcsFile->addWarning( sprintf( 'Code is touching the %s constant. Make sure it\'s used appropriately.', $constantName ), $stackPtr, 'ConstantRestrictions' );
6779
return;
6880
}
@@ -92,7 +104,7 @@ public function process( File $phpcsFile, $stackPtr ) {
92104
if ( true === in_array( $tokens[ $previous ]['code'], Tokens::$functionNameTokens, true ) ) {
93105
if ( 'define' === $tokens[ $previous ]['content'] ) {
94106
$phpcsFile->addError( sprintf( 'The definition of %s constant is prohibited. Please use a different name.', $constantName ), $previous, 'ConstantRestrictions' );
95-
} else {
107+
} elseif ( true === in_array( $constantName, $this->restrictedConstantNames, true ) ) {
96108
$phpcsFile->addWarning( sprintf( 'Code is touching the %s constant. Make sure it\'s used appropriately.', $constantName ), $previous, 'ConstantRestrictions' );
97109
}
98110
}

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)