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

feat(MultiLineTrailingComma): Add sniff to check trailing comma on multi-line function declarations #225

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* \Drupal\Sniffs\Functions\MultiLineTrailingCommaSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/

namespace Drupal\Sniffs\Functions;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

/**
* Multi-line function declarations need to have a trailing comma on the last
* parameter.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class MultiLineTrailingCommaSniff implements Sniff
{


/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register()
{
if (version_compare(PHP_VERSION, '8.0.0') < 0) {
return [];
}

return [
T_FUNCTION,
T_CLOSURE,
];

}//end register()


/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

$function = $tokens[$stackPtr];
if ($tokens[$function['parenthesis_opener']]['line'] === $tokens[$function['parenthesis_closer']]['line']) {
return;
}

$lastTrailingComma = $phpcsFile->findPrevious(
Tokens::$emptyTokens,
($function['parenthesis_closer'] - 1),
$function['parenthesis_opener'],
true
);
if ($tokens[$lastTrailingComma]['code'] !== T_COMMA) {
$error = 'Multi-line function declarations must have a trailing comma after the last parameter';
$fix = $phpcsFile->addFixableError($error, $lastTrailingComma, 'MissingTrailingComma');
if ($fix === true) {
$phpcsFile->fixer->addContent($lastTrailingComma, ',');
}
}

}//end process()


}//end class
23 changes: 23 additions & 0 deletions tests/Drupal/Functions/MultiLineTrailingCommaUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* Test.
*/
function missing_trailing_comma(
$a,
$b
) {

}

$foo = 1;
$bar = 2;
$x = function(
$a,
$b
) use (
$foo,
$bar
) {

};
64 changes: 64 additions & 0 deletions tests/Drupal/Functions/MultiLineTrailingCommaUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Drupal\Test\Functions;

use Drupal\Test\CoderSniffUnitTest;

class MultiLineTrailingCommaUnitTest extends CoderSniffUnitTest
{


/**
* Returns the lines where errors should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
protected function getErrorList(string $testFile): array
{
return [
8 => 1,
17 => 1,
];

}//end getErrorList()


/**
* Returns the lines where warnings should occur.
*
* 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 file being tested.
*
* @return array<int, int>
*/
protected function getWarningList(string $testFile): array
{
return [];

}//end getWarningList()


/**
* Skip this test on PHP versions lower than 8 because the syntax is not allowed there.
*
* @return bool
*/
protected function shouldSkipTest()
{
if (version_compare(PHP_VERSION, '8.0.0') < 0) {
return true;
}

return false;

}//end shouldSkipTest()


}//end class
23 changes: 23 additions & 0 deletions tests/Drupal/Functions/MultiLineTrailingCommaUnitTestinc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* Test.
*/
function missing_trailing_comma(
$a,
$b,
) {

}

$foo = 1;
$bar = 2;
$x = function(
$a,
$b
) use (
$foo,
$bar
) {

};
18 changes: 17 additions & 1 deletion tests/Drupal/bad/BadUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ protected function getErrorList(string $testFile): array
836 => 1,
838 => 1,
849 => 2,
860 => 1,
860 => 2,
864 => 2,
];
}//end switch
Expand Down Expand Up @@ -480,4 +480,20 @@ protected function checkAllSniffCodes()
}//end checkAllSniffCodes()


/**
* Skip this test on PHP versions lower than 8 because of MultiLineTrailingCommaSniff.
*
* @return bool
*/
protected function shouldSkipTest()
{
if (version_compare(PHP_VERSION, '8.0.0') < 0) {
return true;
}

return false;

}//end shouldSkipTest()


}//end class
2 changes: 1 addition & 1 deletion tests/Drupal/bad/bad.php.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ class ScopeKeyword {
*/
function test29(
int $a,
string $b
string $b,
) {
echo "Hello";
}
2 changes: 1 addition & 1 deletion tests/Drupal/good/good.php
Original file line number Diff line number Diff line change
Expand Up @@ -1476,7 +1476,7 @@ function test18(
CacheTagsInvalidatorInterface $cache_invalidator,
ModuleHandlerInterface $module_handler,
EntityFieldManagerInterface $entity_field_manager,
EntityTypeBundleInfoInterface $entity_type_bundle_info
EntityTypeBundleInfoInterface $entity_type_bundle_info,
) {
return 0;
}
Expand Down
Loading