diff --git a/CakePHP/Sniffs/NamingConventions/ValidFunctionNameSniff.php b/CakePHP/Sniffs/NamingConventions/ValidFunctionNameSniff.php index 65037bf..a3ed2f6 100644 --- a/CakePHP/Sniffs/NamingConventions/ValidFunctionNameSniff.php +++ b/CakePHP/Sniffs/NamingConventions/ValidFunctionNameSniff.php @@ -27,9 +27,9 @@ class ValidFunctionNameSniff extends AbstractScopeSniff /** * A list of all PHP magic methods. * - * @var array + * @var array */ - protected array $_magicMethods = [ + protected array $magicMethods = [ 'construct', 'destruct', 'call', @@ -54,7 +54,7 @@ class ValidFunctionNameSniff extends AbstractScopeSniff */ public function __construct() { - parent::__construct([T_CLASS, T_INTERFACE, T_TRAIT], [T_FUNCTION], true); + parent::__construct([T_CLASS, T_INTERFACE, T_TRAIT, T_ENUM], [T_FUNCTION], true); } /** @@ -71,7 +71,7 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop $errorData = [$className . '::' . $methodName]; // Ignore magic methods - if (preg_match('/^__(' . implode('|', $this->_magicMethods) . ')$/', $methodName)) { + if (preg_match('/^__(' . implode('|', $this->magicMethods) . ')$/', $methodName)) { return; } @@ -89,6 +89,17 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop return; } + + // Check non-public methods for underscore prefix + if ($isPublic === false && $methodName[0] === '_') { + // Allow CakePHP Entity accessor/mutator pattern: _getField(), _setField() + if (preg_match('/^_(get|set)[A-Z]/', $methodName)) { + return; + } + + $error = 'Non-public method name "%s" should not be prefixed with underscore'; + $phpcsFile->addError($error, $stackPtr, 'ProtectedWithUnderscore', $errorData); + } } /** diff --git a/CakePHP/Tests/NamingConventions/ValidFunctionNameUnitTest.inc b/CakePHP/Tests/NamingConventions/ValidFunctionNameUnitTest.inc index e4bc565..837502c 100644 --- a/CakePHP/Tests/NamingConventions/ValidFunctionNameUnitTest.inc +++ b/CakePHP/Tests/NamingConventions/ValidFunctionNameUnitTest.inc @@ -29,7 +29,7 @@ class FunctionNames protected function _someFunc() { - // code here + // code here - error: underscore prefix not allowed } protected function noUnderscorePrefix() @@ -40,6 +40,22 @@ class FunctionNames }; } + // Entity accessor/mutator patterns - should be allowed + protected function _getName() + { + return $this->name; + } + + protected function _setName($name) + { + $this->name = $name; + } + + protected function _getFullName() + { + return $this->first_name . ' ' . $this->last_name; + } + public function __call($name, $arguments) { } diff --git a/CakePHP/Tests/NamingConventions/ValidFunctionNameUnitTest.php b/CakePHP/Tests/NamingConventions/ValidFunctionNameUnitTest.php index 2816783..9ac89c5 100644 --- a/CakePHP/Tests/NamingConventions/ValidFunctionNameUnitTest.php +++ b/CakePHP/Tests/NamingConventions/ValidFunctionNameUnitTest.php @@ -9,19 +9,21 @@ class ValidFunctionNameUnitTest extends AbstractSniffTestCase /** * @inheritDoc */ - public function getErrorList() + public function getErrorList(): array { return [ 6 => 1, - 87 => 1, - 96 => 1, + 30 => 1, + 103 => 1, + 112 => 1, + 136 => 1, ]; } /** * @inheritDoc */ - public function getWarningList() + public function getWarningList(): array { return []; } diff --git a/CakePHP/ruleset.xml b/CakePHP/ruleset.xml index f41ec35..13225b5 100644 --- a/CakePHP/ruleset.xml +++ b/CakePHP/ruleset.xml @@ -18,10 +18,9 @@ - @@ -256,6 +255,10 @@ + + + +