Skip to content

Commit

Permalink
Make the whole codebase compliant with our configuration
Browse files Browse the repository at this point in the history
Apart from fixing all the errors and warnings, this
commit also:
- Changes all http occurrences in comments to https.
- Removes all the @Package and @category tags, not much useful.
- Removes all the phpcs:disable moodle.NamingConventions, not needed.

TODO: On purpose, to be able to go testing everything while advancing
with this commit, I've not changed our testing API methods on
purpose (MoodleCSBaseTestCase). That comes in the next commit.
  • Loading branch information
stronk7 committed Feb 24, 2024
1 parent a50b294 commit 1c12b33
Show file tree
Hide file tree
Showing 57 changed files with 1,008 additions and 964 deletions.
8 changes: 4 additions & 4 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.

Expand Down Expand Up @@ -645,7 +645,7 @@ the "copyright" line and a pointer to where the full notice is found.
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
along with this program. If not, see <https://www.gnu.org/licenses/>.

Also add information on how to contact you by electronic and paper mail.

Expand All @@ -664,11 +664,11 @@ might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<http://www.gnu.org/licenses/>.
<https://www.gnu.org/licenses/>.

The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
<https://www.gnu.org/philosophy/why-not-lgpl.html>.
13 changes: 6 additions & 7 deletions moodle/Sniffs/Commenting/CategorySniff.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
// This file is part of Moodle - http://moodle.org/

// 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
Expand All @@ -12,12 +13,10 @@
// 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/>.
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.

namespace MoodleHQ\MoodleCS\moodle\Sniffs\Commenting;

// phpcs:disable moodle.NamingConventions

use MoodleHQ\MoodleCS\moodle\Util\MoodleUtil;
use MoodleHQ\MoodleCS\moodle\Util\Docblocks;
use PHP_CodeSniffer\Sniffs\Sniff;
Expand All @@ -27,10 +26,10 @@
* Checks that all test classes and global functions have appropriate @package tags.
*
* @copyright 2024 Andrew Lyons <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class CategorySniff implements Sniff {

class CategorySniff implements Sniff
{
/**
* Register for open tag (only process once per file).
*/
Expand Down
103 changes: 52 additions & 51 deletions moodle/Sniffs/Commenting/InlineCommentSniff.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
// This file is part of Moodle - http://moodle.org/

// 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
Expand All @@ -12,17 +13,16 @@
// 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/>.
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.

/**
* Verifies that inline comments conform to their coding standards.
*
* Based on {@see PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\InlineCommentSniff}
* with some customizations to suit our very personal rules.
*
* @package local_codechecker
* @copyright 2012 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright 2012 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com}
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
Expand All @@ -35,14 +35,12 @@

namespace MoodleHQ\MoodleCS\moodle\Sniffs\Commenting;

// phpcs:disable moodle.NamingConventions

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

class InlineCommentSniff implements Sniff {

class InlineCommentSniff implements Sniff
{
/**
* A list of tokenizers this sniff supports.
*
Expand All @@ -64,9 +62,7 @@ public function register() {
T_COMMENT,
T_DOC_COMMENT_OPEN_TAG,
];

}//end register()

}

/**
* Processes this test, when one of its tokens is encountered.
Expand Down Expand Up @@ -131,12 +127,12 @@ public function process(File $phpcsFile, $stackPtr) {

// 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)) {
if ($this->isReturnNewClassExtends($phpcsFile, $stackPtr)) {
return;
}

// Allow phpdoc before define() token (see CONTRIB-4150).
if ($tokens[$nextToken]['code'] == T_STRING and $tokens[$nextToken]['content'] == 'define') {
if ($tokens[$nextToken]['code'] == T_STRING && $tokens[$nextToken]['content'] == 'define') {
return;
}

Expand All @@ -148,10 +144,11 @@ public function process(File $phpcsFile, $stackPtr) {
$ignore[] = T_STRING;
$ignore[] = T_OBJECT_OPERATOR;
$nextToken = $phpcsFile->findNext($ignore, ($nextToken + 1), null, true);
if ($tokens[$nextToken]['code'] === T_FUNCTION
|| $tokens[$nextToken]['code'] === T_CLOSURE
|| $tokens[$nextToken]['code'] === T_OBJECT
|| $tokens[$nextToken]['code'] === T_PROTOTYPE
if (
$tokens[$nextToken]['code'] === T_FUNCTION ||
$tokens[$nextToken]['code'] === T_CLOSURE ||
$tokens[$nextToken]['code'] === T_OBJECT ||
$tokens[$nextToken]['code'] === T_PROTOTYPE
) {
return;
}
Expand All @@ -176,8 +173,10 @@ public function process(File $phpcsFile, $stackPtr) {
true
);
// Is it a @var tag in the comment?
if ($tokens[$nextToken]['code'] === T_DOC_COMMENT_TAG &&
$tokens[$nextToken]['content'] == '@var') {
if (
$tokens[$nextToken]['code'] === T_DOC_COMMENT_TAG &&
$tokens[$nextToken]['content'] == '@var'
) {
$nextToken = $phpcsFile->findNext(
T_DOC_COMMENT_WHITESPACE,
($nextToken + 1),
Expand Down Expand Up @@ -221,10 +220,10 @@ public function process(File $phpcsFile, $stackPtr) {
if (!$nextToken) {
// Not valid type-hinting, specialised error.
$error = 'Inline doc block type-hinting for \'%s\' does not match next list() variables';
$data = array($foundvar);
$data = [$foundvar];
$phpcsFile->addError($error, $stackPtr, 'TypeHintingList', $data);
}
} else if ($tokens[$nextToken]['code'] === T_FOREACH) {
} elseif ($tokens[$nextToken]['code'] === T_FOREACH) {
// Let's look within the foreach if the variable appear after the 'as' token.
$astoken = $phpcsFile->findNext(
T_AS,
Expand All @@ -237,13 +236,13 @@ public function process(File $phpcsFile, $stackPtr) {
if ($tokens[$variabletoken]['content'] !== $foundvar) {
// Not valid type-hinting, specialised error.
$error = 'Inline doc block type-hinting for \'%s\' does not match next foreach() as variable';
$data = array($foundvar, $tokens[$nextToken]['content']);
$data = [$foundvar, $tokens[$nextToken]['content']];
$phpcsFile->addError($error, $stackPtr, 'TypeHintingForeach', $data);
}
} else if ($tokens[$nextToken]['content'] !== $foundvar) {
} elseif ($tokens[$nextToken]['content'] !== $foundvar) {
// Not valid type-hinting, specialised error.
$error = 'Inline doc block type-hinting for \'%s\' does not match next code line \'%s...\'';
$data = array($foundvar, $tokens[$nextToken]['content']);
$data = [$foundvar, $tokens[$nextToken]['content']];
$phpcsFile->addError($error, $stackPtr, 'TypeHintingMatch', $data);
}
return; // Have finished.
Expand All @@ -255,7 +254,7 @@ public function process(File $phpcsFile, $stackPtr) {
$error = 'Inline doc block comments are not allowed; use "// Comment." instead';
$phpcsFile->addError($error, $stackPtr, 'DocBlock');
}
}//end if
}

if ($tokens[$stackPtr]['content'][0] === '#') {
$error = 'Perl-style comments are not allowed; use "// Comment." instead';
Expand All @@ -275,8 +274,9 @@ public function process(File $phpcsFile, $stackPtr) {
}

// Special case for JS files.
if ($tokens[$previousContent]['code'] === T_COMMA
|| $tokens[$previousContent]['code'] === T_SEMICOLON
if (
$tokens[$previousContent]['code'] === T_COMMA ||
$tokens[$previousContent]['code'] === T_SEMICOLON
) {
$lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($previousContent - 1), null, true);
if ($tokens[$lastContent]['code'] === T_CLOSE_CURLY_BRACKET) {
Expand All @@ -301,14 +301,14 @@ public function process(File $phpcsFile, $stackPtr) {
$error = 'Comment separators are not allowed to contain other chars buy hyphens (-). Found: (%s)';
// Basic clean dupes for notification.
$wrongcharsfound = implode(array_keys(array_flip(preg_split('//', $wrongcharsfound, -1, PREG_SPLIT_NO_EMPTY))));
$data = array($wrongcharsfound);
$data = [$wrongcharsfound];
$phpcsFile->addWarning($error, $stackPtr, 'IncorrectCommentSeparator', $data);
}
// Verify length between 20 and 120.
$hyphencount = strlen($matches[1] . $matches[2] . $matches[3]);
if ($hyphencount < 20 or $hyphencount > 120) {
if ($hyphencount < 20 || $hyphencount > 120) {
$error = 'Comment separators length must contain 20-120 chars, %s found';
$phpcsFile->addWarning($error, $stackPtr, 'WrongCommentSeparatorLength', array($hyphencount));
$phpcsFile->addWarning($error, $stackPtr, 'WrongCommentSeparatorLength', [$hyphencount]);
}
// Verify it's the first token in the line.
$prevToken = $phpcsFile->findPrevious(
Expand All @@ -317,9 +317,9 @@ public function process(File $phpcsFile, $stackPtr) {
null,
true
);
if (!empty($prevToken) and $tokens[$prevToken]['line'] == $tokens[$stackPtr]['line']) {
if (!empty($prevToken) && $tokens[$prevToken]['line'] == $tokens[$stackPtr]['line']) {
$error = 'Comment separators must be the unique text in the line, code found before';
$phpcsFile->addWarning($error, $stackPtr, 'WrongCommentCodeFoundBefore', array());
$phpcsFile->addWarning($error, $stackPtr, 'WrongCommentCodeFoundBefore', []);
}
// Don't want to continue processing the comment separator.
return;
Expand Down Expand Up @@ -349,7 +349,7 @@ public function process(File $phpcsFile, $stackPtr) {

$commentTokens[] = $nextComment;
$lastComment = $nextComment;
}//end while
}

$commentText = '';
foreach ($commentTokens as $lastCommentToken) {
Expand All @@ -360,7 +360,7 @@ public function process(File $phpcsFile, $stackPtr) {

if ($slashCount > 2) {
$error = '%s slashes comments are not allowed; use "// Comment." instead';
$data = array($slashCount);
$data = [$slashCount];
$phpcsFile->addError($error, $lastCommentToken, 'WrongStyle', $data);
}

Expand Down Expand Up @@ -393,30 +393,30 @@ public function process(File $phpcsFile, $stackPtr) {
$comment,
];
$fix = $phpcsFile->addFixableError($error, $lastCommentToken, 'TabBefore', $data);
} else if ($spaceCount === 0) {
} elseif ($spaceCount === 0) {
$error = 'No space found before comment text; expected "// %s" but found "%s"';
$data = [
substr($comment, $slashCount),
$comment,
];
$fix = $phpcsFile->addFixableError($error, $lastCommentToken, 'NoSpaceBefore', $data);
} else if ($spaceCount > 1) {
} elseif ($spaceCount > 1) {
$error = 'Expected 1 space before comment text but found %s; use block comment if you need indentation';
$data = [
$spaceCount,
substr($comment, ($slashCount + $spaceCount)),
$comment,
];
$fix = $phpcsFile->addFixableError($error, $lastCommentToken, 'SpacingBefore', $data);
}//end if
}

if ($fix === true) {
$newComment = '// '.ltrim($tokens[$lastCommentToken]['content'], "/\t ");
$newComment = '// ' . ltrim($tokens[$lastCommentToken]['content'], "/\t ");
$phpcsFile->fixer->replaceToken($lastCommentToken, $newComment);
}

$commentText .= trim(substr($tokens[$lastCommentToken]['content'], $slashCount));
}//end foreach
}

if ($commentText === '') {
$error = 'Blank comments are not allowed';
Expand Down Expand Up @@ -454,7 +454,7 @@ public function process(File $phpcsFile, $stackPtr) {
$error = 'Inline comments must end in %s';
$ender = '';
foreach ($acceptedClosers as $closerName => $symbol) {
$ender .= ' '.$closerName.',';
$ender .= ' ' . $closerName . ',';
}

$ender = trim($ender, ' ,');
Expand Down Expand Up @@ -500,8 +500,11 @@ public function process(File $phpcsFile, $stackPtr) {
$type = end($conditions);
$conditionPtr = key($conditions);

if (($type === T_FUNCTION || $type === T_CLOSURE)
&& $tokens[$conditionPtr]['scope_closer'] === $next
if (
(
$type === T_FUNCTION ||
$type === T_CLOSURE
) && $tokens[$conditionPtr]['scope_closer'] === $next
) {
$errorCode = 'SpacingAfterAtFunctionEnd';
}
Expand All @@ -512,7 +515,7 @@ public function process(File $phpcsFile, $stackPtr) {
if ($tokens[$i]['code'] !== T_WHITESPACE) {
return ($lastCommentToken + 1);
}
} else if ($tokens[$i]['line'] > ($tokens[$lastCommentToken]['line'] + 1)) {
} elseif ($tokens[$i]['line'] > ($tokens[$lastCommentToken]['line'] + 1)) {
break;
}
}
Expand All @@ -531,11 +534,10 @@ public function process(File $phpcsFile, $stackPtr) {

$phpcsFile->fixer->endChangeset();
}
}//end if
}

return ($lastCommentToken + 1);

}//end process()
}

/**
* This looks if there is a valid "return new class extends" expression allowed to have phpdoc block.
Expand All @@ -544,7 +546,7 @@ public function process(File $phpcsFile, $stackPtr) {
* @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) {
protected function isReturnNewClassExtends(File $file, $pointer) {

$ignoredtokens = Tokens::$emptyTokens;

Expand Down Expand Up @@ -576,6 +578,5 @@ protected function is_return_new_class_extends(File $file, $pointer) {

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

}//end class
}
}
Loading

0 comments on commit 1c12b33

Please sign in to comment.