From 74421e1b5e179ae4f4e620c271ce86893b46d98f Mon Sep 17 00:00:00 2001 From: Daniel Ziegenberg Date: Fri, 22 Mar 2024 19:11:41 +0100 Subject: [PATCH 1/4] Add test for multiline attributes for MissingDocblockSniff Signed-off-by: Daniel Ziegenberg --- .../Commenting/MissingDocblockSniffTest.php | 6 +++ .../docblock_with_multiline_attributes.php | 50 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 moodle/Tests/Sniffs/Commenting/fixtures/MissingDocblock/docblock_with_multiline_attributes.php diff --git a/moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php b/moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php index 9d95312..f545276 100644 --- a/moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php +++ b/moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php @@ -129,6 +129,12 @@ public static function docblockCorrectnessProvider(): array { 'errors' => [], 'warnings' => [], ], + 'Docblock with attributes on function (correct)' => [ + 'fixture' => 'docblock_with_multiline_attributes', + 'fixtureFilename' => null, + 'errors' => [], + 'warnings' => [], + ], 'Testcase' => [ 'fixture' => 'testcase_class', 'fixtureFilename' => '/lib/tests/example_test.php', 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 new file mode 100644 index 0000000..4d248e9 --- /dev/null +++ b/moodle/Tests/Sniffs/Commenting/fixtures/MissingDocblock/docblock_with_multiline_attributes.php @@ -0,0 +1,50 @@ + Date: Sat, 23 Mar 2024 13:14:14 +0800 Subject: [PATCH 2/4] Multiline attributes only supported on PHP 8.0 --- .../Commenting/MissingDocblockSniffTest.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php b/moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php index f545276..05a0c16 100644 --- a/moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php +++ b/moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php @@ -129,12 +129,6 @@ public static function docblockCorrectnessProvider(): array { 'errors' => [], 'warnings' => [], ], - 'Docblock with attributes on function (correct)' => [ - 'fixture' => 'docblock_with_multiline_attributes', - 'fixtureFilename' => null, - 'errors' => [], - 'warnings' => [], - ], 'Testcase' => [ 'fixture' => 'testcase_class', 'fixtureFilename' => '/lib/tests/example_test.php', @@ -148,6 +142,16 @@ public static function docblockCorrectnessProvider(): array { ], ]; + if (version_compare(PHP_VERSION, '8.0.0') >= 0) { + $cases['Multiline attributes'] = [ + 'fixture' => 'docblock_with_multiline_attributes', + 'fixtureFilename' => null, + 'errors' => [ + ], + 'warnings' => [], + ]; + } + if (version_compare(PHP_VERSION, '8.1.0') >= 0) { $cases['Enum only (correct)'] = [ 'fixture' => 'enum_only', From 6a18118a51fefc528440a9dea696a170ab11aa82 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Sat, 23 Mar 2024 13:14:25 +0800 Subject: [PATCH 3/4] 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 366e172..6631daa 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; } From 38a320412f5c1d6f9b29572bd7bb998bacd04e65 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Sat, 23 Mar 2024 11:13:17 +0100 Subject: [PATCH 4/4] Cover methods, interfaces and traits with space after attributes Plus fix some `#[\Attribute(` wrong constructors by renaming to custom one. --- .../Commenting/MissingDocblockSniffTest.php | 17 +++++++++++++++++ ...ly_with_attributes_incorrect_whitespace.php | 8 ++++++++ .../docblock_with_multiline_attributes.php | 18 +++++++++--------- ...ly_with_attributes_incorrect_whitespace.php | 14 ++++++++++++++ ...ly_with_attributes_incorrect_whitespace.php | 14 ++++++++++++++ 5 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 moodle/Tests/Sniffs/Commenting/fixtures/MissingDocblock/interface_only_with_attributes_incorrect_whitespace.php create mode 100644 moodle/Tests/Sniffs/Commenting/fixtures/MissingDocblock/trait_only_with_attributes_incorrect_whitespace.php diff --git a/moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php b/moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php index 8f007a1..0f92795 100644 --- a/moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php +++ b/moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php @@ -108,6 +108,23 @@ public static function docblockCorrectnessProvider(): array { 'fixtureFilename' => null, 'errors' => [ 13 => 'Missing docblock for class class_only_with_attributes_incorrect_whitespace', + 20 => 'Missing docblock for function method_only_with_attributes_incorrect_whitespace', + ], + 'warnings' => [], + ], + 'Interface only with attributes and incorrect whitespace' => [ + 'fixture' => 'interface_only_with_attributes_incorrect_whitespace', + 'fixtureFilename' => null, + 'errors' => [ + 13 => 'Missing docblock for interface interface_only_with_attributes_incorrect_whitespace', + ], + 'warnings' => [], + ], + 'Trait only with attributes and incorrect whitespace' => [ + 'fixture' => 'trait_only_with_attributes_incorrect_whitespace', + 'fixtureFilename' => null, + 'errors' => [ + 13 => 'Missing docblock for trait trait_only_with_attributes_incorrect_whitespace', ], 'warnings' => [], ], diff --git a/moodle/Tests/Sniffs/Commenting/fixtures/MissingDocblock/class_only_with_attributes_incorrect_whitespace.php b/moodle/Tests/Sniffs/Commenting/fixtures/MissingDocblock/class_only_with_attributes_incorrect_whitespace.php index 33c021e..6d50083 100644 --- a/moodle/Tests/Sniffs/Commenting/fixtures/MissingDocblock/class_only_with_attributes_incorrect_whitespace.php +++ b/moodle/Tests/Sniffs/Commenting/fixtures/MissingDocblock/class_only_with_attributes_incorrect_whitespace.php @@ -11,4 +11,12 @@ #[with_multiple_attributes, and_another_attribute] class class_only_with_attributes_incorrect_whitespace { + /** + * Method level docblock. + */ + #[example_attribute] + #[with_multiple_attributes, and_another_attribute] + + function method_only_with_attributes_incorrect_whitespace(): void { + } } 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 045f8d1..f516255 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 @@ -9,9 +9,9 @@ defined('MOODLE_INTERNAL') || die(); // Make this always the 1st line in all CS fixtures. /** - * Example class. + * Example class. */ -#[\Attribute( +#[someattribute( attr1: 'asdf', attr2: 'asdf', )] @@ -20,7 +20,7 @@ class class_multiline_attribute { /** * Method attribute. */ - #[\Attribute( + #[someattribute( attr1: 'asdf', attr2: 'asdf', )] @@ -31,7 +31,7 @@ function method_multiline_attribute(): void { /** * Interface with multiline attributes. */ -#[\Attribute( +#[someattribute( attr1: 'asdf', attr2: 'asdf', )] @@ -41,7 +41,7 @@ interface interface_multiline_attribute { /** * Trait with multiline attributes. */ -#[\Attribute( +#[someattribute( attr1: 'asdf', attr2: 'asdf', )] @@ -52,7 +52,7 @@ trait trait_multiline_attribute { * Example class. */ -#[\Attribute( +#[someattribute( attr1: 'asdf', attr2: 'asdf', )] @@ -62,7 +62,7 @@ class class_multiline_attribute_space_between { * Method attribute. */ - #[\Attribute( + #[someattribute( attr1: 'asdf', attr2: 'asdf', )] @@ -74,7 +74,7 @@ function method_multiline_attribute_space_between(): void { * Interface with multiline attributes. */ -#[\Attribute( +#[someattribute( attr1: 'asdf', attr2: 'asdf', )] @@ -85,7 +85,7 @@ interface interface_multiline_attribute_space_between { * Trait with multiline attributes and space between. */ - #[\Attribute( + #[someattribute( attr1: 'asdf', attr2: 'asdf', )] diff --git a/moodle/Tests/Sniffs/Commenting/fixtures/MissingDocblock/interface_only_with_attributes_incorrect_whitespace.php b/moodle/Tests/Sniffs/Commenting/fixtures/MissingDocblock/interface_only_with_attributes_incorrect_whitespace.php new file mode 100644 index 0000000..6718d64 --- /dev/null +++ b/moodle/Tests/Sniffs/Commenting/fixtures/MissingDocblock/interface_only_with_attributes_incorrect_whitespace.php @@ -0,0 +1,14 @@ +