From bddb63096493140e22e9f61da57dab8f82f6ccf4 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Sat, 23 Mar 2024 13:14:25 +0800 Subject: [PATCH] Fix docblock detection for multiline attribs (#132) Fixes #131. --- .../Commenting/MissingDocblockSniffTest.php | 4 ++ .../docblock_with_multiline_attributes.php | 45 ++++++++++++++++++- moodle/Util/Docblocks.php | 4 +- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php b/moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php index 05a0c16..8f007a1 100644 --- a/moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php +++ b/moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php @@ -147,6 +147,10 @@ public static function docblockCorrectnessProvider(): array { 'fixture' => 'docblock_with_multiline_attributes', 'fixtureFilename' => null, 'errors' => [ + 59 => 'Missing docblock for class class_multiline_attribute_space_between', + 69 => 'Missing docblock for function method_multiline_attribute_space_between', + 81 => 'Missing docblock for interface interface_multiline_attribute_space_between', + 92 => 'Missing docblock for trait trait_multiline_attribute_space_between', ], 'warnings' => [], ]; diff --git a/moodle/Tests/Sniffs/Commenting/fixtures/MissingDocblock/docblock_with_multiline_attributes.php b/moodle/Tests/Sniffs/Commenting/fixtures/MissingDocblock/docblock_with_multiline_attributes.php index 4d248e9..045f8d1 100644 --- a/moodle/Tests/Sniffs/Commenting/fixtures/MissingDocblock/docblock_with_multiline_attributes.php +++ b/moodle/Tests/Sniffs/Commenting/fixtures/MissingDocblock/docblock_with_multiline_attributes.php @@ -41,10 +41,53 @@ interface interface_multiline_attribute { /** * Trait with multiline attributes. */ - #[\Attribute( attr1: 'asdf', attr2: 'asdf', )] trait trait_multiline_attribute { } + +/** + * Example class. + */ + +#[\Attribute( + attr1: 'asdf', + attr2: 'asdf', +)] +class class_multiline_attribute_space_between { + + /** + * Method attribute. + */ + + #[\Attribute( + attr1: 'asdf', + attr2: 'asdf', + )] + function method_multiline_attribute_space_between(): void { + } +} + +/** + * Interface with multiline attributes. + */ + +#[\Attribute( + attr1: 'asdf', + attr2: 'asdf', +)] +interface interface_multiline_attribute_space_between { +} + +/** + * Trait with multiline attributes and space between. + */ + + #[\Attribute( + attr1: 'asdf', + attr2: 'asdf', +)] +trait trait_multiline_attribute_space_between { +} diff --git a/moodle/Util/Docblocks.php b/moodle/Util/Docblocks.php index 2dea703..b4dba9a 100644 --- a/moodle/Util/Docblocks.php +++ b/moodle/Util/Docblocks.php @@ -215,12 +215,12 @@ public static function getDocBlockPointer( if ($token['code'] === T_ATTRIBUTE_END && isset($token['attribute_opener'])) { $commentEnd = $token['attribute_opener']; - $pointerLine = $token['line']; + $pointerLine = $tokens[$commentEnd]['line']; continue; } if ($token['line'] < ($pointerLine - 1)) { - // The comment msut be on the line immediately before the pointer, or immediately before the attribute. z + // The comment must be on the line immediately before the pointer, or immediately before the attribute. z return null; }