Skip to content

Commit 93277c4

Browse files
authored
Merge pull request #143 from stronk7/add_coverage_info
Add coverage info (and related Sniff)
2 parents 46b5d10 + 97df175 commit 93277c4

File tree

7 files changed

+120
-5
lines changed

7 files changed

+120
-5
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ jobs:
153153

154154
- name: PHPUnit tests
155155
if: ${{ always() }}
156-
run: moodle-plugin-ci phpunit
156+
run: moodle-plugin-ci phpunit --coverage-text
157157

158158
- name: Behat features
159159
if: ${{ always() }}

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,5 @@ script:
9191
- moodle-plugin-ci mustache
9292
- moodle-plugin-ci grunt
9393
- moodle-plugin-ci phpdoc
94-
- moodle-plugin-ci phpunit
94+
- moodle-plugin-ci phpunit --coverage-text
9595
- moodle-plugin-ci behat

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! |

tests/coverage.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
defined('MOODLE_INTERNAL') || die();
18+
19+
/**
20+
* Coverage information for the local_codechecker plugin.
21+
*
22+
* @package local_codechecker
23+
* @category test
24+
* @copyright 2021 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com}
25+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26+
*/
27+
return new class extends phpunit_coverage_info {
28+
/** @var array The list of folders relative to the plugin root to include in coverage generation. */
29+
protected $includelistfolders = [
30+
'classes',
31+
'moodle',
32+
];
33+
34+
/** @var array The list of files relative to the plugin root to include in coverage generation. */
35+
protected $includelistfiles = [
36+
'locallib.php'];
37+
38+
/** @var array The list of folders relative to the plugin root to exclude from coverage generation. */
39+
protected $excludelistfolders = [
40+
'moodle/tests',
41+
];
42+
43+
/** @var array The list of files relative to the plugin root to exclude from coverage generation. */
44+
protected $excludelistfiles = [];
45+
};

0 commit comments

Comments
 (0)