-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DisallowHooksInConstructor sniff and migrate some classes to use …
…it (#7249)
- Loading branch information
Showing
13 changed files
with
192 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: dev | ||
|
||
Migrate away from hooking into actions in certain classes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
/** | ||
* This sniff prohibits the use of add_action and add_filter in __construct. | ||
*/ | ||
|
||
namespace WCPay\CodingStandards\Sniffs; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
|
||
class DisallowHooksInConstructorSniff implements Sniff { | ||
private $forbiddenFunctions = [ | ||
'add_action', | ||
'add_filter', | ||
]; | ||
|
||
/** | ||
* Returns the token types that this sniff is interested in. | ||
* | ||
* @return array<int> | ||
*/ | ||
public function register() { | ||
return [ \T_FUNCTION ]; | ||
} | ||
|
||
/** | ||
* Processes the sniff if one of its tokens is encountered. | ||
* | ||
* @param File $phpcsFile The current file being checked. | ||
* @param int $stackPtr The position of the current token in the stack passed in $tokens. | ||
* | ||
* @return void | ||
*/ | ||
public function process( File $phpcsFile, $stackPtr ) { | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
// Check that we're looking at the __construct function. | ||
$namePtr = $phpcsFile->findNext( T_STRING, $stackPtr + 1 ); | ||
$nameToken = $tokens[ $namePtr ]; | ||
if ( $nameToken['content'] !== '__construct' ) { | ||
return; | ||
} | ||
|
||
// Retrieve the current token. | ||
$token = $tokens[ $stackPtr ]; | ||
|
||
// Check the current token's scope. | ||
$scopeOpener = $token['scope_opener']; | ||
$scopeCloser = $token['scope_closer']; | ||
|
||
// For every string token in the scope... | ||
for ( $i = $scopeOpener; $i < $scopeCloser; $i++ ) { | ||
if ( $tokens[ $i ]['type'] !== 'T_STRING' ) { | ||
continue; | ||
} | ||
// ...check if it's one of the forbidden functions. | ||
$currentToken = $tokens[ $i ]; | ||
$currentTokenContent = $currentToken['content']; | ||
if ( in_array( $currentTokenContent, $this->forbiddenFunctions ) ) { | ||
$phpcsFile->addError( | ||
"Usage of $currentTokenContent in __construct() is not allowed", | ||
$i, | ||
'WCPay.CodingStandards.DisallowHooksInConstructor', | ||
[ $token['content'] ] | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0"?> | ||
<ruleset name="WCPay" namespace="WCPay\CodingStandards"> | ||
<description>WCPay custom coding standards.</description> | ||
|
||
<!-- https://github.com/Automattic/woocommerce-payments/issues/7258 --> | ||
<exclude-pattern>*/includes/multi-currency/*</exclude-pattern> | ||
|
||
<!-- https://github.com/Automattic/woocommerce-payments/issues/7259 --> | ||
<exclude-pattern>*/includes/subscriptions/*</exclude-pattern> | ||
<exclude-pattern>*/includes/compat/subscriptions/*</exclude-pattern> | ||
|
||
<!-- https://github.com/Automattic/woocommerce-payments/issues/7266 --> | ||
<exclude-pattern>*/includes/emails/*</exclude-pattern> | ||
|
||
<!-- https://github.com/Automattic/woocommerce-payments/issues/7260 --> | ||
<exclude-pattern>*/includes/class-woopay-tracker.php</exclude-pattern> | ||
<exclude-pattern>*/includes/woopay/*</exclude-pattern> | ||
<exclude-pattern>*/includes/woopay-user/*</exclude-pattern> | ||
<exclude-pattern>*/includes/class-wc-payments-order-success-page.php</exclude-pattern> | ||
|
||
<!-- https://github.com/Automattic/woocommerce-payments/issues/7261 --> | ||
<exclude-pattern>*/includes/class-wc-payments-fraud-service.php</exclude-pattern> | ||
<exclude-pattern>*/includes/fraud-prevention/*</exclude-pattern> | ||
|
||
<!-- https://github.com/Automattic/woocommerce-payments/issues/7262 --> | ||
<exclude-pattern>*/includes/class-wc-payments-account.php</exclude-pattern> | ||
<exclude-pattern>*/includes/class-wc-payments-incentives-service.php</exclude-pattern> | ||
<exclude-pattern>*/includes/class-wc-payments-onboarding-service.php</exclude-pattern> | ||
|
||
<!-- https://github.com/Automattic/woocommerce-payments/issues/7263 --> | ||
<exclude-pattern>*/includes/class-wc-payments-apple-pay-registration.php</exclude-pattern> | ||
<exclude-pattern>*/includes/class-wc-payments-express-checkout-button-display-handler.php</exclude-pattern> | ||
|
||
<!-- https://github.com/Automattic/woocommerce-payments/issues/7264 --> | ||
<exclude-pattern>*/includes/class-wc-payments-customer-service.php</exclude-pattern> | ||
<exclude-pattern>*/includes/class-wc-payments-token-service.php</exclude-pattern> | ||
|
||
<!-- https://github.com/Automattic/woocommerce-payments/issues/7265 --> | ||
<exclude-pattern>*/includes/class-wc-payments-webhook-reliability-service.php</exclude-pattern> | ||
</ruleset> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters