From b73448467cd8956e2e7eca6394f758e0b463320f Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sun, 5 May 2024 13:33:18 +0200 Subject: [PATCH] Add test case for multi line function declaration --- .../WhiteSpace/ScopeClosingBraceSniff.php | 8 ++-- .../WhiteSpace/ScopeClosingBraceUnitTest.inc | 38 ++++++++++++++++++ .../ScopeClosingBraceUnitTest.inc.fixed | 39 +++++++++++++++++++ 3 files changed, 82 insertions(+), 3 deletions(-) diff --git a/coder_sniffer/Drupal/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php b/coder_sniffer/Drupal/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php index c6d5c99d..c3a61c5f 100644 --- a/coder_sniffer/Drupal/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php +++ b/coder_sniffer/Drupal/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php @@ -117,10 +117,12 @@ public function process(File $phpcsFile, $stackPtr) break; } - if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line'] + if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line']) { // Only allow empty classes and methods. - && $tokens[$lastContent]['code'] !== T_OPEN_CURLY_BRACKET - ) { + if ($tokens[$lastContent]['code'] === T_OPEN_CURLY_BRACKET) { + return; + } + $error = 'Closing brace must be on a line by itself'; $fix = $phpcsFile->addFixableError($error, $scopeEnd, 'Line'); if ($fix === true) { diff --git a/tests/Drupal/WhiteSpace/ScopeClosingBraceUnitTest.inc b/tests/Drupal/WhiteSpace/ScopeClosingBraceUnitTest.inc index 0e677bce..5e206362 100644 --- a/tests/Drupal/WhiteSpace/ScopeClosingBraceUnitTest.inc +++ b/tests/Drupal/WhiteSpace/ScopeClosingBraceUnitTest.inc @@ -27,3 +27,41 @@ function test2() { */ function test3() { return 7; } + +/** + * Defines an archiver attribute object. + * + * Plugin Namespace: Plugin\Archiver. + * + * For a working example, see \Drupal\system\Plugin\Archiver\Zip + * + * @see \Drupal\Core\Archiver\ArchiverManager + * @see \Drupal\Core\Archiver\ArchiverInterface + * @see plugin_api + * @see hook_archiver_info_alter() + */ +#[\Attribute(\Attribute::TARGET_CLASS)] +class Archiver extends Plugin { + + /** + * Constructs an archiver plugin attribute object. + * + * @param string $id + * The archiver plugin ID. + * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $title + * The human-readable name of the archiver plugin. + * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description + * The description of the archiver plugin. + * @param array $extensions + * An array of valid extensions for this archiver. + * @param class-string|null $deriver + * (optional) The deriver class. + */ + public function __construct( + public readonly string $id, + public readonly ?TranslatableMarkup $title = NULL, + public readonly ?TranslatableMarkup $description = NULL, + public readonly array $extensions = [], + public readonly ?string $deriver = NULL) {} + +} diff --git a/tests/Drupal/WhiteSpace/ScopeClosingBraceUnitTest.inc.fixed b/tests/Drupal/WhiteSpace/ScopeClosingBraceUnitTest.inc.fixed index 36784104..aea60be2 100644 --- a/tests/Drupal/WhiteSpace/ScopeClosingBraceUnitTest.inc.fixed +++ b/tests/Drupal/WhiteSpace/ScopeClosingBraceUnitTest.inc.fixed @@ -28,3 +28,42 @@ function test2() { function test3() { return 7; } + +/** + * Defines an archiver attribute object. + * + * Plugin Namespace: Plugin\Archiver. + * + * For a working example, see \Drupal\system\Plugin\Archiver\Zip + * + * @see \Drupal\Core\Archiver\ArchiverManager + * @see \Drupal\Core\Archiver\ArchiverInterface + * @see plugin_api + * @see hook_archiver_info_alter() + */ +#[\Attribute(\Attribute::TARGET_CLASS)] +class Archiver extends Plugin { + + /** + * Constructs an archiver plugin attribute object. + * + * @param string $id + * The archiver plugin ID. + * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $title + * The human-readable name of the archiver plugin. + * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description + * The description of the archiver plugin. + * @param array $extensions + * An array of valid extensions for this archiver. + * @param class-string|null $deriver + * (optional) The deriver class. + */ + public function __construct( + public readonly string $id, + public readonly ?TranslatableMarkup $title = NULL, + public readonly ?TranslatableMarkup $description = NULL, + public readonly array $extensions = [], + public readonly ?string $deriver = NULL, + ) {} + +}