Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ class FunctionCommentSniff extends PEARFunctionCommentSniff
*/
private $phpVersion = null;

/**
* Whether to use short forms of type keywords.
*
* @var boolean
*/
public $useShortTypes = false;

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?

Copy link
Contributor Author

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?

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 to true in 4.x.

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.



/**
* Process the return comment of this function comment.
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
class VariableCommentSniff extends AbstractVariableSniff
{

/**
* Whether to use short forms of type keywords.
*
* @var boolean
*/
public $useShortTypes = false;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this empty line.

Suggested change

Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Expand Down Expand Up @@ -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;
}
Expand Down
50 changes: 42 additions & 8 deletions src/Util/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this empty line.

Suggested change

Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Expand Down Expand Up @@ -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';
Expand All @@ -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;
}
Expand All @@ -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 {
Expand Down