Skip to content

Commit

Permalink
PEAR and Squiz FunctionComment sniffs now support variadic functions …
Browse files Browse the repository at this point in the history
…(request #841)
  • Loading branch information
gsherwood committed Jan 12, 2016
1 parent 5b220d5 commit 4966b54
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CodeSniffer/File.php
Expand Up @@ -2729,6 +2729,7 @@ public function getMethodParameters($stackPtr)
$defaultStart = null;
$paramCount = 0;
$passByReference = false;
$variableLength = false;
$typeHint = '';

for ($i = ($opener + 1); $i <= $closer; $i++) {
Expand Down Expand Up @@ -2757,6 +2758,9 @@ public function getMethodParameters($stackPtr)
case T_VARIABLE:
$currVar = $i;
break;
case T_ELLIPSIS:
$variableLength = true;
break;
case T_ARRAY_HINT:
case T_CALLABLE:
$typeHint = $this->_tokens[$i]['content'];
Expand Down Expand Up @@ -2816,11 +2820,13 @@ public function getMethodParameters($stackPtr)
}

$vars[$paramCount]['pass_by_reference'] = $passByReference;
$vars[$paramCount]['variable_length'] = $variableLength;
$vars[$paramCount]['type_hint'] = $typeHint;

// Reset the vars, as we are about to process the next parameter.
$defaultStart = null;
$passByReference = false;
$variableLength = false;
$typeHint = '';

$paramCount++;
Expand Down
Expand Up @@ -225,7 +225,7 @@ protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
$comment = '';
if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) {
$matches = array();
preg_match('/([^$&]+)(?:((?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches);
preg_match('/([^$&.]+)(?:((?:\.\.\.)?(?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches);

$typeLen = strlen($matches[1]);
$type = trim($matches[1]);
Expand Down Expand Up @@ -284,6 +284,14 @@ protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
$realParams = $phpcsFile->getMethodParameters($stackPtr);
$foundParams = array();

// We want to use ... for all variable length arguments, so added
// this prefix to the variable name so comparisons are easier.
foreach ($realParams as $pos => $param) {
if ($param['variable_length'] === true) {
$realParams[$pos]['name'] = '...'.$realParams[$pos]['name'];
}
}

foreach ($params as $pos => $param) {
if ($param['var'] === '') {
continue;
Expand Down
Expand Up @@ -297,3 +297,26 @@ class Baz {
public function completeStep($status, array $array = [Class1::class, 'test'], $note = '') {
echo 'foo';
}

/**
* Variadic function.
*
* @param string $name1 Comment.
* @param string ...$name2 Comment.
*
* @return void
*/
public function myFunction(string $name1, string ...$name2) {
}


/**
* Variadic function.
*
* @param string $name1 Comment.
* @param string $name2 Comment.
*
* @return void
*/
public function myFunction(string $name1, string ...$name2) {
}
Expand Up @@ -69,6 +69,8 @@ public function getErrorList()
206 => 1,
234 => 1,
272 => 1,
313 => 1,
317 => 1,
);

}//end getErrorList()
Expand Down
Expand Up @@ -251,7 +251,7 @@ protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
$commentLines = array();
if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) {
$matches = array();
preg_match('/([^$&]+)(?:((?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches);
preg_match('/([^$&.]+)(?:((?:\.\.\.)?(?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches);

$typeLen = strlen($matches[1]);
$type = trim($matches[1]);
Expand Down Expand Up @@ -327,6 +327,14 @@ protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
$realParams = $phpcsFile->getMethodParameters($stackPtr);
$foundParams = array();

// We want to use ... for all variable length arguments, so added
// this prefix to the variable name so comparisons are easier.
foreach ($realParams as $pos => $param) {
if ($param['variable_length'] === true) {
$realParams[$pos]['name'] = '...'.$realParams[$pos]['name'];
}
}

foreach ($params as $pos => $param) {
// If the type is empty, the whole line is empty.
if ($param['type'] === '') {
Expand Down
Expand Up @@ -728,3 +728,26 @@ public function callableCallback(callable $cb) {
*/
public function myFunction (string $name1, int $name2, float $name3, bool $name4) {
}

/**
* Variadic function.
*
* @param string $name1 Comment.
* @param string ...$name2 Comment.
*
* @return void
*/
public function myFunction(string $name1, string ...$name2) {
}


/**
* Variadic function.
*
* @param string $name1 Comment.
* @param string $name2 Comment.
*
* @return void
*/
public function myFunction(string $name1, string ...$name2) {
}
Expand Up @@ -109,6 +109,8 @@ public function getErrorList()
548 => 1,
641 => 1,
669 => 1,
744 => 1,
748 => 1,
);

// The yield tests will only work in PHP versions where yield exists and
Expand All @@ -131,6 +133,8 @@ public function getErrorList()
$errors[627] = 1;
} else {
$errors[729] = 4;
$errors[740] = 2;
$errors[752] = 2;
}

return $errors;
Expand Down
1 change: 1 addition & 0 deletions package.xml
Expand Up @@ -34,6 +34,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Squiz ComparisonOperatorUsageSniff no longer hangs on JS FOR loops that don't use semicolons
- PHP_CodesSniffer now includes the composer autoload.php file, if there is one
-- Thanks to Klaus Purer for the patch
- PEAR and Squiz FunctionComment sniffs now support variadic functions (request #841)
- Fixed bug #622 : Wrong detection of Squiz.CSS.DuplicateStyleDefinition with media queries
- Fixed bug #752 : The missing exception error is reported in first found DocBlock
- Fixed bug #794 : PSR2 MultiLineFunctionDeclaration forbids comments after opening parenthesis of a multiline call
Expand Down
9 changes: 8 additions & 1 deletion tests/Core/File/GetMethodParametersTest.php
Expand Up @@ -85,6 +85,7 @@ public function testPassByReference()
$expected[0] = array(
'name' => '$var',
'pass_by_reference' => true,
'variable_length' => false,
'type_hint' => '',
);

Expand Down Expand Up @@ -114,6 +115,7 @@ public function testArrayHint()
$expected[0] = array(
'name' => '$var',
'pass_by_reference' => false,
'variable_length' => false,
'type_hint' => 'array',
);

Expand Down Expand Up @@ -143,12 +145,14 @@ public function testTypeHint()
$expected[0] = array(
'name' => '$var1',
'pass_by_reference' => false,
'variable_length' => false,
'type_hint' => 'foo',
);

$expected[1] = array(
'name' => '$var2',
'pass_by_reference' => false,
'variable_length' => false,
'type_hint' => 'bar',
);

Expand Down Expand Up @@ -178,6 +182,7 @@ public function testVariable()
$expected[0] = array(
'name' => '$var',
'pass_by_reference' => false,
'variable_length' => false,
'type_hint' => '',
);

Expand Down Expand Up @@ -208,6 +213,7 @@ public function testSingleDefaultValue()
'name' => '$var1',
'default' => 'self::CONSTANT',
'pass_by_reference' => false,
'variable_length' => false,
'type_hint' => '',
);

Expand Down Expand Up @@ -238,12 +244,14 @@ public function testDefaultValues()
'name' => '$var1',
'default' => '1',
'pass_by_reference' => false,
'variable_length' => false,
'type_hint' => '',
);
$expected[1] = array(
'name' => '$var2',
'default' => "'value'",
'pass_by_reference' => false,
'variable_length' => false,
'type_hint' => '',
);

Expand Down Expand Up @@ -274,4 +282,3 @@ public function testDefaultValues()
// @codingStandardsIgnoreEnd

?>

0 comments on commit 4966b54

Please sign in to comment.