diff --git a/moodle/Sniffs/Commenting/NoInlineSniff.php b/moodle/Sniffs/Commenting/NoInlineSniff.php new file mode 100644 index 0000000..9aa69d5 --- /dev/null +++ b/moodle/Sniffs/Commenting/NoInlineSniff.php @@ -0,0 +1,68 @@ +. + +namespace MoodleHQ\MoodleCS\moodle\Sniffs\Commenting; + +use MoodleHQ\MoodleCS\moodle\Util\MoodleUtil; +use MoodleHQ\MoodleCS\moodle\Util\Docblocks; +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Files\File; + +/** + * Checks for the presence of inline docblocks. + * + * Inline docblocks are those which start with three ///. + * + * @copyright 2024 Andrew Lyons + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class NoInlineSniff implements Sniff +{ + /** + * Register for open tag (only process once per file). + */ + public function register() { + return [ + T_COMMENT, + ]; + } + + /** + * 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(); + $token = $tokens[$stackPtr]; + + if (strpos($token['content'], '///') === 0) { + $fix = $phpcsFile->addFixableError( + 'Invalid inline comment found. Comments should not start with three slashes (///).', + $stackPtr, + 'InvalidInlineComment' + ); + + if ($fix === true) { + $phpcsFile->fixer->beginChangeset(); + $phpcsFile->fixer->replaceToken($stackPtr, preg_replace('@^/{2,}@', '//', $token['content'])); + $phpcsFile->fixer->endChangeset(); + } + } + } +} diff --git a/moodle/Tests/Sniffs/Commenting/NoInlineSniffTest.php b/moodle/Tests/Sniffs/Commenting/NoInlineSniffTest.php new file mode 100644 index 0000000..12f59ea --- /dev/null +++ b/moodle/Tests/Sniffs/Commenting/NoInlineSniffTest.php @@ -0,0 +1,66 @@ +. + +namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\Commenting; + +use MoodleHQ\MoodleCS\moodle\Tests\MoodleCSBaseTestCase; + +/** + * Test the NoInlineSniff 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\NoInlineSniff + */ +class NoInlineSniffTest extends MoodleCSBaseTestCase +{ + /** + * @dataProvider fixtureProvider + */ + public function testFixtures( + string $fixture, + array $errors, + array $warnings + ): void { + $this->setStandard('moodle'); + $this->setSniff('moodle.Commenting.NoInline'); + $this->setFixture(sprintf("%s/fixtures/NoInline/%s.php", __DIR__, $fixture)); + $this->setWarnings($warnings); + $this->setErrors($errors); + + $this->verifyCsResults(); + } + + public static function fixtureProvider(): array { + return [ + 'Standard fixes' => [ + 'fixture' => 'standard', + 'errors' => [ + 3 => 'Invalid inline comment found. Comments should not start with three slashes (///).', + 5 => 'Invalid inline comment found. Comments should not start with three slashes (///).', + 8 => 'Invalid inline comment found. Comments should not start with three slashes (///).', + 10 => 'Invalid inline comment found. Comments should not start with three slashes (///).', + 13 => 'Invalid inline comment found. Comments should not start with three slashes (///).', + 19 => 'Invalid inline comment found. Comments should not start with three slashes (///).', + 20 => 'Invalid inline comment found. Comments should not start with three slashes (///).', + ], + 'warnings' => [], + ], + ]; + } +} diff --git a/moodle/Tests/Sniffs/Commenting/fixtures/NoInline/standard.php b/moodle/Tests/Sniffs/Commenting/fixtures/NoInline/standard.php new file mode 100644 index 0000000..cb6a568 --- /dev/null +++ b/moodle/Tests/Sniffs/Commenting/fixtures/NoInline/standard.php @@ -0,0 +1,20 @@ +