-
-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #623 from WordPress-Coding-Standards/WPCS/feature/…
…issue-507-soap-methods Update `ValidFunctionNames` Sniff to allow for non-valid names in extended classes.
- Loading branch information
Showing
3 changed files
with
175 additions
and
56 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 |
---|---|---|
@@ -1,37 +1,47 @@ | ||
<?php | ||
/** | ||
* Enforces WordPress function name format, based upon Squiz code. | ||
* Enforces WordPress function name format. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author John Godley <[email protected]> | ||
*/ | ||
|
||
/** | ||
* Enforces WordPress function name format. | ||
* Enforces WordPress function name and method name format, based upon Squiz code. | ||
* | ||
* @link https://make.wordpress.org/core/handbook/coding-standards/php/#naming-conventions | ||
* | ||
* Last synced with parent class July 2016 at commit 916b09a. | ||
* @link https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Squiz/Sniffs/NamingConventions/ValidFunctionNameSniff.php | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author John Godley <[email protected]> | ||
*/ | ||
class WordPress_Sniffs_NamingConventions_ValidFunctionNameSniff extends PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff { | ||
|
||
private $_magicMethods = array( | ||
'construct', | ||
'destruct', | ||
'call', | ||
'callStatic', | ||
'get', | ||
'set', | ||
'isset', | ||
'unset', | ||
'sleep', | ||
'wakeup', | ||
'toString', | ||
'set_state', | ||
'clone', | ||
'invoke', | ||
'debugInfo', | ||
/** | ||
* Additional double underscore prefixed methods specific to certain PHP native extensions. | ||
* | ||
* Currently only handles the SoapClient Extension. | ||
* | ||
* @link http://php.net/manual/en/class.soapclient.php | ||
* | ||
* @var array <string method name> => <string class name> | ||
*/ | ||
private $methodsDoubleUnderscore = array( | ||
'doRequest' => 'SoapClient', | ||
'getFunctions' => 'SoapClient', | ||
'getLastRequest' => 'SoapClient', | ||
'getLastRequestHeaders' => 'SoapClient', | ||
'getLastResponse' => 'SoapClient', | ||
'getLastResponseHeaders' => 'SoapClient', | ||
'getTypes' => 'SoapClient', | ||
'setCookie' => 'SoapClient', | ||
'setLocation' => 'SoapClient', | ||
'setSoapHeaders' => 'SoapClient', | ||
'soapCall' => 'SoapClient', | ||
); | ||
|
||
/** | ||
|
@@ -46,13 +56,41 @@ class WordPress_Sniffs_NamingConventions_ValidFunctionNameSniff extends PEAR_Sni | |
protected function processTokenOutsideScope( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { | ||
$functionName = $phpcsFile->getDeclarationName( $stackPtr ); | ||
|
||
if ( ! isset( $functionName ) ) { | ||
// Ignore closures. | ||
return; | ||
} | ||
|
||
if ( '' === ltrim( $functionName, '_' ) ) { | ||
// Ignore special functions. | ||
return; | ||
} | ||
|
||
// Is this a magic function ? I.e., it is prefixed with "__" ? | ||
// Outside class scope this basically just means __autoload(). | ||
if ( 0 === strpos( $functionName, '__' ) ) { | ||
$magicPart = strtolower( substr( $functionName, 2 ) ); | ||
if ( ! isset( $this->magicFunctions[ $magicPart ] ) ) { | ||
$error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; | ||
$errorData = array( $functionName ); | ||
$phpcsFile->addError( $error, $stackPtr, 'FunctionDoubleUnderscore', $errorData ); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if ( strtolower( $functionName ) !== $functionName ) { | ||
$suggested = preg_replace( '/([A-Z])/', '_$1', $functionName ); | ||
$suggested = strtolower( $suggested ); | ||
$suggested = str_replace( '__', '_', $suggested ); | ||
|
||
$error = "Function name \"$functionName\" is in camel caps format, try '{$suggested}'"; | ||
$phpcsFile->addError( $error, $stackPtr, 'FunctionNameInvalid' ); | ||
$suggested = trim( $suggested, '_' ); | ||
|
||
$error = 'Function name "%s" is not in snake case format, try "%s"'; | ||
$errorData = array( | ||
$functionName, | ||
$suggested, | ||
); | ||
$phpcsFile->addError( $error, $stackPtr, 'FunctionNameInvalid', $errorData ); | ||
} | ||
|
||
} // end processTokenOutsideScope() | ||
|
@@ -68,17 +106,17 @@ protected function processTokenOutsideScope( PHP_CodeSniffer_File $phpcsFile, $s | |
* @return void | ||
*/ | ||
protected function processTokenWithinScope( PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope ) { | ||
$className = $phpcsFile->getDeclarationName( $currScope ); | ||
$methodName = $phpcsFile->getDeclarationName( $stackPtr ); | ||
|
||
// Is this a magic method. IE. is prefixed with "__". | ||
if ( 0 === strpos( $methodName, '__' ) ) { | ||
$magicPart = substr( $methodName, 2 ); | ||
if ( false === in_array( $magicPart, $this->_magicMethods, true ) ) { | ||
$error = "Method name \"$className::$methodName\" is invalid; only PHP magic methods should be prefixed with a double underscore"; | ||
$phpcsFile->addError( $error, $stackPtr, 'MethodDoubleUnderscore' ); | ||
} | ||
if ( ! isset( $methodName ) ) { | ||
// Ignore closures. | ||
return; | ||
} | ||
|
||
$className = $phpcsFile->getDeclarationName( $currScope ); | ||
|
||
// Ignore special functions. | ||
if ( '' === ltrim( $methodName, '_' ) ) { | ||
return; | ||
} | ||
|
||
|
@@ -92,38 +130,40 @@ protected function processTokenWithinScope( PHP_CodeSniffer_File $phpcsFile, $st | |
return; | ||
} | ||
|
||
// If this is a child class, it may have to use camelCase. | ||
if ( $phpcsFile->findExtendedClassName( $currScope ) || $this->findImplementedInterfaceName( $currScope, $phpcsFile ) ) { | ||
$extended = $phpcsFile->findExtendedClassName( $currScope ); | ||
$interface = $this->findImplementedInterfaceName( $currScope, $phpcsFile ); | ||
|
||
// If this is a child class or interface implementation, it may have to use camelCase or double underscores. | ||
if ( $extended || $interface ) { | ||
return; | ||
} | ||
|
||
$methodProps = $phpcsFile->getMethodProperties( $stackPtr ); | ||
$scope = $methodProps['scope']; | ||
$scopeSpecified = $methodProps['scope_specified']; | ||
|
||
if ( 'private' === $methodProps['scope'] ) { | ||
$isPublic = false; | ||
} else { | ||
$isPublic = true; | ||
} | ||
// Is this a magic method ? I.e. is it prefixed with "__" ? | ||
if ( 0 === strpos( $methodName, '__' ) ) { | ||
$magicPart = strtolower( substr( $methodName, 2 ) ); | ||
if ( ! isset( $this->magicMethods[ $magicPart ] ) && ! isset( $this->methodsDoubleUnderscore[ $magicPart ] ) ) { | ||
$error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; | ||
$errorData = array( $className . '::' . $methodName ); | ||
$phpcsFile->addError( $error, $stackPtr, 'MethodDoubleUnderscore', $errorData ); | ||
} | ||
|
||
// If the scope was specified on the method, then the method must be | ||
// camel caps and an underscore should be checked for. If it wasn't | ||
// specified, treat it like a public method and remove the underscore | ||
// prefix if there is one because we can't determine if it is private or | ||
// public. | ||
$testMethodName = $methodName; | ||
if ( false === $scopeSpecified && '_' === $methodName{0} ) { | ||
$testMethodName = substr( $methodName, 1 ); | ||
return; | ||
} | ||
|
||
if ( strtolower( $testMethodName ) !== $testMethodName ) { | ||
// Check for all lowercase. | ||
if ( strtolower( $methodName ) !== $methodName ) { | ||
$suggested = preg_replace( '/([A-Z])/', '_$1', $methodName ); | ||
$suggested = strtolower( $suggested ); | ||
$suggested = str_replace( '__', '_', $suggested ); | ||
|
||
$error = "Function name \"$methodName\" is in camel caps format, try '{$suggested}'"; | ||
$phpcsFile->addError( $error, $stackPtr, 'FunctionNameInvalid' ); | ||
$suggested = trim( $suggested, '_' ); | ||
|
||
$error = 'Method name "%s" in class %s is not in snake case format, try "%s"'; | ||
$errorData = array( | ||
$methodName, | ||
$className, | ||
$suggested, | ||
); | ||
$phpcsFile->addError( $error, $stackPtr, 'MethodNameInvalid', $errorData ); | ||
} | ||
|
||
} // end processTokenWithinScope() | ||
|
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