From 09371ae307bcdbf68fbe4b5331c943c689e1f55f Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 23 Feb 2024 08:09:52 +0800 Subject: [PATCH] Attributes should not be considered a side-effect The use of Attributes should not impact the MOODLE_INTERNAL check. --- moodle/Sniffs/Files/MoodleInternalSniff.php | 3 +++ .../Tests/Sniffs/Files/MoodleInternalTest.php | 5 ++++ .../fixtures/moodleinternal/attribute_ok.php | 27 +++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 moodle/Tests/Sniffs/Files/fixtures/moodleinternal/attribute_ok.php diff --git a/moodle/Sniffs/Files/MoodleInternalSniff.php b/moodle/Sniffs/Files/MoodleInternalSniff.php index fdd239b..4ec3aaf 100644 --- a/moodle/Sniffs/Files/MoodleInternalSniff.php +++ b/moodle/Sniffs/Files/MoodleInternalSniff.php @@ -166,6 +166,9 @@ protected function get_position_of_relevant_code(File $file, $pointer) { } else if ($tokens[$pointer]['code'] === T_DECLARE && $tokens[$pointer]['content'] == 'declare') { // Declare statements must be at start of file. $pointer = $file->findEndOfStatement($pointer + 1); + } else if ($tokens[$pointer]['code'] === T_ATTRIBUTE) { + // Attribute statements must be at start of file. + $pointer = $tokens[$pointer]['attribute_closer'] + 1; } else { $relevantcodefound = true; } diff --git a/moodle/Tests/Sniffs/Files/MoodleInternalTest.php b/moodle/Tests/Sniffs/Files/MoodleInternalTest.php index a6f7beb..8e891e1 100644 --- a/moodle/Tests/Sniffs/Files/MoodleInternalTest.php +++ b/moodle/Tests/Sniffs/Files/MoodleInternalTest.php @@ -150,6 +150,11 @@ public static function provider(): array { ], [], ], + [ + 'attribute_ok', + [], + [], + ], ]; } } diff --git a/moodle/Tests/Sniffs/Files/fixtures/moodleinternal/attribute_ok.php b/moodle/Tests/Sniffs/Files/fixtures/moodleinternal/attribute_ok.php new file mode 100644 index 0000000..f75a4ff --- /dev/null +++ b/moodle/Tests/Sniffs/Files/fixtures/moodleinternal/attribute_ok.php @@ -0,0 +1,27 @@ +. + +/** + * Class with an attribute. + * + * @package core + * @copyright 2024 Andrew Lyons + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +#[\Attribute] +class example { +}