Skip to content

Commit 97df175

Browse files
committed
Allow phpdoc blocks before "return new class extends" code
Mostly used by coverage info anon classes, but there may be other cases, that sort of construction is allowed to have an inline phpdoc block. Covered with tests.
1 parent 94ed652 commit 97df175

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

moodle/Sniffs/Commenting/InlineCommentSniff.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ public function process(File $phpcsFile, $stackPtr) {
115115
return;
116116
}
117117

118+
// Allow phpdoc block before "return new class extends" expressions,
119+
// we use those anon classes in places like coverage.php files.
120+
if ($this->is_return_new_class_extends($phpcsFile, $stackPtr)) {
121+
return;
122+
}
123+
118124
// Allow phpdoc before define() token (see CONTRIB-4150).
119125
if ($tokens[$nextToken]['code'] == T_STRING and $tokens[$nextToken]['content'] == 'define') {
120126
return;
@@ -515,5 +521,45 @@ public function process(File $phpcsFile, $stackPtr) {
515521

516522
}//end process()
517523

524+
/**
525+
* This looks if there is a valid "return new class extends" expression allowed to have phpdoc block.
526+
*
527+
* @param File $file The file being scanned.
528+
* @param int $pointer The position in the stack.
529+
* @return bool true if is an allowed to have phpdoc block return new class code.
530+
*/
531+
protected function is_return_new_class_extends(File $file, $pointer) {
532+
533+
$ignoredtokens = Tokens::$emptyTokens;
534+
535+
$tokens = $file->getTokens();
536+
537+
// Detect 'return'.
538+
$pointer = $file->findNext($ignoredtokens, $pointer + 1, null, true);
539+
if ($tokens[$pointer]['code'] !== T_RETURN) {
540+
return false;
541+
}
542+
543+
// Detect 'new'.
544+
$pointer = $file->findNext($ignoredtokens, $pointer + 1, null, true);
545+
if ($tokens[$pointer]['code'] !== T_NEW) {
546+
return false;
547+
}
548+
549+
// Detect 'class'.
550+
$pointer = $file->findNext($ignoredtokens, $pointer + 1, null, true);
551+
if ($tokens[$pointer]['code'] !== T_ANON_CLASS) {
552+
return false;
553+
}
554+
555+
// Detect 'extends'.
556+
$pointer = $file->findNext($ignoredtokens, $pointer + 1, null, true);
557+
if ($tokens[$pointer]['code'] !== T_EXTENDS) {
558+
return false;
559+
}
560+
561+
// Found a valid "return new class extends" expression, phpdoc block allowed.
562+
return true;
563+
}// end is_return_new_class_extends()
518564

519565
}//end class

moodle/tests/fixtures/moodle_comenting_inlinecomment.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,19 @@ final function afunction() {}
113113
foreach ($cms as $something) {
114114
echo 'This is a test';
115115
}
116+
117+
// Allow phpdoc before "return new class extends" expressions.
118+
/** This is a phpdoc block */
119+
return new class extends xxxx {}
120+
121+
// But don't allow it before other expressions.
122+
/** This is a phpdoc block */
123+
return new stdClass();
124+
/** This is a phpdoc block */
125+
return new class {}
126+
/** This is a phpdoc block */
127+
return class extends xxxx {}
128+
/** This is a phpdoc block */
129+
new class testphpdoc {}
130+
/** This is a phpdoc block */
131+
return new class implements something {}

moodle/tests/moodlestandard_test.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,13 @@ public function test_moodle_commenting_inlinecomment() {
9292
91 => '\'$variable\' does not match next code line \'lets_execute_it...\'',
9393
94 => 1,
9494
102 => '\'$cm\' does not match next list() variables @Source: moodle.Commenting.InlineComment.TypeHintingList',
95-
112 => '\'$cm\' does not match next foreach() as variable @Source: moodle.Commenting.InlineComment.TypeHintingFor'));
95+
112 => '\'$cm\' does not match next foreach() as variable @Source: moodle.Commenting.InlineComment.TypeHintingFor',
96+
118 => 0,
97+
122 => 1,
98+
124 => 1,
99+
126 => 1,
100+
128 => 1,
101+
130 => 1));
96102
$this->set_warnings(array(
97103
4 => 0,
98104
6 => array(null, 'Commenting.InlineComment.InvalidEndChar'),
@@ -107,7 +113,9 @@ public function test_moodle_commenting_inlinecomment() {
107113
71 => 3,
108114
75 => 2,
109115
77 => 1,
110-
79 => 1));
116+
79 => 1,
117+
118 => 0,
118+
122 => 0));
111119

112120
// Let's do all the hard work!
113121
$this->verify_cs_results();

tests/behat/ui.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Feature: Codechecker UI works as expected
2121
| local/codechecker/version.php | Well done! | Invalid path |
2222
| local/codechecker/moodle/tests/fixtures | Files found: 0 | Invalid path |
2323
| local/codechecker/tests/ | local_codechecker_testcase.php | Invalid path |
24-
| local/codechecker/tests/ | Files found: 1 | Invalid path |
24+
| local/codechecker/tests/ | Files found: 2 | Invalid path |
2525
| local/codechecker/tests/ | Well done! | Invalid path |
2626
| admin/index.php | Files found: 1 | Invalid path |
2727
| admin/index.php | Total: | Well done! |

0 commit comments

Comments
 (0)