diff --git a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php index 78a715061f..f36641173d 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php @@ -25,6 +25,13 @@ class FunctionCommentSniff extends PEARFunctionCommentSniff */ public $skipIfInheritdoc = false; + /** + * Whether to use short forms of type keywords. + * + * @var boolean + */ + public $useShortTypes = false; + /** * The current PHP version. * @@ -88,7 +95,7 @@ protected function processReturn(File $phpcsFile, int $stackPtr, int $commentSta $typeNames = explode('|', $returnType); $suggestedNames = []; foreach ($typeNames as $typeName) { - $suggestedName = Common::suggestType($typeName); + $suggestedName = Common::suggestType($typeName, $this->useShortTypes); if (in_array($suggestedName, $suggestedNames, true) === false) { $suggestedNames[] = $suggestedName; } @@ -419,7 +426,7 @@ protected function processParams(File $phpcsFile, int $stackPtr, int $commentSta $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 eb9dba5022..6e6a760f5d 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php @@ -19,6 +19,13 @@ class VariableCommentSniff extends AbstractVariableSniff { + /** + * Whether to use short forms of type keywords. + * + * @var boolean + */ + public $useShortTypes = false; + /** * Only listen to variables within OO scopes. @@ -149,7 +156,7 @@ public function processMemberVar(File $phpcsFile, int $stackPtr) $typeNames = explode('|', $varType); $suggestedNames = []; foreach ($typeNames as $typeName) { - $suggestedName = Common::suggestType($typeName); + $suggestedName = Common::suggestType($typeName, $this->useShortTypes); if (in_array($suggestedName, $suggestedNames, true) === false) { $suggestedNames[] = $suggestedName; } diff --git a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.3.inc b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.3.inc new file mode 100644 index 0000000000..628aba06b6 --- /dev/null +++ b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.3.inc @@ -0,0 +1,33 @@ + 1, ]; + case 'FunctionCommentUnitTest.3.inc': + return [ + 8 => 1, + 10 => 1, + 25 => 1, + 27 => 1, + ]; + default: return []; } diff --git a/src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.inc b/src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.1.inc similarity index 100% rename from src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.inc rename to src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.1.inc diff --git a/src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.inc.fixed b/src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.1.inc.fixed similarity index 100% rename from src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.inc.fixed rename to src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.1.inc.fixed diff --git a/src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.2.inc b/src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.2.inc new file mode 100644 index 0000000000..b32cbc3d76 --- /dev/null +++ b/src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.2.inc @@ -0,0 +1,51 @@ + */ - public function getErrorList() + public function getErrorList($testFile = '') { - return [ - 21 => 1, - 24 => 1, - 56 => 1, - 64 => 1, - 73 => 1, - 84 => 1, - 130 => 1, - 136 => 1, - 144 => 1, - 152 => 1, - 160 => 1, - 168 => 1, - 176 => 1, - 184 => 1, - 192 => 1, - 200 => 1, - 208 => 1, - 216 => 1, - 224 => 1, - 232 => 1, - 240 => 1, - 248 => 1, - 256 => 1, - 264 => 1, - 272 => 1, - 280 => 1, - 290 => 1, - 294 => 1, - 311 => 1, - 336 => 1, - 361 => 1, - 364 => 1, - 399 => 1, - 403 => 1, - 457 => 1, - ]; + switch ($testFile) { + case 'VariableCommentUnitTest.1.inc': + return [ + 21 => 1, + 24 => 1, + 56 => 1, + 64 => 1, + 73 => 1, + 84 => 1, + 130 => 1, + 136 => 1, + 144 => 1, + 152 => 1, + 160 => 1, + 168 => 1, + 176 => 1, + 184 => 1, + 192 => 1, + 200 => 1, + 208 => 1, + 216 => 1, + 224 => 1, + 232 => 1, + 240 => 1, + 248 => 1, + 256 => 1, + 264 => 1, + 272 => 1, + 280 => 1, + 290 => 1, + 294 => 1, + 311 => 1, + 336 => 1, + 361 => 1, + 364 => 1, + 399 => 1, + 403 => 1, + 457 => 1, + ]; + + case 'VariableCommentUnitTest.2.inc': + return [ + 8 => 1, + 18 => 1, + 38 => 1, + 48 => 1, + ]; + + default: + return []; + } } @@ -77,15 +93,24 @@ public function getErrorList() * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * + * @param string $testFile The name of the test file being tested. + * * @return array */ - public function getWarningList() + public function getWarningList($testFile = '') { - return [ - 93 => 1, - 494 => 1, - 495 => 1, - 496 => 1, - ]; + switch ($testFile) { + case 'VariableCommentUnitTest.1.inc': + + return [ + 93 => 1, + 494 => 1, + 495 => 1, + 496 => 1, + ]; + + default: + return []; + } } } diff --git a/src/Util/Common.php b/src/Util/Common.php index 06c9785d42..8f8e9812f1 100644 --- a/src/Util/Common.php +++ b/src/Util/Common.php @@ -33,6 +33,23 @@ class Common 'callable' => 'callable', ]; + /** + * An array of short variable types for param/var we will check. + * + * @var array + */ + public const ALLOWED_SHORT_TYPES = [ + 'array' => 'array', + 'bool' => 'bool', + 'float' => 'float', + 'int' => 'int', + 'mixed' => 'mixed', + 'object' => 'object', + 'string' => 'string', + 'resource' => 'resource', + 'callable' => 'callable', + ]; + /** * An array of variable types for param/var we will check. * @@ -455,31 +472,48 @@ public static function isUnderscoreName(string $name) * 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(string $varType) + public static function suggestType(string $varType, bool $useShortTypes = false) { if ($varType === '') { return ''; } - if (isset(self::ALLOWED_TYPES[$varType]) === true) { + if ($useShortTypes === true) { + $allowedTypes = self::ALLOWED_SHORT_TYPES; + } else { + $allowedTypes = self::ALLOWED_TYPES; + } + + if (isset($allowedTypes[$varType]) === 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'; @@ -501,8 +535,8 @@ public static function suggestType(string $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; } @@ -511,7 +545,7 @@ public static function suggestType(string $varType) } else { return 'array'; } - } elseif (isset(self::ALLOWED_TYPES[$lowerVarType]) === true) { + } elseif (isset($allowedTypes[$lowerVarType]) === true) { // A valid type, but not lower cased. return $lowerVarType; } else { diff --git a/tests/Core/Util/Common/SuggestTypeTest.php b/tests/Core/Util/Common/SuggestTypeTest.php index bb3cda50a0..fdba30cda4 100644 --- a/tests/Core/Util/Common/SuggestTypeTest.php +++ b/tests/Core/Util/Common/SuggestTypeTest.php @@ -213,4 +213,34 @@ public static function dataSuggestTypeOther() ], ]; } + + + /** + * Test the suggestType() for suggesting long types. + * + * @return void + */ + public function testSuggestLongType() + { + $this->assertSame('boolean', Common::suggestType('bool', false)); + $this->assertSame('boolean', Common::suggestType('boolean', false)); + + $this->assertSame('integer', Common::suggestType('int', false)); + $this->assertSame('integer', Common::suggestType('integer', false)); + } + + + /** + * Test the suggestType() for suggesting short types. + * + * @return void + */ + public function testSuggestShortType() + { + $this->assertSame('bool', Common::suggestType('bool', true)); + $this->assertSame('bool', Common::suggestType('boolean', true)); + + $this->assertSame('int', Common::suggestType('int', true)); + $this->assertSame('int', Common::suggestType('integer', true)); + } }