Skip to content

Commit

Permalink
Bump moodle-cs to v3.3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Oct 20, 2023
1 parent 7da28be commit 3447914
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
composer.phar
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "GPL-3.0-or-later",
"require": {
"php": ">=7.4",
"moodlehq/moodle-cs": "^3.3.9",
"moodlehq/moodle-cs": "^v3.3.10",
"phpcompatibility/php-compatibility": "dev-develop#0a17f9ed"
},
"config": {
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file removed composer.phar
Binary file not shown.
4 changes: 2 additions & 2 deletions tests/locallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static function local_codechecker_find_other_files_provider(): array {
* @covers ::local_codechecker_find_other_files
*/
public function test_local_codechecker_find_other_files(string $path, array $ignores,
array $extensions, array $matches, array $nomatches) {
array $extensions, array $matches, array $nomatches): void {

global $CFG;
require_once(__DIR__ . '/../locallib.php');
Expand Down Expand Up @@ -149,7 +149,7 @@ public function test_local_codechecker_find_other_files(string $path, array $ign
*
* @covers ::local_codechecker_check_other_file
*/
public function test_local_codechecker_check_other_file() {
public function test_local_codechecker_check_other_file(): void {

require_once(__DIR__ . '/../locallib.php');

Expand Down
12 changes: 6 additions & 6 deletions vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@
},
{
"name": "moodlehq/moodle-cs",
"version": "v3.3.9",
"version_normalized": "3.3.9.0",
"version": "v3.3.10",
"version_normalized": "3.3.10.0",
"source": {
"type": "git",
"url": "https://github.com/moodlehq/moodle-cs.git",
"reference": "5503396df563cbe3244b67625246664ba5b663f1"
"reference": "13192ea4f0e7ac0c94571a6c7e33948db740f1ab"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/moodlehq/moodle-cs/zipball/5503396df563cbe3244b67625246664ba5b663f1",
"reference": "5503396df563cbe3244b67625246664ba5b663f1",
"url": "https://api.github.com/repos/moodlehq/moodle-cs/zipball/13192ea4f0e7ac0c94571a6c7e33948db740f1ab",
"reference": "13192ea4f0e7ac0c94571a6c7e33948db740f1ab",
"shasum": ""
},
"require": {
Expand All @@ -113,7 +113,7 @@
"sebastian/phpcpd": "^6.0",
"thor-juhasz/phpunit-coverage-check": "^0.3.0"
},
"time": "2023-09-26T14:48:47+00:00",
"time": "2023-10-20T07:20:45+00:00",
"type": "phpcodesniffer-standard",
"installation-source": "dist",
"autoload": {
Expand Down
10 changes: 5 additions & 5 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'name' => 'moodlehq/local_codechecker',
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => '3a7e2dfb86d7f08b3ba48a2007c015b0f40e27b9',
'reference' => '94709634e38007ebb30a695dbdf3f6f8fe7d1fb6',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand All @@ -22,16 +22,16 @@
'moodlehq/local_codechecker' => array(
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => '3a7e2dfb86d7f08b3ba48a2007c015b0f40e27b9',
'reference' => '94709634e38007ebb30a695dbdf3f6f8fe7d1fb6',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'dev_requirement' => false,
),
'moodlehq/moodle-cs' => array(
'pretty_version' => 'v3.3.9',
'version' => '3.3.9.0',
'reference' => '5503396df563cbe3244b67625246664ba5b663f1',
'pretty_version' => 'v3.3.10',
'version' => '3.3.10.0',
'reference' => '13192ea4f0e7ac0c94571a6c7e33948db740f1ab',
'type' => 'phpcodesniffer-standard',
'install_path' => __DIR__ . '/../moodlehq/moodle-cs',
'aliases' => array(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?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/>.

namespace MoodleHQ\MoodleCS\moodle\Sniffs\PHPUnit;

// phpcs:disable moodle.NamingConventions

use MoodleHQ\MoodleCS\moodle\Util\MoodleUtil;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHPCSUtils\Utils\FunctionDeclarations;
use PHPCSUtils\Utils\ObjectDeclarations;

/**
* Checks that a test file has the @coversxxx annotations properly defined.
*
* @package local_codechecker
* @copyright 2022 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class TestReturnTypeSniff implements Sniff
{

/**
* Register for open tag (only process once per file).
*/
public function register() {
return [
T_OPEN_TAG,
];
}

/**
* Processes php files and perform various checks with file.
*
* @param File $file The file being scanned.
* @param int $pointer The position in the stack.
*/
public function process(File $file, $pointer)
{
// Before starting any check, let's look for various things.

// If we aren't checking Moodle 4.4dev (404) and up, nothing to check.
// Make and exception for codechecker phpunit tests, so they are run always.
if (!MoodleUtil::meetsMinimumMoodleVersion($file, 404) && !MoodleUtil::isUnitTestRunning()) {
return; // @codeCoverageIgnore
}

// If the file is not a unit test file, nothing to check.
if (!MoodleUtil::isUnitTest($file) && !MoodleUtil::isUnitTestRunning()) {
return; // @codeCoverageIgnore
}

// We have all we need from core, let's start processing the file.

// Get the file tokens, for ease of use.
$tokens = $file->getTokens();

// We only want to do this once per file.
$prevopentag = $file->findPrevious(T_OPEN_TAG,
$pointer - 1
);
if ($prevopentag !== false) {
return; // @codeCoverageIgnore
}

// Iterate over all the classes (hopefully only one, but that's not this sniff problem).
$cStart = $pointer;
while ($cStart = $file->findNext(T_CLASS, $cStart + 1)) {
if (MoodleUtil::isUnitTestCaseClass($file, $cStart) === false) {
// This class does not related to a uit test.
continue;
}

$classInfo = ObjectDeclarations::getClassProperties($file, $cStart);

// Iterate over all the methods in the class.
$mStart = $cStart;
while ($mStart = $file->findNext(T_FUNCTION, $mStart + 1, $tokens[$cStart]['scope_closer'])) {
$method = $file->getDeclarationName($mStart);

// Ignore non test_xxxx() methods.
if (strpos($method, 'test_') !== 0) {
continue;
}

// Get the function declaration.
$functionInfo = FunctionDeclarations::getProperties($file, $mStart);

// 'return_type_token' => int|false, // The stack pointer to the start of the return type.
if ($functionInfo['return_type_token'] !== false) {
// This method has a return type.
// In most cases that will be void, but it could be anything in the
// case of chained or dependant tests.
// TODO: Detect all cases of chained tests and dependant tests.
continue;
}

$methodNamePtr = $file->findNext(T_STRING, $mStart + 1, $tokens[$mStart]['parenthesis_opener']);
$methodEnd = $file->findNext(T_CLOSE_PARENTHESIS, $mStart + 1);

// Detect if the method has a return statement.
// If it does, then see if it has a value or not.
$hasReturn = $file->findNext(T_RETURN, $mStart + 1, $tokens[$mStart]['scope_closer']);
$probablyVoid = !$hasReturn;
if ($hasReturn) {
$next = $file->findNext(
T_WHITESPACE,
$hasReturn + 1,
$tokens[$mStart]['scope_closer'],
true
);
if ($tokens[$next]['code'] === T_SEMICOLON) {
$probablyVoid = true;
}
}

$fix = false;
if ($probablyVoid) {
$fix = $file->addFixableWarning(
'Test method %s() is missing a return type',
$methodNamePtr,
'MissingReturnType',
[$method]
);
} else {
$file->addWarning(
'Test method %s() is missing a return type',
$methodNamePtr,
'MissingReturnType',
[$method]
);
}

if ($fix) {
$file->fixer->beginChangeset();
$file->fixer->addContent($methodEnd, ': void');
$file->fixer->endChangeset();
}
}
}
}
}
31 changes: 29 additions & 2 deletions vendor/moodlehq/moodle-cs/moodle/Util/MoodleUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Exceptions\DeepExitException;
use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Files\DummyFile;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Tokenizers\PHP;

// phpcs:disable moodle.NamingConventions

Expand Down Expand Up @@ -424,6 +422,35 @@ public static function isUnitTestRunning(): bool
return defined('PHPUNIT_TEST') && PHPUNIT_TEST;
}

/**
* Whether the class is a unit test case class.
*
* @param File $file
* @param int $classPtr
* @return bool
*/
public static function isUnitTestCaseClass(
File $file,
int $classPtr
): bool {
$tokens = $file->getTokens();
$class = $file->getDeclarationName($classPtr);

// Only if the class is extending something.
// TODO: We could add a list of valid classes once we have a class-map available.
if (!$file->findNext(T_EXTENDS, $classPtr + 1, $tokens[$classPtr]['scope_opener'])) {
return false;
}

// Ignore non ended "_test|_testcase" classes.
if (substr($class, -5) !== '_test' && substr($class, -9) != '_testcase') {
return false;
}

// This is a class, which extends another class, and whose name ends in _test.
return true;
}

/**
* Whether the file belongs to a version of Moodle meeting the specifeid minimum version.
*
Expand Down
10 changes: 10 additions & 0 deletions vendor/moodlehq/moodle-cs/moodle/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@
-->
<rule ref="moodle.PHPUnit.TestCaseProvider"/>

<!--
Tests should have a return type. The default return type should be void.
-->
<rule ref="moodle.PHPUnit.TestReturnType"/>

<!-- Disable this exact error unless it's approved -->
<rule ref="moodle.Commenting.InlineComment.SpacingAfter">
<severity>0</severity>
Expand All @@ -124,4 +129,9 @@
<!-- Let's add the complete PHPCompatibility standard -->
<rule ref="PHPCompatibility" />

<!--
Detect duplicate array keys. This is useful for unit test providers in particular.
-->
<rule ref="Universal.Arrays.DuplicateArrayKey"/>

</ruleset>

0 comments on commit 3447914

Please sign in to comment.