From 0f95abc3e79f08a5d651dbf6952090753d061409 Mon Sep 17 00:00:00 2001 From: Jonathan Smith Date: Sat, 27 Jan 2024 17:29:27 +0000 Subject: [PATCH] feat(FunctionComment): Docblock is not required for __constuct() methods (#3400560) --- .../Drupal/Sniffs/Commenting/FunctionCommentSniff.php | 10 ++++++++++ tests/Drupal/Commenting/FunctionCommentUnitTest.inc | 10 ++++++++++ .../Commenting/FunctionCommentUnitTest.inc.fixed | 10 ++++++++++ 3 files changed, 30 insertions(+) diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php index ddfd5dd1..9c7acb47 100644 --- a/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php @@ -93,6 +93,16 @@ public function process(File $phpcsFile, $stackPtr) break; } + // Constructor methods are exempt from requiring a docblock. + // @see https://www.drupal.org/project/coder/issues/3400560. + $methodName = $phpcsFile->getDeclarationName($stackPtr); + if ($methodName === '__construct' + && $tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG + && $tokens[$commentEnd]['code'] !== T_COMMENT + ) { + return; + } + $beforeCommentEnd = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($commentEnd - 1), null, true); if (($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG && $tokens[$commentEnd]['code'] !== T_COMMENT) diff --git a/tests/Drupal/Commenting/FunctionCommentUnitTest.inc b/tests/Drupal/Commenting/FunctionCommentUnitTest.inc index 635ed82b..2db543b5 100644 --- a/tests/Drupal/Commenting/FunctionCommentUnitTest.inc +++ b/tests/Drupal/Commenting/FunctionCommentUnitTest.inc @@ -947,4 +947,14 @@ class Test3 { return []; } +} + +/** + * Test that docblock is optional for __construct methods. + */ +class Test43 { + + public function __construct() { + } + } \ No newline at end of file diff --git a/tests/Drupal/Commenting/FunctionCommentUnitTest.inc.fixed b/tests/Drupal/Commenting/FunctionCommentUnitTest.inc.fixed index 3ff9bb36..da234c6c 100644 --- a/tests/Drupal/Commenting/FunctionCommentUnitTest.inc.fixed +++ b/tests/Drupal/Commenting/FunctionCommentUnitTest.inc.fixed @@ -974,3 +974,13 @@ class Test3 { } } + +/** + * Test that docblock is optional for __construct methods. + */ +class Test43 { + + public function __construct() { + } + +}