* Simple modifications to the CLI class to only use the Moodle Standard
*
* @package lib-pear-php-codesniffer
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-include_once('PHP/MoodleCodeSniffer.php');
+require_once('PHP/MoodleCodeSniffer.php');
require_once('PHP/CodeSniffer/CLI.php');
/**
* @return array The updated CLI values.
* @see getCommandLineValues()
*/
- public function processUnknownArgument($arg, $pos, $values) {
+ public function processunknownargument($arg, $pos, $values) {
global $args, $argv, $argc;
// We don't know about any additional switches; just files.
}
$file = $_SERVER['PWD'] . '/' . $arg;
+
if (file_exists($file) === false) {
echo 'ERROR: The file "'.$arg.'" does not exist.'.PHP_EOL.PHP_EOL;
$this->printUsage();
*
* Simple modifications to the CLI class to only use the Moodle Standard
*
- * @package lib-pear-php-codesniffer
+ * @package lib-pear-php-codesniffer-standards-moodle
* @copyright 2008 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-
if (class_exists('PHP_CodeSniffer_Standards_CodingStandard', true) === false) {
throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_CodingStandard not found');
}
$tokens = $phpcsfile->gettokens();
if (isset($tokens[$stackptr]['scope_opener']) === false) {
- $error = 'Possible parse error: ';
+ $error = 'possible parse error: ';
$error .= $tokens[$stackptr]['content'];
$error .= ' missing opening or closing brace';
$phpcsfile->addwarning($error, $stackptr);
}
$params = array();
- foreach ($phpcsfile->getMethodParameters($stackptr) as $param) {
+ foreach ($phpcsfile->getmethodParameters($stackptr) as $param) {
$params[$param['name']] = $stackptr;
}
}
if ($emptyBody === false && count($params) > 0) {
- foreach ($params as $paramName => $position) {
- $error = 'The method parameter '.$paramName.' is never used';
+ foreach ($params as $paramname => $position) {
+ $error = 'The method parameter '.$paramname.' is never used';
$phpcsfile->addwarning($error, $position);
}
}
}
// Get function name.
- $methodName = $phpcsfile->getDeclarationName($stackptr);
+ $methodname = $phpcsfile->getDeclarationname($stackptr);
// Get all parameters from method signature.
$signature = array();
- foreach ($phpcsfile->getMethodParameters($stackptr) as $param) {
+ foreach ($phpcsfile->getmethodParameters($stackptr) as $param) {
$signature[] = $param['name'];
}
$next = $phpcsfile->findNext(PHP_CodeSniffer_tokens::$emptyTokens, ($next + 1), null, true);
// Skip for invalid code or other method.
- if ($next === false || $tokens[$next]['content'] !== $methodName) {
+ if ($next === false || $tokens[$next]['content'] !== $methodname) {
return;
}
);
$parameters = array('');
- $parenthesisCount = 1;
+ $parenthesiscount = 1;
$count = count($tokens);
for (++$next; $next < $count; ++$next) {
$code = $tokens[$next]['code'];
if ($code === T_OPEN_PARENTHESIS) {
- ++$parenthesisCount;
+ ++$parenthesiscount;
} else if ($code === T_CLOSE_PARENTHESIS) {
- --$parenthesisCount;
- } else if ($parenthesisCount === 1 && $code === T_COMMA) {
+ --$parenthesiscount;
+ } else if ($parenthesiscount === 1 && $code === T_COMMA) {
$parameters[] = '';
} else if (in_array($code, PHP_CodeSniffer_tokens::$emptyTokens) === false) {
$parameters[(count($parameters) - 1)] .= $tokens[$next]['content'];
}
- if ($parenthesisCount === 0) {
+ if ($parenthesiscount === 0) {
break;
}
}
);
// Extract the class comment docblock.
- $commentEnd = $phpcsfile->findPrevious($find, ($stackptr - 1), null, true);
+ $commentend = $phpcsfile->findPrevious($find, ($stackptr - 1), null, true);
- if ($commentEnd !== false && $tokens[$commentEnd]['code'] === T_COMMENT) {
+ if ($commentend !== false && $tokens[$commentend]['code'] === T_COMMENT) {
$phpcsfile->adderror("You must use \"/**\" style comments for a $type comment", $stackptr);
return;
- } else if ($commentEnd === false || $tokens[$commentEnd]['code'] !== T_DOC_COMMENT) {
+
+ } else if ($commentend === false || $tokens[$commentend]['code'] !== T_DOC_COMMENT) {
$phpcsfile->adderror("Missing $type doc comment", $stackptr);
return;
}
- $commentStart = ($phpcsfile->findPrevious(T_DOC_COMMENT, ($commentEnd - 1), null, true) + 1);
- $commentNext = $phpcsfile->findPrevious(T_WHITESPACE, ($commentEnd + 1), $stackptr, false, $phpcsfile->eolChar);
+ $commentstart = ($phpcsfile->findPrevious(T_DOC_COMMENT, ($commentend - 1), null, true) + 1);
+ $commentnext = $phpcsfile->findPrevious(T_WHITESPACE, ($commentend + 1), $stackptr, false, $phpcsfile->eolChar);
// Distinguish file and class comment.
- $prevClasstoken = $phpcsfile->findPrevious(T_CLASS, ($stackptr - 1));
- if ($prevClasstoken === false) {
+ $prevclasstoken = $phpcsfile->findPrevious(T_CLASS, ($stackptr - 1));
+
+ if ($prevclasstoken === false) {
// This is the first class token in this file, need extra checks.
- $prevNonComment = $phpcsfile->findPrevious(T_DOC_COMMENT, ($commentStart - 1), null, true);
- if ($prevNonComment !== false) {
- $prevComment = $phpcsfile->findPrevious(T_DOC_COMMENT, ($prevNonComment - 1));
- if ($prevComment === false) {
+ $prevnoncomment = $phpcsfile->findPrevious(T_DOC_COMMENT, ($commentstart - 1), null, true);
+
+ if ($prevnoncomment !== false) {
+ $prevcomment = $phpcsfile->findPrevious(T_DOC_COMMENT, ($prevnoncomment - 1));
+
+ if ($prevcomment === false) {
// There is only 1 doc comment between open tag and class token.
- $newlinetoken = $phpcsfile->findNext(T_WHITESPACE, ($commentEnd + 1), $stackptr, false, $phpcsfile->eolChar);
+ $newlinetoken = $phpcsfile->findNext(T_WHITESPACE, ($commentend + 1), $stackptr, false, $phpcsfile->eolChar);
+
if ($newlinetoken !== false) {
$newlinetoken = $phpcsfile->findNext(T_WHITESPACE, ($newlinetoken + 1), $stackptr, false, $phpcsfile->eolChar);
+
if ($newlinetoken !== false) {
// Blank line between the class and the doc block.
// The doc block is most likely a file comment.
}
}
- $comment = $phpcsfile->gettokensAsString($commentStart, ($commentEnd - $commentStart + 1));
+ $comment = $phpcsfile->gettokensAsString($commentstart, ($commentend - $commentstart + 1));
// Parse the class comment.docblock.
try {
$this->commentparser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsfile);
$this->commentparser->parse();
} catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
- $line = ($e->getlinewithinComment() + $commentStart);
+ $line = ($e->getlinewithinComment() + $commentstart);
$phpcsfile->adderror($e->getMessage(), $line);
return;
}
$comment = $this->commentparser->getComment();
+
if (is_null($comment) === true) {
$error = ucfirst($type).' doc comment is empty';
- $phpcsfile->adderror($error, $commentStart);
+ $phpcsfile->adderror($error, $commentstart);
return;
}
// No extra newline before short description.
$short = $comment->getShortComment();
- $newlineCount = 0;
- $newlineSpan = strspn($short, $phpcsfile->eolChar);
- if ($short !== '' && $newlineSpan > 0) {
- $line = ($newlineSpan > 1) ? 'newlines' : 'newline';
+ $newlinecount = 0;
+ $newlinespan = strspn($short, $phpcsfile->eolChar);
+
+ if ($short !== '' && $newlinespan > 0) {
+ $line = ($newlinespan > 1) ? 'newlines' : 'newline';
$error = "Extra $line found before $type comment short description";
- $phpcsfile->adderror($error, ($commentStart + 1));
+ $phpcsfile->adderror($error, ($commentstart + 1));
}
- $newlineCount = (substr_count($short, $phpcsfile->eolChar) + 1);
+ $newlinecount = (substr_count($short, $phpcsfile->eolChar) + 1);
// Exactly one blank line between short and long description.
$long = $comment->getlongcomment();
+
if (empty($long) === false) {
$between = $comment->getWhiteSpaceBetween();
- $newlineBetween = substr_count($between, $phpcsfile->eolChar);
- if ($newlineBetween !== 2) {
+ $newlinebetween = substr_count($between, $phpcsfile->eolChar);
+
+ if ($newlinebetween !== 2) {
$error = "There must be exactly one blank line between descriptions in $type comments";
- $phpcsfile->adderror($error, ($commentStart + $newlineCount + 1));
+ $phpcsfile->adderror($error, ($commentstart + $newlinecount + 1));
}
- $newlineCount += $newlineBetween;
+ $newlinecount += $newlinebetween;
}
// Exactly one blank line before tags.
- $tags = $this->commentparser->getTagOrders();
+ $tags = $this->commentparser->gettagOrders();
+
if (count($tags) > 1) {
- $newlineSpan = $comment->getNewlineAfter();
- if ($newlineSpan !== 2) {
+ $newlinespan = $comment->getNewlineAfter();
+
+ if ($newlinespan !== 2) {
$error = "There must be exactly one blank line before the tags in $type comments";
+
if ($long !== '') {
- $newlineCount += (substr_count($long, $phpcsfile->eolChar) - $newlineSpan + 1);
+ $newlinecount += (substr_count($long, $phpcsfile->eolChar) - $newlinespan + 1);
}
- $phpcsfile->addwarning($error, ($commentStart + $newlineCount));
+ $phpcsfile->addwarning($error, ($commentstart + $newlinecount));
$short = rtrim($short, $phpcsfile->eolChar.' ');
}
}
// Check each tag.
- $this->processTags($commentStart, $commentEnd);
+ $this->processtags($commentstart, $commentend);
}
/**
* Process the version tag.
*
- * @param int $errorPos The line number where the error occurs.
+ * @param int $errorpos The line number where the error occurs.
*
* @return void
*/
- protected function processversion($errorPos)
- {
+ protected function processversion($errorpos) {
$version = $this->commentparser->getVersion();
+
if ($version !== null) {
$content = $version->getcontent();
$matches = array();
+
if (empty($content) === true) {
$error = 'content missing for @version tag in doc comment';
- $this->currentfile->adderror($error, $errorPos);
+ $this->currentfile->adderror($error, $errorpos);
+
} else if ((strstr($content, 'Release:') === false)) {
$error = "Invalid version \"$content\" in doc comment; consider \"Release: <package_version>\" instead";
- $this->currentfile->addwarning($error, $errorPos);
+ $this->currentfile->addwarning($error, $errorpos);
}
}
}
}
-
-?>
*
* @return array
*/
- public function register()
- {
+ public function register() {
return array(T_OPEN_TAG);
-
}
// We are only interested if this is the first open tag.
if ($stackptr !== 0) {
+
if ($phpcsfile->findPrevious(T_OPEN_TAG, ($stackptr - 1)) !== false) {
return;
}
$tokens = $phpcsfile->gettokens();
// Find the next non whitespace token.
- $commentStart = $phpcsfile->findnext(T_WHITESPACE, ($stackptr + 1), null, true);
+ $commentstart = $phpcsfile->findnext(T_WHITESPACE, ($stackptr + 1), null, true);
// Look for $Id$ and boilerplate
- if ($tokens[$commentStart]['code'] != T_COMMENT) {
+ if ($tokens[$commentstart]['code'] != T_COMMENT) {
$phpcsfile->adderror('File must begin with License boilerplate', ($stackptr + 1));
return;
- } else if (preg_match('|\$Id\$|i', $tokens[$commentStart]['content'])) {
- $phpcsfile->addwarning('$Id$ tag is no longer required, please remove.', ($stackptr + 1));
+
+ } else if (preg_match('|\$Id\$|i', $tokens[$commentstart]['content'])) {
+ $phpcsfile->addwarning("\$Id\$ tag is no longer required, please remove.", ($stackptr + 1));
return;
}
if ($tokens[$filedoctoken]['code'] === T_CLOSE_TAG) {
// We are only interested if this is the first open tag.
return;
+
} else if ($tokens[$filedoctoken]['code'] === T_COMMENT) {
$phpcsfile->adderror('You must use "/**" style comments for a file comment', ($filedoctoken + 1));
return;
+
} else if ($filedoctoken === false || $tokens[$filedoctoken]['code'] !== T_DOC_COMMENT) {
$phpcsfile->adderror('Missing file doc comment', ($filedoctoken + 1));
return;
+
} else {
// Extract the header comment docblock.
- $commentEnd = ($phpcsfile->findnext(T_DOC_COMMENT, ($filedoctoken + 1), null, true) - 1);
+ $commentend = ($phpcsfile->findnext(T_DOC_COMMENT, ($filedoctoken + 1), null, true) - 1);
// Check if there is only 1 doc comment between the open tag and class token.
$nexttoken = array(
T_FUNCTION,
T_DOC_COMMENT,
);
- $commentNext = $phpcsfile->findnext($nexttoken, ($commentEnd + 1));
- if ($commentNext !== false && $tokens[$commentNext]['code'] !== T_DOC_COMMENT) {
+ $commentnext = $phpcsfile->findnext($nexttoken, ($commentend + 1));
+
+ if ($commentnext !== false && $tokens[$commentnext]['code'] !== T_DOC_COMMENT) {
// Found a class token right after comment doc block.
- $newlinetoken = $phpcsfile->findnext(T_WHITESPACE, ($commentEnd + 1), $commentNext, false, $phpcsfile->eolChar);
+ $newlinetoken = $phpcsfile->findnext(T_WHITESPACE, ($commentend + 1),
+ $commentnext, false, $phpcsfile->eolChar);
+
if ($newlinetoken !== false) {
- $newlinetoken = $phpcsfile->findnext(T_WHITESPACE, ($newlinetoken + 1), $commentNext, false, $phpcsfile->eolChar);
+ $newlinetoken = $phpcsfile->findnext(T_WHITESPACE, ($newlinetoken + 1),
+ $commentnext, false, $phpcsfile->eolChar);
+
if ($newlinetoken === false) {
// No blank line between the class token and the doc block.
// The doc block is most likely a class comment.
}
}
- $comment = $phpcsfile->gettokensAsString($filedoctoken, ($commentEnd - $filedoctoken + 1));
+ $comment = $phpcsfile->gettokensAsString($filedoctoken, ($commentend - $filedoctoken + 1));
// Parse the header comment docblock.
try {
$this->commentparser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsfile);
$this->commentparser->parse();
+
} catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
$line = ($e->getlinewithinComment() + $filedoctoken);
$phpcsfile->adderror($e->getMessage(), $line);
}
$comment = $this->commentparser->getComment();
+
if (is_null($comment) === true) {
$error = 'File doc comment is empty';
$phpcsfile->adderror($error, $filedoctoken);
// No extra newline before short description.
$short = $comment->getShortComment();
- $newlineCount = 0;
- $newlineSpan = strspn($short, $phpcsfile->eolChar);
- if ($short !== '' && $newlineSpan > 0) {
- $line = ($newlineSpan > 1) ? 'newlines' : 'newline';
+ $newlinecount = 0;
+ $newlinespan = strspn($short, $phpcsfile->eolChar);
+
+ if ($short !== '' && $newlinespan > 0) {
+ $line = ($newlinespan > 1) ? 'newlines' : 'newline';
$error = "Extra $line found before file comment short description";
$phpcsfile->adderror($error, ($filedoctoken + 1));
}
- $newlineCount = (substr_count($short, $phpcsfile->eolChar) + 1);
+ $newlinecount = (substr_count($short, $phpcsfile->eolChar) + 1);
// Exactly one blank line between short and long description.
$long = $comment->getlongcomment();
+
if (empty($long) === false) {
- $between = $comment->getWhiteSpaceBetween();
- $newlineBetween = substr_count($between, $phpcsfile->eolChar);
- if ($newlineBetween !== 2) {
+ $between = $comment->getWhiteSpacebetween();
+ $newlinebetween = substr_count($between, $phpcsfile->eolChar);
+
+ if ($newlinebetween !== 2) {
$error = 'There should be exactly one blank line between descriptions in file comment';
- $phpcsfile->addwarning($error, ($filedoctoken + $newlineCount + 1));
+ $phpcsfile->addwarning($error, ($filedoctoken + $newlinecount + 1));
}
- $newlineCount += $newlineBetween;
+ $newlinecount += $newlinebetween;
}
// Exactly one blank line before tags.
- $tags = $this->commentparser->getTagOrders();
+ $tags = $this->commentparser->gettagOrders();
+
if (count($tags) > 1) {
- $newlineSpan = $comment->getNewlineAfter();
- if ($newlineSpan !== 2) {
+ $newlinespan = $comment->getNewlineAfter();
+
+ if ($newlinespan !== 2) {
$error = 'There must be exactly one blank line before the tags in file comment';
+
if ($long !== '') {
- $newlineCount += (substr_count($long, $phpcsfile->eolChar) - $newlineSpan + 1);
+ $newlinecount += (substr_count($long, $phpcsfile->eolChar) - $newlinespan + 1);
}
- $phpcsfile->addwarning($error, ($filedoctoken + $newlineCount));
+ $phpcsfile->addwarning($error, ($filedoctoken + $newlinecount));
$short = rtrim($short, $phpcsfile->eolChar.' ');
}
}
/*
if (strstr(strtolower($long), 'php version') === false) {
$error = 'PHP version not specified';
- $phpcsfile->addwarning($error, $commentEnd);
+ $phpcsfile->addwarning($error, $commentend);
}
*/
// Check each tag.
- $this->processTags($filedoctoken, $commentEnd);
+ $this->processtags($filedoctoken, $commentend);
}
}
/**
* Processes each required or optional tag.
*
- * @param int $commentStart The position in the stack where the comment started.
- * @param int $commentEnd The position in the stack where the comment ended.
+ * @param int $commentstart The position in the stack where the comment started.
+ * @param int $commentend The position in the stack where the comment ended.
*
* @return void
*/
- protected function processtags($commentStart, $commentEnd) {
- $docBlock = (get_class($this) === 'moodle_sniffs_commenting_filecommentsniff') ? 'file' : 'class';
- $foundTags = $this->commentparser->getTagOrders();
- $orderIndex = 0;
+ protected function processtags($commentstart, $commentend) {
+ $docblock = (get_class($this) === 'moodle_sniffs_commenting_filecommentsniff') ? 'file' : 'class';
+ $foundtags = $this->commentparser->gettagOrders();
+ $orderindex = 0;
$indentation = array();
- $longestTag = 0;
- $errorPos = 0;
+ $longesttag = 0;
+ $errorpos = 0;
foreach ($this->tags as $tag => $info) {
// Required tag missing.
- if ($info['required'] === true && in_array($tag, $foundTags) === false) {
- $error = "Missing @$tag tag in $docBlock comment";
- $this->currentfile->adderror($error, $commentEnd);
+ if ($info['required'] === true && in_array($tag, $foundtags) === false) {
+ $error = "Missing @$tag tag in $docblock comment";
+ $this->currentfile->adderror($error, $commentend);
continue;
}
// Get the line number for current tag.
- $tagName = ucfirst($tag);
+ $tagname = ucfirst($tag);
+
if ($info['allow_multiple'] === true) {
- $tagName .= 's';
+ $tagname .= 's';
}
- $getMethod = 'get'.$tagName;
- $tagElement = $this->commentparser->$getMethod();
- if (is_null($tagElement) === true || empty($tagElement) === true) {
+ $getmethod = 'get'.$tagname;
+ $tagelement = $this->commentparser->$getmethod();
+
+ if (is_null($tagelement) === true || empty($tagelement) === true) {
continue;
}
- $errorPos = $commentStart;
- if (is_array($tagElement) === false) {
- $errorPos = ($commentStart + $tagElement->getline());
+ $errorpos = $commentstart;
+
+ if (is_array($tagelement) === false) {
+ $errorpos = ($commentstart + $tagelement->getline());
}
// Get the tag order.
- $foundIndexes = array_keys($foundTags, $tag);
+ $foundindexes = array_keys($foundtags, $tag);
- if (count($foundIndexes) > 1) {
+ if (count($foundindexes) > 1) {
// Multiple occurance not allowed.
if ($info['allow_multiple'] === false) {
- $error = "Only 1 @$tag tag is allowed in a $docBlock comment";
- $this->currentfile->adderror($error, $errorPos);
+ $error = "Only 1 @$tag tag is allowed in a $docblock comment";
+ $this->currentfile->adderror($error, $errorpos);
+
} else {
// Make sure same tags are grouped together.
$i = 0;
- $count = $foundIndexes[0];
- foreach ($foundIndexes as $index) {
+ $count = $foundindexes[0];
+
+ foreach ($foundindexes as $index) {
+
if ($index !== $count) {
- $errorPosIndex = ($errorPos + $tagElement[$i]->getline());
+ $errorposindex = ($errorpos + $tagelement[$i]->getline());
$error = "@$tag tags must be grouped together";
- $this->currentfile->adderror($error, $errorPosIndex);
+ $this->currentfile->adderror($error, $errorposindex);
}
$i++;
}
// Check tag order.
- if ($foundIndexes[0] > $orderIndex) {
- $orderIndex = $foundIndexes[0];
+ if ($foundindexes[0] > $orderindex) {
+ $orderindex = $foundindexes[0];
+
} else {
- if (is_array($tagElement) === true && empty($tagElement) === false) {
- $errorPos += $tagElement[0]->getline();
+
+ if (is_array($tagelement) === true && empty($tagelement) === false) {
+ $errorpos += $tagelement[0]->getline();
}
- $orderText = $info['order_text'];
- $error = "The @$tag tag is in the wrong order; the tag $orderText";
- $this->currentfile->adderror($error, $errorPos);
+ $ordertext = $info['order_text'];
+ $error = "The @$tag tag is in the wrong order; the tag $ordertext";
+ $this->currentfile->adderror($error, $errorpos);
}
// Store the indentation for checking.
$len = strlen($tag);
- if ($len > $longestTag) {
- $longestTag = $len;
+
+ if ($len > $longesttag) {
+ $longesttag = $len;
}
- if (is_array($tagElement) === true) {
- foreach ($tagElement as $key => $element) {
+ if (is_array($tagelement) === true) {
+
+ foreach ($tagelement as $key => $element) {
$indentation[] = array(
'tag' => $tag,
'space' => $this->getIndentation($tag, $element),
'line' => $element->getline(),
);
}
+
} else {
$indentation[] = array(
'tag' => $tag,
- 'space' => $this->getIndentation($tag, $tagElement),
+ 'space' => $this->getIndentation($tag, $tagelement),
);
}
- $method = 'process'.$tagName;
+ $method = 'process'.$tagname;
+
if (method_exists($this, $method) === true) {
// Process each tag if a method is defined.
- call_user_func(array($this, $method), $errorPos);
+ call_user_func(array($this, $method), $errorpos);
+
} else {
- if (is_array($tagElement) === true) {
- foreach ($tagElement as $key => $element) {
- $element->process($this->currentfile, $commentStart, $docBlock);
+
+ if (is_array($tagelement) === true) {
+
+ foreach ($tagelement as $key => $element) {
+ $element->process($this->currentfile, $commentstart, $docblock);
}
+
} else {
- $tagElement->process($this->currentfile, $commentStart, $docBlock);
+ $tagelement->process($this->currentfile, $commentstart, $docblock);
}
}
}
- foreach ($indentation as $indentInfo) {
- if ($indentInfo['space'] !== 0 && $indentInfo['space'] !== ($longestTag + 1)) {
- $expected = (($longestTag - strlen($indentInfo['tag'])) + 1);
- $space = ($indentInfo['space'] - strlen($indentInfo['tag']));
- $error = "@$indentInfo[tag] tag comment indented incorrectly. ";
+ foreach ($indentation as $indentinfo) {
+
+ if ($indentinfo['space'] !== 0 && $indentinfo['space'] !== ($longesttag + 1)) {
+ $expected = (($longesttag - strlen($indentinfo['tag'])) + 1);
+ $space = ($indentinfo['space'] - strlen($indentinfo['tag']));
+ $error = "@$indentinfo[tag] tag comment indented incorrectly. ";
$error .= "Expected $expected spaces but found $space.";
- $getTagMethod = 'get'.ucfirst($indentInfo['tag']);
- if ($this->tags[$indentInfo['tag']]['allow_multiple'] === true) {
- $line = $indentInfo['line'];
+ $gettagmethod = 'get'.ucfirst($indentinfo['tag']);
+
+ if ($this->tags[$indentinfo['tag']]['allow_multiple'] === true) {
+ $line = $indentinfo['line'];
+
} else {
- $tagElem = $this->commentparser->$getTagMethod();
- $line = $tagElem->getline();
+ $tagelem = $this->commentparser->$gettagmethod();
+ $line = $tagelem->getline();
}
- $this->currentfile->addwarning($error, ($commentStart + $line));
+ $this->currentfile->addwarning($error, ($commentstart + $line));
}
}
/**
* Get the indentation information of each tag.
*
- * @param string $tagName The name of the doc comment element.
- * @param PHP_CodeSniffer_CommentParser_DocElement $tagElement The doc comment element.
+ * @param string $tagname The name of the doc comment element.
+ * @param PHP_CodeSniffer_CommentParser_Docelement $tagelement The doc comment element.
*
* @return void
*/
- protected function getindentation($tagName, $tagElement)
- {
- if ($tagElement instanceof PHP_CodeSniffer_CommentParser_SingleElement) {
- if ($tagElement->getcontent() !== '') {
- return (strlen($tagName) + substr_count($tagElement->getWhitespaceBeforecontent(), ' '));
+ protected function getindentation($tagname, $tagelement) {
+
+ if ($tagelement instanceof PHP_CodeSniffer_CommentParser_Singleelement) {
+
+ if ($tagelement->getcontent() !== '') {
+ return (strlen($tagname) + substr_count($tagelement->getWhitespaceBeforecontent(), ' '));
}
- } else if ($tagElement instanceof PHP_CodeSniffer_CommentParser_PairElement) {
- if ($tagElement->getValue() !== '') {
- return (strlen($tagName) + substr_count($tagElement->getWhitespaceBeforeValue(), ' '));
+
+ } else if ($tagelement instanceof PHP_CodeSniffer_CommentParser_Pairelement) {
+
+ if ($tagelement->getValue() !== '') {
+ return (strlen($tagname) + substr_count($tagelement->getWhitespaceBeforeValue(), ' '));
}
}
return 0;
-
}
/**
* Process the category tag.
*
- * @param int $errorPos The line number where the error occurs.
+ * @param int $errorpos The line number where the error occurs.
*
* @return void
*/
- protected function processcategory($errorPos)
- {
+ protected function processcategory($errorpos) {
$category = $this->commentparser->getCategory();
+
if ($category !== null) {
$content = $category->getcontent();
+
if ($content !== '') {
- if (PHP_CodeSniffer::isUnderscoreName($content) !== true) {
+
+ if (PHP_CodeSniffer::isUnderscorename($content) !== true) {
$newcontent = str_replace(' ', '_', $content);
- $nameBits = explode('_', $newcontent);
- $firstBit = array_shift($nameBits);
- $newName = ucfirst($firstBit).'_';
- foreach ($nameBits as $bit) {
- $newName .= ucfirst($bit).'_';
+ $namebits = explode('_', $newcontent);
+ $firstbit = array_shift($namebits);
+ $newname = ucfirst($firstbit).'_';
+
+ foreach ($namebits as $bit) {
+ $newname .= ucfirst($bit).'_';
}
- $validName = trim($newName, '_');
- $error = "Category name \"$content\" is not valid; consider \"$validName\" instead";
- // $this->currentfile->adderror($error, $errorPos);
+ $validname = trim($newname, '_');
+ $error = "Category name \"$content\" is not valid; consider \"$validname\" instead";
+ // $this->currentfile->adderror($error, $errorpos);
}
+
} else {
$error = '@category tag must contain a name';
- $this->currentfile->adderror($error, $errorPos);
+ $this->currentfile->adderror($error, $errorpos);
}
}
/**
* Process the package tag.
*
- * @param int $errorPos The line number where the error occurs.
+ * @param int $errorpos The line number where the error occurs.
*
* @return void
*/
- protected function processpackage($errorPos) {
+ protected function processpackage($errorpos) {
global $CFG;
$package = $this->commentparser->getPackage();
$filename = str_replace($CFG->dirroot, '', $this->currentfile->getfilename());
// Remove first dash if present
$expected_package = ltrim($expected_package, '-');
+
if ($package !== null) {
$content = $package->getcontent();
if ($content !== $expected_package) {
$error = "Package name \"$content\" is not valid; \"$expected_package\" expected.";
- $this->currentfile->adderror($error, $errorPos);
+ $this->currentfile->adderror($error, $errorpos);
} else if ($content !== '') {
+
if (!preg_match('/^[a-z\-]*$/', $content)) {
$error = "Package name \"$content\" is not valid; must be lower-case with optional hyphens.";
- $this->currentfile->adderror($error, $errorPos);
+ $this->currentfile->adderror($error, $errorpos);
}
+
} else {
$error = '@package tag must contain a name';
- $this->currentfile->adderror($error, $errorPos);
+ $this->currentfile->adderror($error, $errorpos);
}
}
/**
* Process the subpackage tag.
*
- * @param int $errorPos The line number where the error occurs.
+ * @param int $errorpos The line number where the error occurs.
*
* @return void
*/
- protected function processsubpackage($errorPos)
- {
+ protected function processsubpackage($errorpos) {
$package = $this->commentparser->getSubpackage();
+
if ($package !== null) {
$content = $package->getcontent();
+
if ($content !== '') {
- if (PHP_CodeSniffer::isUnderscoreName($content) !== true) {
+
+ if (PHP_CodeSniffer::isUnderscorename($content) !== true) {
$newcontent = str_replace(' ', '_', $content);
- $nameBits = explode('_', $newcontent);
- $firstBit = array_shift($nameBits);
- $newName = strtoupper($firstBit{0}).substr($firstBit, 1).'_';
- foreach ($nameBits as $bit) {
- $newName .= strtoupper($bit{0}).substr($bit, 1).'_';
+ $namebits = explode('_', $newcontent);
+ $firstbit = array_shift($namebits);
+ $newname = strtoupper($firstbit{0}).substr($firstbit, 1).'_';
+
+ foreach ($namebits as $bit) {
+ $newname .= strtoupper($bit{0}).substr($bit, 1).'_';
}
- $validName = trim($newName, '_');
- $error = "Subpackage name \"$content\" is not valid; consider \"$validName\" instead";
- $this->currentfile->adderror($error, $errorPos);
+ $validname = trim($newname, '_');
+ $error = "Subpackage name \"$content\" is not valid; consider \"$validname\" instead";
+ $this->currentfile->adderror($error, $errorpos);
}
+
} else {
$error = '@subpackage tag must contain a name';
- $this->currentfile->adderror($error, $errorPos);
+ $this->currentfile->adderror($error, $errorpos);
}
}
* Process the author tag(s) that this header comment has.
*
* This function is different from other _process functions
- * as $authors is an array of SingleElements, so we work out
- * the errorPos for each element separately
+ * as $authors is an array of Singleelements, so we work out
+ * the errorpos for each element separately
*
- * @param int $commentStart The position in the stack where
+ * @param int $commentstart The position in the stack where
* the comment started.
*
* @return void
*/
- protected function processauthors($commentStart)
- {
+ protected function processauthors($commentstart) {
$authors = $this->commentparser->getAuthors();
// Report missing return.
if (empty($authors) === false) {
+
foreach ($authors as $author) {
- $errorPos = ($commentStart + $author->getline());
+ $errorpos = ($commentstart + $author->getline());
$content = $author->getcontent();
+
if ($content !== '') {
$local = '\da-zA-Z-_+';
// Dot character cannot be the first or last character in the local-part.
- $localMiddle = $local.'.\w';
- if (preg_match('/^([^<]*)\s+<(['.$local.']['.$localMiddle.']*['.$local.']@[\da-zA-Z][-.\w]*[\da-zA-Z]\.[a-zA-Z]{2,7})>$/', $content) === 0) {
- $error = 'content of the @author tag must be in the form "Display Name <username@example.com>"';
- $this->currentfile->adderror($error, $errorPos);
+ $localmiddle = $local.'.\w';
+
+ if (preg_match('/^([^<]*)\s+<(['.$local.']['.$localmiddle.']*['.$local.']@[\da-zA-Z][-.\w]*[\da-zA-Z]\.[a-zA-Z]{2,7})>$/', $content) === 0) {
+ $error = 'content of the @author tag must be in the form "Display name <username@example.com>"';
+ $this->currentfile->adderror($error, $errorpos);
}
+
} else {
- $docBlock = (get_class($this) === 'moodle_sniffs_commenting_filecommentsniff') ? 'file' : 'class';
- $error = "content missing for @author tag in $docBlock comment";
- $this->currentfile->adderror($error, $errorPos);
+ $docblock = (get_class($this) === 'moodle_sniffs_commenting_filecommentsniff') ? 'file' : 'class';
+ $error = "content missing for @author tag in $docblock comment";
+ $this->currentfile->adderror($error, $errorpos);
}
}
}
/**
* Process the copyright tags.
*
- * @param int $commentStart The position in the stack where
+ * @param int $commentstart The position in the stack where
* the comment started.
*
* @return void
*/
- protected function processcopyrights($commentStart)
- {
+ protected function processcopyrights($commentstart) {
$copyrights = $this->commentparser->getCopyrights();
+
foreach ($copyrights as $copyright) {
- $errorPos = ($commentStart + $copyright->getline());
+ $errorpos = ($commentstart + $copyright->getline());
$content = $copyright->getcontent();
+
if ($content !== '') {
$matches = array();
+
if (preg_match('/^([0-9]{4})((.{1})([0-9]{4}))? (.+)$/', $content, $matches) !== 0) {
// Check earliest-latest year order.
+
if ($matches[3] !== '') {
+
if ($matches[3] !== '-') {
$error = 'A hyphen must be used between the earliest and latest year';
- $this->currentfile->adderror($error, $errorPos);
+ $this->currentfile->adderror($error, $errorpos);
}
if ($matches[4] !== '' && $matches[4] < $matches[1]) {
$error = "Invalid year span \"$matches[1]$matches[3]$matches[4]\" found; consider \"$matches[4]-$matches[1]\" instead";
- $this->currentfile->addwarning($error, $errorPos);
+ $this->currentfile->addwarning($error, $errorpos);
}
}
+
} else {
$error = '@copyright tag must contain a year and the name of the copyright holder';
- $this->currentfile->adderror($error, $errorPos);
+ $this->currentfile->adderror($error, $errorpos);
}
+
} else {
$error = '@copyright tag must contain a year and the name of the copyright holder';
- $this->currentfile->adderror($error, $errorPos);
+ $this->currentfile->adderror($error, $errorpos);
}
}
/**
* Process the license tag.
*
- * @param int $errorPos The line number where the error occurs.
+ * @param int $errorpos The line number where the error occurs.
*
* @return void
*/
- protected function processlicense($errorPos)
- {
+ protected function processlicense($errorpos) {
$license = $this->commentparser->getLicense();
+
if ($license !== null) {
$value = $license->getValue();
$comment = $license->getComment();
+
if ($value === '' || $comment === '') {
$error = '@license tag must contain a URL and a license name';
- $this->currentfile->adderror($error, $errorPos);
+ $this->currentfile->adderror($error, $errorpos);
}
+
if ($comment != 'GNU GPL v3 or later') {
- $this->currentfile->adderror('License must be "GNU GPL v3 or later", found "'.$comment.'"', $errorPos);
+ $this->currentfile->adderror('License must be "GNU GPL v3 or later", found "'.$comment.'"', $errorpos);
}
+
if ($value != 'http://www.gnu.org/copyleft/gpl.html') {
- $this->currentfile->adderror('License must be "GNU GPL v3 or later"', $errorPos);
+ $this->currentfile->adderror('License must be "GNU GPL v3 or later"', $errorpos);
}
}
-
}
/**
* Process the version tag.
*
- * @param int $errorPos The line number where the error occurs.
+ * @param int $errorpos The line number where the error occurs.
*
* @return void
*/
- protected function processversion($errorPos)
- {
+ protected function processversion($errorpos) {
$version = $this->commentparser->getVersion();
+
if ($version !== null) {
$content = $version->getcontent();
$matches = array();
+
if (empty($content) === true) {
$error = 'content missing for @version tag in file comment';
- $this->currentfile->adderror($error, $errorPos);
+ $this->currentfile->adderror($error, $errorpos);
+
} else if (strstr($content, 'CVS:') === false && strstr($content, 'SVN:') === false) {
$error = "Invalid version \"$content\" in file comment; consider \"CVS: <cvs_id>\" or \"SVN: <svn_id>\" instead";
- $this->currentfile->addwarning($error, $errorPos);
+ $this->currentfile->addwarning($error, $errorpos);
}
}
}
-
-
}
-
-?>
*
* @var string
*/
- private $_methodName = '';
+ private $_methodname = '';
/**
* The position in the stack where the fucntion token was found.
T_OPEN_TAG,
);
- $commentEnd = $phpcsfile->findPrevious($find, ($stackptr - 1));
+ $commentend = $phpcsfile->findPrevious($find, ($stackptr - 1));
- if ($commentEnd === false) {
+ if ($commentend === false) {
return;
}
// If the token that we found was a class or a function, then this
// function has no doc comment.
- $code = $tokens[$commentEnd]['code'];
+ $code = $tokens[$commentend]['code'];
if ($code === T_COMMENT) {
$error = 'You must use "/**" style comments for a function comment';
$ignore[] = T_ABSTRACT;
$ignore[] = T_FINAL;
$prevtoken = $phpcsfile->findPrevious($ignore, ($stackptr - 1), null, true);
- if ($prevtoken !== $commentEnd) {
+ if ($prevtoken !== $commentend) {
$phpcsfile->adderror('Missing function doc comment', $stackptr);
return;
}
// If the first T_OPEN_TAG is right before the comment, it is probably
// a file comment.
- $commentStart = ($phpcsfile->findPrevious(T_DOC_COMMENT, ($commentEnd - 1), null, true) + 1);
- $prevtoken = $phpcsfile->findPrevious(T_WHITESPACE, ($commentStart - 1), null, true);
+ $commentstart = ($phpcsfile->findPrevious(T_DOC_COMMENT, ($commentend - 1), null, true) + 1);
+ $prevtoken = $phpcsfile->findPrevious(T_WHITESPACE, ($commentstart - 1), null, true);
if ($tokens[$prevtoken]['code'] === T_OPEN_TAG) {
// Is this the first open tag?
if ($stackptr === 0 || $phpcsfile->findPrevious(T_OPEN_TAG, ($prevtoken - 1)) === false) {
}
}
- $comment = $phpcsfile->gettokensAsString($commentStart, ($commentEnd - $commentStart + 1));
- $this->_methodName = $phpcsfile->getDeclarationName($stackptr);
+ $comment = $phpcsfile->gettokensAsString($commentstart, ($commentend - $commentstart + 1));
+ $this->_methodname = $phpcsfile->getDeclarationname($stackptr);
try {
$this->commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsfile);
$this->commentParser->parse();
} catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
- $line = ($e->getlinewithinComment() + $commentStart);
+ $line = ($e->getlinewithinComment() + $commentstart);
$phpcsfile->adderror($e->getMessage(), $line);
return;
}
$comment = $this->commentParser->getComment();
if (is_null($comment) === true) {
$error = 'Function doc comment is empty';
- $phpcsfile->adderror($error, $commentStart);
+ $phpcsfile->adderror($error, $commentstart);
return;
}
- $this->processParams($commentStart);
- $this->processReturn($commentStart, $commentEnd);
- $this->processThrows($commentStart);
+ $this->processParams($commentstart);
+ $this->processReturn($commentstart, $commentend);
+ $this->processThrows($commentstart);
// No extra newline before short description.
$short = $comment->getShortComment();
- $newlineCount = 0;
+ $newlinecount = 0;
$newlineSpan = strspn($short, $phpcsfile->eolChar);
if ($short !== '' && $newlineSpan > 0) {
$line = ($newlineSpan > 1) ? 'newlines' : 'newline';
$error = "Extra $line found before function comment short description";
- $phpcsfile->adderror($error, ($commentStart + 1));
+ $phpcsfile->adderror($error, ($commentstart + 1));
}
- $newlineCount = (substr_count($short, $phpcsfile->eolChar) + 1);
+ $newlinecount = (substr_count($short, $phpcsfile->eolChar) + 1);
// Exactly one blank line between short and long description.
$long = $comment->getlongcomment();
$newlineBetween = substr_count($between, $phpcsfile->eolChar);
if ($newlineBetween !== 2) {
$error = 'There must be exactly one blank line between descriptions in function comment';
- $phpcsfile->adderror($error, ($commentStart + $newlineCount + 1));
+ $phpcsfile->adderror($error, ($commentstart + $newlinecount + 1));
}
- $newlineCount += $newlineBetween;
+ $newlinecount += $newlineBetween;
}
// Exactly one blank line before tags.
- $params = $this->commentParser->getTagOrders();
+ $params = $this->commentParser->gettagOrders();
if (count($params) > 1) {
$newlineSpan = $comment->getNewlineAfter();
if ($newlineSpan !== 2) {
$error = 'There must be exactly one blank line before the tags in function comment';
if ($long !== '') {
- $newlineCount += (substr_count($long, $phpcsfile->eolChar) - $newlineSpan + 1);
+ $newlinecount += (substr_count($long, $phpcsfile->eolChar) - $newlineSpan + 1);
}
- $phpcsfile->addwarning($error, ($commentStart + $newlineCount));
+ $phpcsfile->addwarning($error, ($commentstart + $newlinecount));
$short = rtrim($short, $phpcsfile->eolChar.' ');
}
}
/**
* Process any throw tags that this function comment has.
*
- * @param int $commentStart The position in the stack where the
+ * @param int $commentstart The position in the stack where the
* comment started.
*
* @return void
*/
- protected function processThrows($commentStart)
+ protected function processThrows($commentstart)
{
if (count($this->commentParser->getThrows()) === 0) {
return;
foreach ($this->commentParser->getThrows() as $throw) {
$exception = $throw->getValue();
- $errorPos = ($commentStart + $throw->getline());
+ $errorpos = ($commentstart + $throw->getline());
if ($exception === '') {
$error = '@throws tag must contain the exception class name';
- $this->currentFile->adderror($error, $errorPos);
+ $this->currentFile->adderror($error, $errorpos);
}
}
/**
* Process the return comment of this function comment.
*
- * @param int $commentStart The position in the stack where the comment started.
- * @param int $commentEnd The position in the stack where the comment ended.
+ * @param int $commentstart The position in the stack where the comment started.
+ * @param int $commentend The position in the stack where the comment ended.
*
* @return void
*/
- protected function processReturn($commentStart, $commentEnd)
+ protected function processReturn($commentstart, $commentend)
{
// Skip constructor and destructor.
- $className = '';
+ $classname = '';
if ($this->_classtoken !== null) {
- $className = $this->currentFile->getDeclarationName($this->_classtoken);
- $className = strtolower(ltrim($className, '_'));
+ $classname = $this->currentFile->getDeclarationname($this->_classtoken);
+ $classname = strtolower(ltrim($classname, '_'));
}
- $methodName = strtolower(ltrim($this->_methodName, '_'));
- $isSpecialMethod = ($this->_methodName === '__construct' || $this->_methodName === '__destruct');
+ $methodname = strtolower(ltrim($this->_methodname, '_'));
+ $isspecialmethod = ($this->_methodname === '__construct' || $this->_methodname === '__destruct');
- if ($isSpecialMethod === false && $methodName !== $className) {
+ if ($isspecialmethod === false && $methodname !== $classname) {
// Report missing return tag.
if ($this->commentParser->getReturn() === null) {
$error = 'Missing @return tag in function comment';
- $this->currentFile->adderror($error, $commentEnd);
+ $this->currentFile->adderror($error, $commentend);
} else if (trim($this->commentParser->getReturn()->getRawcontent()) === '') {
$error = '@return tag is empty in function comment';
- $errorPos = ($commentStart + $this->commentParser->getReturn()->getline());
- $this->currentFile->adderror($error, $errorPos);
+ $errorpos = ($commentstart + $this->commentParser->getReturn()->getline());
+ $this->currentFile->adderror($error, $errorpos);
}
}
/**
* Process the function parameter comments.
*
- * @param int $commentStart The position in the stack where
+ * @param int $commentstart The position in the stack where
* the comment started.
*
* @return void
*/
- protected function processParams($commentStart)
+ protected function processParams($commentstart)
{
- $realParams = $this->currentFile->getMethodParameters($this->_functiontoken);
+ $realParams = $this->currentFile->getmethodParameters($this->_functiontoken);
$params = $this->commentParser->getParams();
$foundParams = array();
$lastParm = (count($params) - 1);
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->addwarning($error, $errorPos);
+ $errorpos = ($params[$lastParm]->getline() + $commentstart);
+ $this->currentFile->addwarning($error, $errorpos);
}
// Parameters must appear immediately after the comment.
if ($params[0]->getOrder() !== 2) {
$error = 'Parameters must appear immediately after the comment';
- $errorPos = ($params[0]->getline() + $commentStart);
- $this->currentFile->adderror($error, $errorPos);
+ $errorpos = ($params[0]->getline() + $commentstart);
+ $this->currentFile->adderror($error, $errorpos);
}
$previousParam = null;
foreach ($params as $param) {
$paramComment = trim($param->getComment());
- $errorPos = ($param->getline() + $commentStart);
+ $errorpos = ($param->getline() + $commentstart);
// Make sure that there is only one space before the var type.
if ($param->getWhitespaceBeforeType() !== ' ') {
$error = 'Expected 1 space before variable type';
- $this->currentFile->addwarning($error, $errorPos);
+ $this->currentFile->addwarning($error, $errorpos);
}
- $spaceCount = substr_count($param->getWhitespaceBeforeVarName(), ' ');
- if ($spaceCount < $spaceBeforeVar) {
- $spaceBeforeVar = $spaceCount;
- $longestType = $errorPos;
+ $spacecount = substr_count($param->getWhitespaceBeforeVarname(), ' ');
+ if ($spacecount < $spaceBeforeVar) {
+ $spaceBeforeVar = $spacecount;
+ $longestType = $errorpos;
}
- $spaceCount = substr_count($param->getWhitespaceBeforeComment(), ' ');
+ $spacecount = substr_count($param->getWhitespaceBeforeComment(), ' ');
- if ($spaceCount < $spaceBeforeComment && $paramComment !== '') {
- $spaceBeforeComment = $spaceCount;
- $longestVar = $errorPos;
+ if ($spacecount < $spaceBeforeComment && $paramComment !== '') {
+ $spaceBeforeComment = $spacecount;
+ $longestVar = $errorpos;
}
// Make sure they are in the correct order,
// and have the correct name.
- $pos = $param->getPosition();
+ $pos = $param->getposition();
- $paramName = ($param->getVarName() !== '') ? $param->getVarName() : '[ UNKNOWN ]';
+ $paramname = ($param->getVarname() !== '') ? $param->getVarname() : '[ UNKNOWN ]';
if ($previousParam !== null) {
- $previousName = ($previousParam->getVarName() !== '') ? $previousParam->getVarName() : 'UNKNOWN';
+ $previousname = ($previousParam->getVarname() !== '') ? $previousParam->getVarname() : 'UNKNOWN';
// 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->addwarning($error, $errorPos);
+ $error = 'The variable names for parameters '.$previousname.' ('.($pos - 1).') and '.$paramname.' ('.$pos.') do not align';
+ $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->addwarning($error, $errorPos);
+ $error = 'The comments for parameters '.$previousname.' ('.($pos - 1).') and '.$paramname.' ('.$pos.') do not align';
+ $this->currentFile->addwarning($error, $errorpos);
}
}
// Make sure the names of the parameter comment matches the
// actual parameter.
if (isset($realParams[($pos - 1)]) === true) {
- $realName = $realParams[($pos - 1)]['name'];
- $foundParams[] = $realName;
+ $realname = $realParams[($pos - 1)]['name'];
+ $foundParams[] = $realname;
// Append ampersand to name if passing by reference.
if ($realParams[($pos - 1)]['pass_by_reference'] === true) {
- $realName = '&'.$realName;
+ $realname = '&'.$realname;
}
- if ($realName !== $param->getVarName()) {
- $error = 'Doc comment var "'.$paramName;
- $error .= '" does not match actual variable name "'.$realName;
+ if ($realname !== $param->getVarname()) {
+ $error = 'Doc comment var "'.$paramname;
+ $error .= '" does not match actual variable name "'.$realname;
$error .= '" at position '.$pos;
- $this->currentFile->adderror($error, $errorPos);
+ $this->currentFile->adderror($error, $errorpos);
}
} else {
// We must have an extra parameter comment.
$error = 'Superfluous doc comment at position '.$pos;
- $this->currentFile->adderror($error, $errorPos);
+ $this->currentFile->adderror($error, $errorpos);
}
- if ($param->getVarName() === '') {
+ if ($param->getVarname() === '') {
$error = 'Missing parameter name at position '.$pos;
- $this->currentFile->adderror($error, $errorPos);
+ $this->currentFile->adderror($error, $errorpos);
}
if ($param->getType() === '') {
$error = 'Missing type at position '.$pos;
- $this->currentFile->adderror($error, $errorPos);
+ $this->currentFile->adderror($error, $errorpos);
}
if ($paramComment === '') {
- $error = 'Missing comment for param "'.$paramName.'" at position '.$pos;
- $this->currentFile->adderror($error, $errorPos);
+ $error = 'Missing comment for param "'.$paramname.'" at position '.$pos;
+ $this->currentFile->adderror($error, $errorpos);
}
$previousParam = $param;
}
- $realNames = array();
+ $realnames = array();
foreach ($realParams as $realParam) {
- $realNames[] = $realParam['name'];
+ $realnames[] = $realParam['name'];
}
// Report and missing comments.
- $diff = array_diff($realNames, $foundParams);
+ $diff = array_diff($realnames, $foundParams);
foreach ($diff as $neededParam) {
if (count($params) !== 0) {
- $errorPos = ($params[(count($params) - 1)]->getline() + $commentStart);
+ $errorpos = ($params[(count($params) - 1)]->getline() + $commentstart);
} else {
- $errorPos = $commentStart;
+ $errorpos = $commentstart;
}
$error = 'Doc comment for "'.$neededParam.'" missing';
- $this->currentFile->adderror($error, $errorPos);
+ $this->currentFile->adderror($error, $errorpos);
}
}
$found = $phpcsfile->eolChar;
$found = str_replace("\n", '\n', $found);
$found = str_replace("\r", '\r', $found);
- $error = "End of line character is invalid; expected \"$expected\" but found \"$found\"";
+ $error = "end of line character is invalid; expected \"$expected\" but found \"$found\"";
$phpcsfile->adderror($error, $stackptr);
}
$tokens = $phpcsfile->gettokens();
// Make sure this is the first open tag.
- $previousOpenTag = $phpcsfile->findPrevious(array(T_OPEN_TAG), ($stackptr - 1));
- if ($previousOpenTag !== false) {
+ $previousOpentag = $phpcsfile->findPrevious(array(T_OPEN_TAG), ($stackptr - 1));
+ if ($previousOpentag !== false) {
return;
}
- $tokenCount = 0;
+ $tokencount = 0;
$currentlinecontent = '';
$currentline = 1;
- for (; $tokenCount < $phpcsfile->numTokens; $tokenCount++) {
- if ($tokens[$tokenCount]['line'] === $currentline) {
- $currentlinecontent .= $tokens[$tokenCount]['content'];
+ for (; $tokencount < $phpcsfile->numTokens; $tokencount++) {
+ if ($tokens[$tokencount]['line'] === $currentline) {
+ $currentlinecontent .= $tokens[$tokencount]['content'];
} else {
$currentlinecontent = trim($currentlinecontent, $phpcsfile->eolChar);
- $this->checklineLength($phpcsfile, ($tokenCount - 1), $currentlinecontent);
- $currentlinecontent = $tokens[$tokenCount]['content'];
+ $this->checklineLength($phpcsfile, ($tokencount - 1), $currentlinecontent);
+ $currentlinecontent = $tokens[$tokencount]['content'];
$currentline++;
}
}
- $this->checklineLength($phpcsfile, ($tokenCount - 1), $currentlinecontent);
+ $this->checklineLength($phpcsfile, ($tokencount - 1), $currentlinecontent);
}
// function myFunction...
// "myFunction" is T_STRING but we should skip because it is not a
// function or method *call*.
- $functionName = $stackptr;
+ $functionname = $stackptr;
$functionKeyword = $phpcsfile->findPrevious(PHP_CodeSniffer_tokens::$emptyTokens, ($stackptr - 1), null, true);
if ($tokens[$functionKeyword]['code'] === T_FUNCTION || $tokens[$functionKeyword]['code'] === T_CLASS) {
return;
// If the next non-whitespace token after the function or method call
// is not an opening parenthesis then it cant really be a *call*.
- $openBracket = $phpcsfile->findNext(PHP_CodeSniffer_tokens::$emptyTokens, ($functionName + 1), null, true);
+ $openBracket = $phpcsfile->findNext(PHP_CodeSniffer_tokens::$emptyTokens, ($functionname + 1), null, true);
if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
return;
}
{
$tokens = $phpcsfile->gettokens();
- $argStart = $tokens[$stackptr]['parenthesis_opener'];
- $argEnd = $tokens[$stackptr]['parenthesis_closer'];
+ $argstart = $tokens[$stackptr]['parenthesis_opener'];
+ $argend = $tokens[$stackptr]['parenthesis_closer'];
// Flag for when we have found a default in our arg list.
// If there is a value without a default after this, it is an error.
$defaultFound = false;
- $nextArg = $argStart;
- while (($nextArg = $phpcsfile->findNext(T_VARIABLE, ($nextArg + 1), $argEnd)) !== false) {
+ $nextArg = $argstart;
+ while (($nextArg = $phpcsfile->findNext(T_VARIABLE, ($nextArg + 1), $argend)) !== false) {
$argHasDefault = self::_argHasDefault($phpcsfile, $nextArg);
if (($argHasDefault === false) && ($defaultFound === true)) {
$error = 'Arguments with default values must be at the end';
public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
$tokens = $phpcsfile->gettokens();
- $constName = $tokens[$stackptr]['content'];
+ $constname = $tokens[$stackptr]['content'];
// If this token is in a heredoc, ignore it.
if ($phpcsfile->hasCondition($stackptr, T_START_HEREDOC) === true) {
if ($tokens[$functionKeyword]['code'] === T_CONST) {
// This is a class constant.
- if (strtoupper($constName) !== $constName) {
- $error = 'Class constants must be uppercase; expected '.strtoupper($constName)." but found $constName";
+ if (strtoupper($constname) !== $constname) {
+ $error = 'Class constants must be uppercase; expected '.strtoupper($constname)." but found $constname";
$phpcsfile->adderror($error, $stackptr);
}
}
// This is a real constant.
- if (strtoupper($constName) !== $constName) {
- $error = 'Constants must be uppercase; expected '.strtoupper($constName)." but found $constName";
+ if (strtoupper($constname) !== $constname) {
+ $error = 'Constants must be uppercase; expected '.strtoupper($constname)." but found $constname";
$phpcsfile->adderror($error, $stackptr);
}
- } else if (strtolower($constName) === 'define' || strtolower($constName) === 'constant') {
+ } else if (strtolower($constname) === 'define' || strtolower($constname) === 'constant') {
/*
This may be a "define" or "constant" function call.
return;
}
- $constName = $tokens[$constPtr]['content'];
- if (strtoupper($constName) !== $constName) {
- $error = 'Constants must be uppercase; expected '.strtoupper($constName)." but found $constName";
+ $constname = $tokens[$constPtr]['content'];
+ if (strtoupper($constname) !== $constname) {
+ $error = 'Constants must be uppercase; expected '.strtoupper($constname)." but found $constname";
$phpcsfile->adderror($error, $stackptr);
}
}
{
$tokens = $phpcsfile->gettokens();
- $className = $phpcsfile->findNext(T_STRING, $stackptr);
- $name = trim($tokens[$className]['content']);
+ $classname = $phpcsfile->findNext(T_STRING, $stackptr);
+ $name = trim($tokens[$classname]['content']);
// Make sure that the word is all lowercase
*
* @var array
*/
- private $_magicMethods = array(
+ private $_magicmethods = array(
'construct',
'destruct',
'call',
*/
protected function processtokenwithinScope(PHP_CodeSniffer_File $phpcsfile, $stackptr, $currScope)
{
- $className = $phpcsfile->getDeclarationName($currScope);
- $methodName = $phpcsfile->getDeclarationName($stackptr);
+ $classname = $phpcsfile->getDeclarationname($currScope);
+ $methodname = $phpcsfile->getDeclarationname($stackptr);
// Is this a magic method. IE. is prefixed with "__".
- if (preg_match('|^__|', $methodName) !== 0) {
- $magicPart = substr($methodName, 2);
- if (in_array($magicPart, $this->_magicMethods) === false) {
- $error = "Method name \"$className::$methodName\" is invalid; only PHP magic methods should be prefixed with a double underscore";
+ if (preg_match('|^__|', $methodname) !== 0) {
+ $magicPart = substr($methodname, 2);
+ if (in_array($magicPart, $this->_magicmethods) === false) {
+ $error = "method name \"$classname::$methodname\" is invalid; only PHP magic methods should be prefixed with a double underscore";
$phpcsfile->adderror($error, $stackptr);
}
}
// PHP4 constructors are allowed to break our rules.
- if ($methodName === $className) {
+ if ($methodname === $classname) {
return;
}
// PHP4 destructors are allowed to break our rules.
- if ($methodName === '_'.$className) {
+ if ($methodname === '_'.$classname) {
return;
}
- $methodProps = $phpcsfile->getMethodProperties($stackptr);
+ $methodProps = $phpcsfile->getmethodProperties($stackptr);
$isPublic = ($methodProps['scope'] === 'private') ? false : true;
$scope = $methodProps['scope'];
$scopeSpecified = $methodProps['scope_specified'];
// Only lower-case accepted
- if (preg_match('/[A-Z]+/', $methodName)) {
+ if (preg_match('/[A-Z]+/', $methodname)) {
if ($scopeSpecified === true) {
- $error = ucfirst($scope)." method name \"$className::$methodName\" must be in lower-case letters only";
+ $error = ucfirst($scope)." method name \"$classname::$methodname\" must be in lower-case letters only";
} else {
- $error = "Method name \"$className::$methodName\" must be in lower-case letters only";
+ $error = "method name \"$classname::$methodname\" must be in lower-case letters only";
}
$phpcsfile->adderror($error, $stackptr);
}
// No numbers accepted
- if (preg_match('/[0-9]+/', $methodName)) {
+ if (preg_match('/[0-9]+/', $methodname)) {
if ($scopeSpecified === true) {
- $error = ucfirst($scope)." method name \"$className::$methodName\" must only contain letters";
+ $error = ucfirst($scope)." method name \"$classname::$methodname\" must only contain letters";
} else {
- $error = "Method name \"$className::$methodName\" must only contain letters";
+ $error = "Method name \"$classname::$methodname\" must only contain letters";
}
$phpcsfile->adderror($error, $stackptr);
*/
protected function processtokenOutsideScope(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
- $functionName = $phpcsfile->getDeclarationName($stackptr);
+ $functionname = $phpcsfile->getDeclarationname($stackptr);
// Is this a magic function. IE. is prefixed with "__".
- if (preg_match('|^__|', $functionName) !== 0) {
- $magicPart = substr($functionName, 2);
+ if (preg_match('|^__|', $functionname) !== 0) {
+ $magicPart = substr($functionname, 2);
if (in_array($magicPart, $this->_magicFunctions) === false) {
- $error = "Function name \"$functionName\" is invalid; only PHP magic methods should be prefixed with a double underscore";
+ $error = "Function name \"$functionname\" is invalid; only PHP magic methods should be prefixed with a double underscore";
$phpcsfile->adderror($error, $stackptr);
}
}
// Only lower-case accepted
- if (preg_match('/[A-Z]+/', $functionName)) {
- $error = "function name \"$functionName\" must be lower-case letters only";
+ if (preg_match('/[A-Z]+/', $functionname)) {
+ $error = "function name \"$functionname\" must be lower-case letters only";
$phpcsfile->adderror($error, $stackptr);
return;
}
// Only letters accepted
- if (preg_match('/[0-9]+/', $functionName)) {
- $error = "function name \"$functionName\" must only contain letters";
+ if (preg_match('/[0-9]+/', $functionname)) {
+ $error = "function name \"$functionname\" must only contain letters";
$phpcsfile->adderror($error, $stackptr);
return;
class moodle_sniffs_namingconventions_validvariablenamesniff extends php_codesniffer_standards_abstractvariablesniff
{
- private $allowed_global_vars = array('CFG', 'SESSION', 'USER', 'COURSE', 'SITE', 'PAGE', 'DB', 'THEME');
+ private $allowed_global_vars = array('CFG', 'SESSION', 'USER', 'COURSE', 'SITE', 'PAGE', 'DB', 'THEME',
+ '_SERVER', '_GET', '_POST', '_FILES', '_REQUEST', '_SESSION',
+ '_ENV', '_COOKIE', '_HTTP_RAW_POST_DATA');
/**
* Processes class member variables.
protected function processMemberVar(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";
+ $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;
}
protected function processVariable(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
$tokens = $phpcsfile->gettokens();
- $memberName = ltrim($tokens[$stackptr]['content'], '$');
- if (preg_match('/[A-Z]+/', $memberName)) {
- if (!in_array($memberName, $this->allowed_global_vars)) {
- $error = "Member variable \"$memberName\" must be all lower-case";
+ $membername = ltrim($tokens[$stackptr]['content'], '$');
+ if (preg_match('/[A-Z]+/', $membername)) {
+ if (!in_array($membername, $this->allowed_global_vars)) {
+ $error = "Member variable \"$membername\" must be all lower-case";
$phpcsfile->adderror($error, $stackptr);
return;
}
*
* @return void
*/
- protected function processVariableInString(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ protected function processvariableinstring(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
+
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;
+ $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;
}
}
-
return;
-
}
-
-
}
-
-?>
}
$tokens = $phpcsfile->gettokens();
- $openTag = $tokens[$stackptr];
+ $opentag = $tokens[$stackptr];
- if ($openTag['content'] === '<?') {
- $error = 'Short PHP opening tag used. Found "'.$openTag['content'].'" Expected "<?php".';
+ if ($opentag['content'] === '<?') {
+ $error = 'Short PHP opening tag used. Found "'.$opentag['content'].'" Expected "<?php".';
$phpcsfile->adderror($error, $stackptr);
}
- if ($openTag['code'] === T_OPEN_TAG_WITH_ECHO) {
+ if ($opentag['code'] === T_OPEN_TAG_WITH_ECHO) {
$nextVar = $tokens[$phpcsfile->findNext(PHP_CodeSniffer_tokens::$emptyTokens, ($stackptr + 1), null, true)];
$error = 'Short PHP opening tag used with echo. Found "';
- $error .= $openTag['content'].' '.$nextVar['content'].' ..." but expected "<?php echo '.$nextVar['content'].' ...".';
+ $error .= $opentag['content'].' '.$nextVar['content'].' ..." but expected "<?php echo '.$nextVar['content'].' ...".';
$phpcsfile->adderror($error, $stackptr);
}
* @return array
*/
public function register() {
- return array(T_IF, T_FOR, T_FOREACH, T_WHILE, T_SWITCH);
+ return array(T_IF, T_FOR, T_FOREACH, T_WHILE, T_SWITCH, T_TRY, T_CATCH);
}
$tokens = $phpcsfile->gettokens();
$previoustoken = $stackptr - 1;
- while ($tokens[$previoustoken]['line'] == $tokens[$stackptr]['line']) {
- $previoustoken = $phpcsfile->findprevious(T_WHITESPACE, ($previoustoken - 1), null, true);
- }
+ // Move back until we find the previous non-whitespace, non-comment token
+ do {
+ $previoustoken = $phpcsfile->findprevious(array(T_WHITESPACE, T_COMMENT, T_DOC_COMMENT), ($previoustoken - 1), null, true);
+ } while ($tokens[$previoustoken]['line'] == $tokens[$stackptr]['line']);
$previous_non_ws_token = $tokens[$previoustoken];
+ // If this token is immediately on the line before this control structure, print a warning
if ($previous_non_ws_token['line'] == ($tokens[$stackptr]['line'] - 1)) {
$phpcsfile->addWarning('You should add a blank line before control structures', $stackptr);
}
// as if this is a method with tokens before it (public, static etc)
// or an if with an else before it, then we need to start the scope
// checking from there, rather than the current token.
- $lineStart = ($stackptr - 1);
- for ($lineStart; $lineStart > 0; $lineStart--) {
- if (strpos($tokens[$lineStart]['content'], $phpcsfile->eolChar) !== false) {
+ $linestart = ($stackptr - 1);
+ for ($linestart; $linestart > 0; $linestart--) {
+ if (strpos($tokens[$linestart]['content'], $phpcsfile->eolChar) !== false) {
break;
}
}
// We found a new line, now go forward and find the first non-whitespace
// token.
- $lineStart = $phpcsfile->findNext(array(T_WHITESPACE), ($lineStart + 1), null, true);
+ $linestart = $phpcsfile->findNext(array(T_WHITESPACE), ($linestart + 1), null, true);
- $startColumn = $tokens[$lineStart]['column'];
- $scopeStart = $tokens[$stackptr]['scope_opener'];
- $scopeEnd = $tokens[$stackptr]['scope_closer'];
+ $startColumn = $tokens[$linestart]['column'];
+ $scopestart = $tokens[$stackptr]['scope_opener'];
+ $scopeend = $tokens[$stackptr]['scope_closer'];
// Check that the closing brace is on its own line.
- $lastcontent = $phpcsfile->findPrevious(array(T_WHITESPACE), ($scopeEnd - 1), $scopeStart, true);
- if ($tokens[$lastcontent]['line'] === $tokens[$scopeEnd]['line']) {
+ $lastcontent = $phpcsfile->findPrevious(array(T_WHITESPACE), ($scopeend - 1), $scopestart, true);
+ if ($tokens[$lastcontent]['line'] === $tokens[$scopeend]['line']) {
$error = 'Closing brace must be on a line by itself';
- $phpcsfile->adderror($error, $scopeEnd);
+ $phpcsfile->adderror($error, $scopeend);
return;
}
// Check now that the closing brace is lined up correctly.
- $braceIndent = $tokens[$scopeEnd]['column'];
- $isBreakCloser = ($tokens[$scopeEnd]['code'] === T_BREAK);
+ $braceIndent = $tokens[$scopeend]['column'];
+ $isBreakCloser = ($tokens[$scopeend]['code'] === T_BREAK);
if (in_array($tokens[$stackptr]['code'], array(T_CASE, T_DEFAULT)) === true && $isBreakCloser === true) {
// BREAK statements should be indented 4 spaces from the
// CASE or DEFAULT statement.
if ($braceIndent !== ($startColumn + 4)) {
$error = 'Break statement indented incorrectly; expected '.($startColumn + 3).' spaces, found '.($braceIndent - 1);
- $phpcsfile->adderror($error, $scopeEnd);
+ $phpcsfile->adderror($error, $scopeend);
}
} else {
if (in_array($tokens[$stackptr]['code'], array(T_CASE, T_DEFAULT))) {
if ($braceIndent !== $startColumn) {
$error = 'Closing brace indented incorrectly; expected '.($startColumn - 1).' spaces, found '.($braceIndent - 1);
- $phpcsfile->adderror($error, $scopeEnd);
+ $phpcsfile->adderror($error, $scopeend);
}
}