Skip to content

Commit

Permalink
Merge branch 'main' into main-boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
micaherne authored Jun 5, 2024
2 parents ebd9b05 + 4f1bc63 commit 0ce282e
Show file tree
Hide file tree
Showing 18 changed files with 1,349 additions and 19 deletions.
38 changes: 31 additions & 7 deletions .github/workflows/phpcs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,42 @@ jobs:
run: ./vendor/bin/phpunit-coverage-check -t 80 clover.xml

- name: Integration tests
if: ${{ (!cancelled()) && (runner.os == 'ubuntu-latest') }}
if: ${{ !cancelled() && matrix.os == 'ubuntu-latest' }}
run: |
# There is one failure (exit with error)
vendor/bin/phpcs --standard=moodle moodle/Tests/fixtures/integration_test_ci.php | tee output.txt || [[ $? = 1 ]]
# There is one failure (exit with error 2, because some are fixable).
expectedcode=2
vendor/bin/phpcs --standard=moodle moodle/Tests/fixtures/integration_test_ci.php | tee output.txt
exitcode="${PIPESTATUS[0]}"
if [[ "${exitcode}" = "${expectedcode}" ]]; then
echo "Ok, got expected ${exitcode} exit code."
else
echo "Error: Expected ${expectedcode}, got ${exitcode} exit code."
exit 1
fi
grep -q "PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY" output.txt
# The failure is fixed (exit with error)
vendor/bin/phpcbf --standard=moodle moodle/Tests/fixtures/integration_test_ci.php | tee output.txt || [[ $? = 1 ]]
# The failure is fixed (exit with error 1, because all fixable ones were fixed).
expectedcode=1
vendor/bin/phpcbf --standard=moodle moodle/Tests/fixtures/integration_test_ci.php | tee output.txt
exitcode="${PIPESTATUS[0]}"
if [[ "${exitcode}" = "${expectedcode}" ]]; then
echo "Ok, got expected ${exitcode} exit code."
else
echo "Error: Expected ${expectedcode}, got ${exitcode} exit code."
exit 1
fi
grep -q "A TOTAL OF 1 ERROR WERE FIXED IN 1 FILE" output.txt
# So, there isn't any failure any more (exit without error)
vendor/bin/phpcs --standard=moodle moodle/Tests/fixtures/integration_test_ci.php | tee output.txt && [[ $? = 0 ]]
# So, there isn't any failure any more (exit without error, aka, 0)
expectedcode=0
vendor/bin/phpcs --standard=moodle moodle/Tests/fixtures/integration_test_ci.php | tee output.txt
exitcode="${PIPESTATUS[0]}"
if [[ "${exitcode}" = "${expectedcode}" ]]; then
echo "Ok, got expected ${exitcode} exit code."
else
echo "Error: Expected ${expectedcode}, got ${exitcode} exit code."
exit 1
fi
- name: Mark cancelled jobs as failed
if: ${{ cancelled() }}
Expand Down
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ The format of this change log follows the advice given at [Keep a CHANGELOG](htt

## [Unreleased]

## [v3.4.7] - 2024-05-31
### Added
- Add new `moodle.PHPUnit.ParentSetUpTearDown` sniff to verify, among other things, that all the `setUp()`, `tearDown()`, `setUpBeforeClass()` and `tearDownAfterClass()` methods in unit tests are properly calling to their parent counterparts. Applies to Moodle 4.5 and up.
- Add new `moodle.Commenting.ConstructorReturn` sniff to check that constructors do not document a return value.

### Changed
- Update composer dependencies to current versions, notably `PHP_CodeSniffer` (3.10.1) and `PHPCompatibility` (96072c30).
- The `moodle.Commenting.MissingDocblock` sniff will now detect use of the Override attribute (Fixes #155).

### Fixed
- Various fixes to own (GH workflow) integration tests.

## [v3.4.6] - 2024-04-03
### Fixed
- Solved a problem where Windows file paths were not normalised leading to false positive results on some path-based sniffs.
Expand Down Expand Up @@ -210,7 +222,8 @@ All features are maintained and no new features have been introduced to either t

All the details about [previous releases] can be found in [local_codechecker](https://github.com/moodlehq/moodle-local_codechecker) own change log.

[Unreleased]: https://github.com/moodlehq/moodle-cs/compare/v3.4.6...main
[Unreleased]: https://github.com/moodlehq/moodle-cs/compare/v3.4.7...main
[v3.4.7]: https://github.com/moodlehq/moodle-cs/compare/v3.4.6...v3.4.7
[v3.4.6]: https://github.com/moodlehq/moodle-cs/compare/v3.4.5...v3.4.6
[v3.4.5]: https://github.com/moodlehq/moodle-cs/compare/v3.4.4...v3.4.5
[v3.4.4]: https://github.com/moodlehq/moodle-cs/compare/v3.4.3...v3.4.4
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"php": ">=7.4.0",
"ext-json": "*",
"dealerdirect/phpcodesniffer-composer-installer": "^1.0.0",
"squizlabs/php_codesniffer": "^3.9.0",
"squizlabs/php_codesniffer": "^3.10.1",
"phpcsstandards/phpcsextra": "^1.2.1",
"phpcompatibility/php-compatibility": "dev-develop#e5cd2e24"
"phpcompatibility/php-compatibility": "dev-develop#96072c30"
},
"config": {
"allow-plugins": {
Expand Down
120 changes: 120 additions & 0 deletions moodle/Sniffs/Commenting/ConstructorReturnSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

// This file is part of Moodle - https://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 WARRANdTY; 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 <https://www.gnu.org/licenses/>.

namespace MoodleHQ\MoodleCS\moodle\Sniffs\Commenting;

use MoodleHQ\MoodleCS\moodle\Util\Docblocks;
use MoodleHQ\MoodleCS\moodle\Util\TokenUtil;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

/**
* Checks that all files an classes have appropriate docs.
*
* @copyright 2024 Andrew Lyons <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class ConstructorReturnSniff implements Sniff
{
/**
* Register for class tags.
*/
public function register() {

return [
T_CLASS,
];
}

/**
* Processes php files and perform various checks with file.
*
* @param File $phpcsFile The file being scanned.
* @param int $stackPtr The position in the stack.
*/
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
$endClassPtr = $tokens[$stackPtr]['scope_closer'];

while (
($methodPtr = $phpcsFile->findNext(T_FUNCTION, $stackPtr + 1, $endClassPtr)) !== false
) {
$this->processClassMethod($phpcsFile, $methodPtr);
$stackPtr = $methodPtr;
}
}

/**
* Processes the class method.
*
* @param File $phpcsFile The file being scanned.
* @param int $stackPtr The position in the stack.
*/
protected function processClassMethod(File $phpcsFile, int $stackPtr): void {
$objectName = TokenUtil::getObjectName($phpcsFile, $stackPtr);
if ($objectName !== '__constructor') {
// We only care about constructors.
return;
}

// Get docblock.
$docblockPtr = Docblocks::getDocBlockPointer($phpcsFile, $stackPtr);
if ($docblockPtr === null) {
// No docblocks for this constructor.
return;
}

$returnTokens = Docblocks::getMatchingDocTags($phpcsFile, $docblockPtr, '@return');
if (count($returnTokens) === 0) {
// No @return tag in the docblock.
return;
}

$fix = $phpcsFile->addFixableError(
'Constructor should not have a return tag in the docblock',
$returnTokens[0],
'ConstructorReturn'
);
if ($fix) {
$tokens = $phpcsFile->getTokens();
$phpcsFile->fixer->beginChangeset();

// Find the tokens at the start and end of the line.
$lineStart = $phpcsFile->findFirstOnLine(T_DOC_COMMENT_STAR, $returnTokens[0]);
if ($lineStart === false) {
$lineStart = $returnTokens[0];
}

$ptr = $phpcsFile->findNext(T_DOC_COMMENT_WHITESPACE, $lineStart);
for ($lineEnd = $lineStart; $lineEnd < $tokens[$docblockPtr]['comment_closer']; $lineEnd++) {
if ($tokens[$lineEnd]['line'] !== $tokens[$lineStart]['line']) {
break;
}
}

if ($tokens[$lineEnd]['code'] === T_DOC_COMMENT_CLOSE_TAG) {
$lineEnd--;
}

for ($ptr = $lineStart; $ptr <= $lineEnd; $ptr++) {
$phpcsFile->fixer->replaceToken($ptr, '');
}

$phpcsFile->fixer->endChangeset();
}
}
}
14 changes: 12 additions & 2 deletions moodle/Sniffs/Commenting/MissingDocblockSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace MoodleHQ\MoodleCS\moodle\Sniffs\Commenting;

use MoodleHQ\MoodleCS\moodle\Util\Attributes;
use MoodleHQ\MoodleCS\moodle\Util\Docblocks;
use MoodleHQ\MoodleCS\moodle\Util\MoodleUtil;
use MoodleHQ\MoodleCS\moodle\Util\TokenUtil;
Expand Down Expand Up @@ -183,6 +184,17 @@ protected function processFunctions(File $phpcsFile, int $stackPtr): void {

foreach ($missingDocblocks as $typePtr => $extendsOrImplements) {
$token = $tokens[$typePtr];
if ($extendsOrImplements) {
$attributes = Attributes::getAttributePointers($phpcsFile, $typePtr);
foreach ($attributes as $attributePtr) {
$attribute = Attributes::getAttributeProperties($phpcsFile, $attributePtr);
if ($attribute['attribute_name'] === '\Override') {
// Skip methods that are marked as overrides.
continue 2;
}
}
}

$objectName = TokenUtil::getObjectName($phpcsFile, $typePtr);
$objectType = TokenUtil::getObjectType($phpcsFile, $typePtr);

Expand All @@ -195,8 +207,6 @@ protected function processFunctions(File $phpcsFile, int $stackPtr): void {
[$objectType, $objectName]
);
}
} elseif ($extendsOrImplements) {
$phpcsFile->addWarning('Missing docblock for %s %s', $typePtr, 'Missing', [$objectType, $objectName]);
} else {
$phpcsFile->addError('Missing docblock for %s %s', $typePtr, 'Missing', [$objectType, $objectName]);
}
Expand Down
Loading

0 comments on commit 0ce282e

Please sign in to comment.