diff --git a/West/Sniffs/Commenting/FunctionCommentSniff.php b/West/Sniffs/Commenting/FunctionCommentSniff.php index cf8ed68..932c3d1 100644 --- a/West/Sniffs/Commenting/FunctionCommentSniff.php +++ b/West/Sniffs/Commenting/FunctionCommentSniff.php @@ -13,7 +13,6 @@ use PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\FunctionCommentSniff as SquizFunctionCommentSniff; use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Config; -use West\CodingStandard\Util\Common; /** * Formatting tests for functions (including methods). @@ -50,6 +49,23 @@ class FunctionCommentSniff extends SquizFunctionCommentSniff */ private $phpVersion = null; + /** + * An array of variable types for param/var we will check. + * + * @var string[] + */ + private static $allowedTypes = [ + 'array', + 'bool', + 'float', + 'int', + 'mixed', + 'object', + 'string', + 'resource', + 'callable', + ]; + /** * {@inheritdoc} */ @@ -106,7 +122,7 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart) $typeNames = explode('|', $returnType); $suggestedNames = []; foreach ($typeNames as $i => $typeName) { - $suggestedName = Common::suggestType($typeName); + $suggestedName = self::suggestType($typeName); if (in_array($suggestedName, $suggestedNames) === false) { $suggestedNames[] = $suggestedName; } @@ -390,7 +406,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart) $typeName = substr($typeName, 1); } - $suggestedName = Common::suggestType($typeName); + $suggestedName = self::suggestType($typeName); $suggestedTypeNames[] = $suggestedName; if (count($typeNames) > 1) { @@ -405,7 +421,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart) $suggestedTypeHint = 'callable'; } else if (strpos($suggestedName, 'callback') !== false) { $suggestedTypeHint = 'callable'; - } else if (in_array($suggestedName, Common::$allowedTypes) === false) { + } else if (in_array($suggestedName, self::$allowedTypes) === false) { $suggestedTypeHint = $suggestedName; } @@ -550,4 +566,76 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart) $phpcsFile->addError($error, $commentStart, 'MissingParamTag', $data); } } + + /** + * Returns a valid variable type for param/var tag. + * + * If type is not one of the standard type, it must be a custom type. + * Returns the correct type name suggestion if type name is invalid. + * + * @param string $varType The variable type to process. + * + * @return string + */ + private static function suggestType($varType) + { + if ($varType === '') { + return ''; + } + + if (in_array($varType, self::$allowedTypes) === true) { + return $varType; + } else { + $lowerVarType = strtolower($varType); + switch ($lowerVarType) { + case 'bool': + case 'boolean': + return 'bool'; + case 'double': + case 'real': + case 'float': + return 'float'; + case 'int': + case 'integer': + return 'int'; + case 'array()': + case 'array': + return 'array'; + } + + if (strpos($lowerVarType, 'array(') !== false) { + // Valid array declaration: + // array, array(type), array(type1 => type2). + $matches = []; + $pattern = '/^array\(\s*([^\s^=^>]*)(\s*=>\s*(.*))?\s*\)/i'; + if (preg_match($pattern, $varType, $matches) !== 0) { + $type1 = ''; + if (isset($matches[1]) === true) { + $type1 = $matches[1]; + } + + $type2 = ''; + if (isset($matches[3]) === true) { + $type2 = $matches[3]; + } + + $type1 = self::suggestType($type1); + $type2 = self::suggestType($type2); + if ($type2 !== '') { + $type2 = ' => ' . $type2; + } + + return "array($type1$type2)"; + } else { + return 'array'; + } + } elseif (in_array($lowerVarType, self::$allowedTypes) === true) { + // A valid type, but not lower cased. + return $lowerVarType; + } else { + // Must be a custom type name. + return $varType; + } + } + } } diff --git a/West/Util/Common.php b/West/Util/Common.php deleted file mode 100644 index 2e0d126..0000000 --- a/West/Util/Common.php +++ /dev/null @@ -1,102 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace West\CodingStandard\Util; - -use PHP_CodeSniffer\Util\Common as CodeSnifferCommon; - -/** - * Common utilities. - * - * Update the CodeSniffer common utilities to prefer 'int' and 'bool' - * over 'integer' and 'boolean'. - */ -class Common extends CodeSnifferCommon -{ - /** - * @inheritdoc - */ - public static $allowedTypes = [ - 'array', - 'bool', - 'float', - 'int', - 'mixed', - 'object', - 'string', - 'resource', - 'callable', - ]; - - /** - * @inheritdoc - */ - public static function suggestType($varType) - { - if ($varType === '') { - return ''; - } - - if (in_array($varType, self::$allowedTypes) === true) { - return $varType; - } else { - $lowerVarType = strtolower($varType); - switch ($lowerVarType) { - case 'bool': - case 'boolean': - return 'bool'; - case 'double': - case 'real': - case 'float': - return 'float'; - case 'int': - case 'integer': - return 'int'; - case 'array()': - case 'array': - return 'array'; - } - - if (strpos($lowerVarType, 'array(') !== false) { - // Valid array declaration: - // array, array(type), array(type1 => type2). - $matches = []; - $pattern = '/^array\(\s*([^\s^=^>]*)(\s*=>\s*(.*))?\s*\)/i'; - if (preg_match($pattern, $varType, $matches) !== 0) { - $type1 = ''; - if (isset($matches[1]) === true) { - $type1 = $matches[1]; - } - - $type2 = ''; - if (isset($matches[3]) === true) { - $type2 = $matches[3]; - } - - $type1 = self::suggestType($type1); - $type2 = self::suggestType($type2); - if ($type2 !== '') { - $type2 = ' => ' . $type2; - } - - return "array($type1$type2)"; - } else { - return 'array'; - } - } elseif (in_array($lowerVarType, self::$allowedTypes) === true) { - // A valid type, but not lower cased. - return $lowerVarType; - } else { - // Must be a custom type name. - return $varType; - } - } - } -}