MDL-19247 Minor adjustments to Moodle Coding Standard, mostly switching Errors to Warnings. Added a file to sniff out elseif declarations. Cleaned up grade/lib.php as an example (no more errors).

This commit is contained in:
nicolasconnault
2009-05-19 21:17:54 +00:00
parent 2a8435c4ce
commit cf72e2dde3
11 changed files with 582 additions and 236 deletions
+477 -216
View File
File diff suppressed because it is too large Load Diff
@@ -80,7 +80,7 @@ class Moodle_Sniffs_Commenting_ClassCommentSniff extends Moodle_Sniffs_Commentin
// Modify array of required tags
$this->tags['package']['required'] = false;
$this->tags['copyright']['required'] = false;
$this->tags['author']['required'] = true;
$this->tags['author']['required'] = false;
$this->currentFile = $phpcsFile;
@@ -183,7 +183,7 @@ class Moodle_Sniffs_Commenting_ClassCommentSniff extends Moodle_Sniffs_Commentin
$newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1);
}
$phpcsFile->addError($error, ($commentStart + $newlineCount));
$phpcsFile->addWarning($error, ($commentStart + $newlineCount));
$short = rtrim($short, $phpcsFile->eolChar.' ');
}
}
@@ -280,8 +280,8 @@ class Moodle_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff
$between = $comment->getWhiteSpaceBetween();
$newlineBetween = substr_count($between, $phpcsFile->eolChar);
if ($newlineBetween !== 2) {
$error = 'There must be exactly one blank line between descriptions in file comment';
$phpcsFile->addError($error, ($filedocToken + $newlineCount + 1));
$error = 'There should be exactly one blank line between descriptions in file comment';
$phpcsFile->addWarning($error, ($filedocToken + $newlineCount + 1));
}
$newlineCount += $newlineBetween;
@@ -297,7 +297,7 @@ class Moodle_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff
$newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1);
}
$phpcsFile->addError($error, ($filedocToken + $newlineCount));
$phpcsFile->addWarning($error, ($filedocToken + $newlineCount));
$short = rtrim($short, $phpcsFile->eolChar.' ');
}
}
@@ -448,7 +448,7 @@ class Moodle_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff
$line = $tagElem->getLine();
}
$this->currentFile->addError($error, ($commentStart + $line));
$this->currentFile->addWarning($error, ($commentStart + $line));
}
}
@@ -229,7 +229,7 @@ class Moodle_Sniffs_Commenting_FunctionCommentSniff implements PHP_CodeSniffer_S
$newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1);
}
$phpcsFile->addError($error, ($commentStart + $newlineCount));
$phpcsFile->addWarning($error, ($commentStart + $newlineCount));
$short = rtrim($short, $phpcsFile->eolChar.' ');
}
}
@@ -321,7 +321,7 @@ class Moodle_Sniffs_Commenting_FunctionCommentSniff implements PHP_CodeSniffer_S
if (substr_count($params[$lastParm]->getWhitespaceAfter(), $this->currentFile->eolChar) !== 2) {
$error = 'Last parameter comment requires a blank newline after it';
$errorPos = ($params[$lastParm]->getLine() + $commentStart);
$this->currentFile->addError($error, $errorPos);
$this->currentFile->addWarning($error, $errorPos);
}
// Parameters must appear immediately after the comment.
@@ -345,7 +345,7 @@ class Moodle_Sniffs_Commenting_FunctionCommentSniff implements PHP_CodeSniffer_S
// Make sure that there is only one space before the var type.
if ($param->getWhitespaceBeforeType() !== ' ') {
$error = 'Expected 1 space before variable type';
$this->currentFile->addError($error, $errorPos);
$this->currentFile->addWarning($error, $errorPos);
}
$spaceCount = substr_count($param->getWhitespaceBeforeVarName(), ' ');
@@ -373,12 +373,12 @@ class Moodle_Sniffs_Commenting_FunctionCommentSniff implements PHP_CodeSniffer_S
// Check to see if the parameters align properly.
if ($param->alignsVariableWith($previousParam) === false) {
$error = 'The variable names for parameters '.$previousName.' ('.($pos - 1).') and '.$paramName.' ('.$pos.') do not align';
$this->currentFile->addError($error, $errorPos);
$this->currentFile->addWarning($error, $errorPos);
}
if ($param->alignsCommentWith($previousParam) === false) {
$error = 'The comments for parameters '.$previousName.' ('.($pos - 1).') and '.$paramName.' ('.$pos.') do not align';
$this->currentFile->addError($error, $errorPos);
$this->currentFile->addWarning($error, $errorPos);
}
}//end if
@@ -58,7 +58,6 @@ class Moodle_Sniffs_ControlStructures_ControlSignatureSniff extends PHP_CodeSnif
'if (...) {EOL',
'foreach (...) {EOL',
'} else if (...) {EOL',
'} elseif (...) {EOL',
'} else {EOL',
'do {EOL',
);
@@ -0,0 +1,68 @@
<?php
/**
* Moodle_Sniffs_ControlStructures_ElseIfDeclarationSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <[email protected]>
* @author Marc McIntyre <[email protected]>
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version CVS: $Id$
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Moodle_Sniffs_ControlStructures_ElseIfDeclarationSniff.
*
* Verifies that there are not elseif statements. The else and the if should
* be separated by a space.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <[email protected]>
* @author Marc McIntyre <[email protected]>
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.1.0
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Moodle_Sniffs_ControlStructures_ElseIfDeclarationSniff implements PHP_CodeSniffer_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(T_ELSEIF);
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$error = 'Usage of ELSEIF not allowed. Use ELSE IF instead.';
$phpcsFile->addError($error, $stackPtr);
}//end process()
}//end class
?>
@@ -100,12 +100,26 @@ class Moodle_Sniffs_NamingConventions_ValidVariableNameSniff extends PHP_CodeSni
protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$memberName = ltrim($tokens[$stackPtr]['content'], '$');
if (preg_match('/[A-Z]+/', $memberName)) {
$error = "Member variable \"$memberName\" must be all lower-case";
$phpcsFile->addError($error, $stackPtr);
return;
if (preg_match('/\$([A-Za-z0-9_]+)(\-\>([A-Za-z0-9_]+))?/i', $tokens[$stackPtr]['content'], $matches)) {
$firstvar = $matches[1];
$objectvar = (empty($matches[3])) ? null : $matches[3];
$memberName = $firstvar . $objectvar;
if (preg_match('/[A-Z]+/', $firstvar, $matches)) {
if (!in_array($firstvar, $this->allowed_global_vars)) {
$error = "Member variable \"$firstvar\" must be all lower-case";
$phpcsFile->addError($error, $stackPtr);
return;
}
}
if (!empty($objectvar) && preg_match('/[A-Z]+/', $objectvar, $matches)) {
$error = "Member variable \"$objectvar\" must be all lower-case";
$phpcsFile->addError($error, $stackPtr);
return;
}
}
return;
}//end processVariableInString()
@@ -99,7 +99,7 @@ class Moodle_Sniffs_Strings_DoubleQuoteUsageSniff implements PHP_CodeSniffer_Sni
}
$error = "String $workingString does not require double quotes; use single quotes instead";
$phpcsFile->addError($error, $stackPtr);
$phpcsFile->addWarning($error, $stackPtr);
}//end process()
@@ -78,7 +78,7 @@ class Moodle_Sniffs_WhiteSpace_MemberVarSpacingSniff extends PHP_CodeSniffer_Sta
}//end if
if ($foundLines !== 1) {
$phpcsFile->addError("Expected 1 blank line before member var; $foundLines found", $stackPtr);
// $phpcsFile->addError("Expected 1 blank line before member var; $foundLines found", $stackPtr);
}
}//end processMemberVar()
@@ -101,6 +101,10 @@ class Moodle_Sniffs_WhiteSpace_ScopeClosingBraceSniff implements PHP_CodeSniffer
$phpcsFile->addError($error, $scopeEnd);
}
} else {
if (in_array($tokens[$stackPtr]['code'], array(T_CASE, T_DEFAULT))) {
$startColumn -= 4;
}
if ($braceIndent !== $startColumn) {
$error = 'Closing brace indented incorrectly; expected '.($startColumn - 1).' spaces, found '.($braceIndent - 1);
$phpcsFile->addError($error, $scopeEnd);
@@ -55,7 +55,7 @@ class Moodle_Sniffs_WhiteSpace_ScopeIndentSniff implements PHP_CodeSniffer_Sniff
*
* @var array(int)
*/
protected $nonIndentingScopes = array(T_SWITCH);
protected $nonIndentingScopes = array();
/**