Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add coverage info (and related Sniff) #143

Merged
merged 2 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ jobs:

- name: PHPUnit tests
if: ${{ always() }}
run: moodle-plugin-ci phpunit
run: moodle-plugin-ci phpunit --coverage-text

- name: Behat features
if: ${{ always() }}
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ script:
- moodle-plugin-ci mustache
- moodle-plugin-ci grunt
- moodle-plugin-ci phpdoc
- moodle-plugin-ci phpunit
- moodle-plugin-ci phpunit --coverage-text
- moodle-plugin-ci behat
46 changes: 46 additions & 0 deletions moodle/Sniffs/Commenting/InlineCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ public function process(File $phpcsFile, $stackPtr) {
return;
}

// Allow phpdoc block before "return new class extends" expressions,
// we use those anon classes in places like coverage.php files.
if ($this->is_return_new_class_extends($phpcsFile, $stackPtr)) {
return;
}

// Allow phpdoc before define() token (see CONTRIB-4150).
if ($tokens[$nextToken]['code'] == T_STRING and $tokens[$nextToken]['content'] == 'define') {
return;
Expand Down Expand Up @@ -515,5 +521,45 @@ public function process(File $phpcsFile, $stackPtr) {

}//end process()

/**
* This looks if there is a valid "return new class extends" expression allowed to have phpdoc block.
*
* @param File $file The file being scanned.
* @param int $pointer The position in the stack.
* @return bool true if is an allowed to have phpdoc block return new class code.
*/
protected function is_return_new_class_extends(File $file, $pointer) {

$ignoredtokens = Tokens::$emptyTokens;

$tokens = $file->getTokens();

// Detect 'return'.
$pointer = $file->findNext($ignoredtokens, $pointer + 1, null, true);
if ($tokens[$pointer]['code'] !== T_RETURN) {
return false;
}

// Detect 'new'.
$pointer = $file->findNext($ignoredtokens, $pointer + 1, null, true);
if ($tokens[$pointer]['code'] !== T_NEW) {
return false;
}

// Detect 'class'.
$pointer = $file->findNext($ignoredtokens, $pointer + 1, null, true);
if ($tokens[$pointer]['code'] !== T_ANON_CLASS) {
return false;
}

// Detect 'extends'.
$pointer = $file->findNext($ignoredtokens, $pointer + 1, null, true);
if ($tokens[$pointer]['code'] !== T_EXTENDS) {
return false;
}

// Found a valid "return new class extends" expression, phpdoc block allowed.
return true;
}// end is_return_new_class_extends()

}//end class
16 changes: 16 additions & 0 deletions moodle/tests/fixtures/moodle_comenting_inlinecomment.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,19 @@ final function afunction() {}
foreach ($cms as $something) {
echo 'This is a test';
}

// Allow phpdoc before "return new class extends" expressions.
/** This is a phpdoc block */
return new class extends xxxx {}

// But don't allow it before other expressions.
/** This is a phpdoc block */
return new stdClass();
/** This is a phpdoc block */
return new class {}
/** This is a phpdoc block */
return class extends xxxx {}
/** This is a phpdoc block */
new class testphpdoc {}
/** This is a phpdoc block */
return new class implements something {}
12 changes: 10 additions & 2 deletions moodle/tests/moodlestandard_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ public function test_moodle_commenting_inlinecomment() {
91 => '\'$variable\' does not match next code line \'lets_execute_it...\'',
94 => 1,
102 => '\'$cm\' does not match next list() variables @Source: moodle.Commenting.InlineComment.TypeHintingList',
112 => '\'$cm\' does not match next foreach() as variable @Source: moodle.Commenting.InlineComment.TypeHintingFor'));
112 => '\'$cm\' does not match next foreach() as variable @Source: moodle.Commenting.InlineComment.TypeHintingFor',
118 => 0,
122 => 1,
124 => 1,
126 => 1,
128 => 1,
130 => 1));
$this->set_warnings(array(
4 => 0,
6 => array(null, 'Commenting.InlineComment.InvalidEndChar'),
Expand All @@ -107,7 +113,9 @@ public function test_moodle_commenting_inlinecomment() {
71 => 3,
75 => 2,
77 => 1,
79 => 1));
79 => 1,
118 => 0,
122 => 0));

// Let's do all the hard work!
$this->verify_cs_results();
Expand Down
2 changes: 1 addition & 1 deletion tests/behat/ui.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Feature: Codechecker UI works as expected
| local/codechecker/version.php | Well done! | Invalid path |
| local/codechecker/moodle/tests/fixtures | Files found: 0 | Invalid path |
| local/codechecker/tests/ | local_codechecker_testcase.php | Invalid path |
| local/codechecker/tests/ | Files found: 1 | Invalid path |
| local/codechecker/tests/ | Files found: 2 | Invalid path |
| local/codechecker/tests/ | Well done! | Invalid path |
| admin/index.php | Files found: 1 | Invalid path |
| admin/index.php | Total: | Well done! |
Expand Down
45 changes: 45 additions & 0 deletions tests/coverage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

defined('MOODLE_INTERNAL') || die();

/**
* Coverage information for the local_codechecker plugin.
*
* @package local_codechecker
* @category test
* @copyright 2021 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
return new class extends phpunit_coverage_info {
/** @var array The list of folders relative to the plugin root to include in coverage generation. */
protected $includelistfolders = [
'classes',
'moodle',
];

/** @var array The list of files relative to the plugin root to include in coverage generation. */
protected $includelistfiles = [
'locallib.php'];

/** @var array The list of folders relative to the plugin root to exclude from coverage generation. */
protected $excludelistfolders = [
'moodle/tests',
];

/** @var array The list of files relative to the plugin root to exclude from coverage generation. */
protected $excludelistfiles = [];
};