diff --git a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php index 38fc4d0f94..d360a26cb1 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php @@ -24,6 +24,13 @@ class FunctionCommentSniff extends PEARFunctionCommentSniff */ private $phpVersion = null; + /** + * Whether to use short forms of type keywords. + * + * @var boolean + */ + public $useShortTypes = false; + /** * Process the return comment of this function comment. @@ -77,7 +84,7 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart) $typeNames = explode('|', $returnType); $suggestedNames = []; foreach ($typeNames as $i => $typeName) { - $suggestedName = Common::suggestType($typeName); + $suggestedName = Common::suggestType($typeName, $this->useShortTypes); if (in_array($suggestedName, $suggestedNames, true) === false) { $suggestedNames[] = $suggestedName; } @@ -382,7 +389,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart) $typeName = substr($typeName, 1); } - $suggestedName = Common::suggestType($typeName); + $suggestedName = Common::suggestType($typeName, $this->useShortTypes); $suggestedTypeNames[] = $suggestedName; if (count($typeNames) > 1) { diff --git a/src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php index 7b9fc933ad..d3ec785bc1 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php @@ -16,6 +16,13 @@ class VariableCommentSniff extends AbstractVariableSniff { + /** + * Whether to use short forms of type keywords. + * + * @var boolean + */ + public $useShortTypes = false; + /** * Called to process class member vars. @@ -113,7 +120,7 @@ public function processMemberVar(File $phpcsFile, $stackPtr) $typeNames = explode('|', $varType); $suggestedNames = []; foreach ($typeNames as $i => $typeName) { - $suggestedName = Common::suggestType($typeName); + $suggestedName = Common::suggestType($typeName, $this->useShortTypes); if (in_array($suggestedName, $suggestedNames, true) === false) { $suggestedNames[] = $suggestedName; } diff --git a/src/Util/Common.php b/src/Util/Common.php index f939fc76de..28e7b777af 100644 --- a/src/Util/Common.php +++ b/src/Util/Common.php @@ -29,6 +29,23 @@ class Common 'callable', ]; + /** + * An array of short variable types for param/var we will check. + * + * @var string[] + */ + public static $allowedShortTypes = [ + 'array', + 'bool', + 'float', + 'int', + 'mixed', + 'object', + 'string', + 'resource', + 'callable', + ]; + /** * Return TRUE if the path is a PHAR file. @@ -389,31 +406,48 @@ public static function isUnderscoreName($string) * If type is not one of the standard types, 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. + * @param string $varType The variable type to process. + * @param boolean $useShortTypes Whether to use short forms of type keywords. * * @return string */ - public static function suggestType($varType) + public static function suggestType($varType, $useShortTypes=false) { if ($varType === '') { return ''; } - if (in_array($varType, self::$allowedTypes, true) === true) { + if ($useShortTypes === true) { + $allowedTypes = self::$allowedShortTypes; + } else { + $allowedTypes = self::$allowedTypes; + } + + if (in_array($varType, $allowedTypes, true) === true) { return $varType; } else { $lowerVarType = strtolower($varType); switch ($lowerVarType) { case 'bool': case 'boolean': - return 'boolean'; + if ($useShortTypes === true) { + return 'bool'; + } else { + return 'boolean'; + } + case 'double': case 'real': case 'float': return 'float'; case 'int': case 'integer': - return 'integer'; + if ($useShortTypes === true) { + return 'int'; + } else { + return 'integer'; + } + case 'array()': case 'array': return 'array'; @@ -435,8 +469,8 @@ public static function suggestType($varType) $type2 = $matches[3]; } - $type1 = self::suggestType($type1); - $type2 = self::suggestType($type2); + $type1 = self::suggestType($type1, $useShortTypes); + $type2 = self::suggestType($type2, $useShortTypes); if ($type2 !== '') { $type2 = ' => '.$type2; } @@ -445,7 +479,7 @@ public static function suggestType($varType) } else { return 'array'; }//end if - } else if (in_array($lowerVarType, self::$allowedTypes, true) === true) { + } else if (in_array($lowerVarType, $allowedTypes, true) === true) { // A valid type, but not lower cased. return $lowerVarType; } else {