-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support to use short forms of type keywords #3139
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -16,6 +16,13 @@ | |||
class VariableCommentSniff extends AbstractVariableSniff | ||||
{ | ||||
|
||||
/** | ||||
* Whether to use short forms of type keywords. | ||||
* | ||||
* @var boolean | ||||
*/ | ||||
public $useShortTypes = false; | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this empty line.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't request changes which are in violation with the coding standard used by the library. |
||||
|
||||
/** | ||||
* 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; | ||||
} | ||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -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', | ||||
]; | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this empty line.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't request changes which are in violation with the coding standard used by the library. |
||||
|
||||
/** | ||||
* 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 { | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this default to true due to PSR-12?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Problem is that this might be considered as a breaking change. I'm not sure how this was done in the past, but maybe it should remain
false
in PHPCS 3.x and changed totrue
in 4.x.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand. In the mean time it's easy to just change that property in the config file.