Skip to content

Commit

Permalink
Merge branch 'master' into 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Jan 12, 2016
2 parents 5c5d9e2 + 4966b54 commit 0ba9c39
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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
6 changes: 6 additions & 0 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,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 @@ -1228,6 +1229,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 @@ -1287,11 +1291,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
10 changes: 9 additions & 1 deletion src/Standards/PEAR/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
$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 @@ -272,6 +272,14 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
$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
23 changes: 23 additions & 0 deletions src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.inc
Original file line number Diff line number Diff line change
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) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public function getErrorList()
206 => 1,
234 => 1,
272 => 1,
313 => 1,
317 => 1,
);

}//end getErrorList()
Expand Down
10 changes: 9 additions & 1 deletion src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
$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 @@ -311,6 +311,14 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
$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
23 changes: 23 additions & 0 deletions src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc
Original file line number Diff line number Diff line change
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) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,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 @@ -117,6 +119,8 @@ public function getErrorList()
$errors[627] = 1;
} else {
$errors[729] = 4;
$errors[740] = 2;
$errors[752] = 2;
}

return $errors;
Expand Down
8 changes: 8 additions & 0 deletions tests/Core/File/GetMethodParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function testPassByReference()
$expected[0] = array(
'name' => '$var',
'pass_by_reference' => true,
'variable_length' => false,
'type_hint' => '',
);

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

Expand Down Expand Up @@ -127,12 +129,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 @@ -162,6 +166,7 @@ public function testVariable()
$expected[0] = array(
'name' => '$var',
'pass_by_reference' => false,
'variable_length' => false,
'type_hint' => '',
);

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

Expand Down Expand Up @@ -222,12 +228,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

0 comments on commit 0ba9c39

Please sign in to comment.