From 228d21b03670d8abe04f81375864bbede6d2e235 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 17 Apr 2024 09:29:34 +0800 Subject: [PATCH] Add new sniff to detect and remove constructor @return docs (#157) --- CHANGELOG.md | 1 + .../Commenting/ConstructorReturnSniff.php | 120 ++++++++++++++++++ .../Commenting/ConstructorReturnSniffTest.php | 64 ++++++++++ .../fixtures/ConstructorReturn/general.php | 76 +++++++++++ .../ConstructorReturn/general.php.fixed | 74 +++++++++++ 5 files changed, 335 insertions(+) create mode 100644 moodle/Sniffs/Commenting/ConstructorReturnSniff.php create mode 100644 moodle/Tests/Sniffs/Commenting/ConstructorReturnSniffTest.php create mode 100644 moodle/Tests/Sniffs/Commenting/fixtures/ConstructorReturn/general.php create mode 100644 moodle/Tests/Sniffs/Commenting/fixtures/ConstructorReturn/general.php.fixed diff --git a/CHANGELOG.md b/CHANGELOG.md index 0806898..8003d0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format of this change log follows the advice given at [Keep a CHANGELOG](htt ## [Unreleased] ### Added - Add new `moodle.PHPUnit.ParentSetUpTearDown` sniff to verify, among other things, that all the `setUp()`, `tearDown()`, `setUpBeforeClass()` and `tearDownAfterClass()` methods in unit tests are properly calling to their parent counterparts. Applies to Moodle 4.5 and up. +- Add new `moodle.Commenting.ConstructorReturn` sniff to check that constructors do not document a return value. ### Changed - Update composer dependencies to current versions, notably `PHP_CodeSniffer` (3.10.1) and `PHPCompatibility` (96072c30). diff --git a/moodle/Sniffs/Commenting/ConstructorReturnSniff.php b/moodle/Sniffs/Commenting/ConstructorReturnSniff.php new file mode 100644 index 0000000..594cc7b --- /dev/null +++ b/moodle/Sniffs/Commenting/ConstructorReturnSniff.php @@ -0,0 +1,120 @@ +. + +namespace MoodleHQ\MoodleCS\moodle\Sniffs\Commenting; + +use MoodleHQ\MoodleCS\moodle\Util\Docblocks; +use MoodleHQ\MoodleCS\moodle\Util\TokenUtil; +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Sniffs\Sniff; + +/** + * Checks that all files an classes have appropriate docs. + * + * @copyright 2024 Andrew Lyons + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class ConstructorReturnSniff implements Sniff +{ + /** + * Register for class tags. + */ + public function register() { + + return [ + T_CLASS, + ]; + } + + /** + * Processes php files and perform various checks with file. + * + * @param File $phpcsFile The file being scanned. + * @param int $stackPtr The position in the stack. + */ + public function process(File $phpcsFile, $stackPtr) { + $tokens = $phpcsFile->getTokens(); + $endClassPtr = $tokens[$stackPtr]['scope_closer']; + + while ( + ($methodPtr = $phpcsFile->findNext(T_FUNCTION, $stackPtr + 1, $endClassPtr)) !== false + ) { + $this->processClassMethod($phpcsFile, $methodPtr); + $stackPtr = $methodPtr; + } + } + + /** + * Processes the class method. + * + * @param File $phpcsFile The file being scanned. + * @param int $stackPtr The position in the stack. + */ + protected function processClassMethod(File $phpcsFile, int $stackPtr): void { + $objectName = TokenUtil::getObjectName($phpcsFile, $stackPtr); + if ($objectName !== '__constructor') { + // We only care about constructors. + return; + } + + // Get docblock. + $docblockPtr = Docblocks::getDocBlockPointer($phpcsFile, $stackPtr); + if ($docblockPtr === null) { + // No docblocks for this constructor. + return; + } + + $returnTokens = Docblocks::getMatchingDocTags($phpcsFile, $docblockPtr, '@return'); + if (count($returnTokens) === 0) { + // No @return tag in the docblock. + return; + } + + $fix = $phpcsFile->addFixableError( + 'Constructor should not have a return tag in the docblock', + $returnTokens[0], + 'ConstructorReturn' + ); + if ($fix) { + $tokens = $phpcsFile->getTokens(); + $phpcsFile->fixer->beginChangeset(); + + // Find the tokens at the start and end of the line. + $lineStart = $phpcsFile->findFirstOnLine(T_DOC_COMMENT_STAR, $returnTokens[0]); + if ($lineStart === false) { + $lineStart = $returnTokens[0]; + } + + $ptr = $phpcsFile->findNext(T_DOC_COMMENT_WHITESPACE, $lineStart); + for ($lineEnd = $lineStart; $lineEnd < $tokens[$docblockPtr]['comment_closer']; $lineEnd++) { + if ($tokens[$lineEnd]['line'] !== $tokens[$lineStart]['line']) { + break; + } + } + + if ($tokens[$lineEnd]['code'] === T_DOC_COMMENT_CLOSE_TAG) { + $lineEnd--; + } + + for ($ptr = $lineStart; $ptr <= $lineEnd; $ptr++) { + $phpcsFile->fixer->replaceToken($ptr, ''); + } + + $phpcsFile->fixer->endChangeset(); + } + } +} diff --git a/moodle/Tests/Sniffs/Commenting/ConstructorReturnSniffTest.php b/moodle/Tests/Sniffs/Commenting/ConstructorReturnSniffTest.php new file mode 100644 index 0000000..c27b72a --- /dev/null +++ b/moodle/Tests/Sniffs/Commenting/ConstructorReturnSniffTest.php @@ -0,0 +1,64 @@ +. + +namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\Commenting; + +use MoodleHQ\MoodleCS\moodle\Tests\MoodleCSBaseTestCase; + +/** + * Test the ConstructorReturnSniff sniff. + * + * @copyright 2024 onwards Andrew Lyons + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + * @covers \MoodleHQ\MoodleCS\moodle\Sniffs\Commenting\ConstructorReturnSniff + */ +class ConstructorReturnSniffTest extends MoodleCSBaseTestCase +{ + /** + * @dataProvider docblockCorrectnessProvider + */ + public function testMissingDocblockSniff( + string $fixture, + array $errors, + array $warnings + ): void { + $this->setStandard('moodle'); + $this->setSniff('moodle.Commenting.ConstructorReturn'); + $this->setFixture(sprintf("%s/fixtures/ConstructorReturn/%s.php", __DIR__, $fixture)); + $this->setWarnings($warnings); + $this->setErrors($errors); + + $this->verifyCsResults(); + } + + public static function docblockCorrectnessProvider(): array { + $cases = [ + [ + 'fixture' => 'general', + 'errors' => [ + 43 => 'Constructor should not have a return tag in the docblock', + 52 => 'Constructor should not have a return tag in the docblock', + 60 => 'Constructor should not have a return tag in the docblock', + 71 => 'Constructor should not have a return tag in the docblock', + ], + 'warnings' => [], + ], + ]; + return $cases; + } +} diff --git a/moodle/Tests/Sniffs/Commenting/fixtures/ConstructorReturn/general.php b/moodle/Tests/Sniffs/Commenting/fixtures/ConstructorReturn/general.php new file mode 100644 index 0000000..2207678 --- /dev/null +++ b/moodle/Tests/Sniffs/Commenting/fixtures/ConstructorReturn/general.php @@ -0,0 +1,76 @@ +