/**
* File containing the Class Declaration Test.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-classes
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-classes
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
}
$curlybrace = $tokens[$stackptr]['scope_opener'];
- $lastcontent = $phpcsfile->findPrevious(T_WHITESPACE, ($curlybrace - 1), $stackptr, true);
+ $lastcontent = $phpcsfile->findprevious(T_WHITESPACE, ($curlybrace - 1), $stackptr, true);
$classline = $tokens[$lastcontent]['line'];
$braceline = $tokens[$curlybrace]['line'];
/**
* File containing the moodle_sniffs_codeanalysis_emptystatementsniff Class
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-codeanalysis
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-codeanalysis
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* <b>elsif</b>, <b>for</b>, <b>foreach<b>, <b>if</b>, <b>switch</b>, <b>try</b>
* and <b>while</b>.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class moodle_sniffs_codeanalysis_emptystatementsniff implements php_codesniffer_sniff
/**
* This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-codeanalysis
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-codeanalysis
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* }
* </code>
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class moodle_sniffs_codeanalysis_forloopshouldbewhileloopsniff implements php_codesniffer_sniff
/**
* This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-codeanalysis
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-codeanalysis
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* }
* </code>
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class moodle_sniffs_codeanalysis_forloopwithtestfunctioncallsniff implements php_codesniffer_sniff
// Find next non empty token, if it is a open curly brace we have a
// function call.
- $index = $phpcsfile->findNext(PHP_CodeSniffer_tokens::$emptyTokens, ($next + 1), null, true);
+ $index = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, ($next + 1), null, true);
if ($tokens[$index]['code'] === T_OPEN_PARENTHESIS) {
$error = 'Avoid function calls in a FOR loop test part';
/**
* This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-codeanalysis
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-codeanalysis
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* }
* </code>
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class moodle_sniffs_codeanalysis_jumbledincrementersniff implements php_codesniffer_sniff
/**
* This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-codeanalysis
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-codeanalysis
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* }
* </code>
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class moodle_sniffs_codeanalysis_unconditionalifstatementsniff implements php_codesniffer_sniff
/**
* This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-codeanalysis
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-codeanalysis
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* }
* </code>
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class moodle_sniffs_codeanalysis_unnecessaryfinalmodifiersniff implements php_codesniffer_sniff
}
// Fetch previous token.
- $prev = $phpcsfile->findPrevious(PHP_CodeSniffer_tokens::$emptyTokens, ($stackptr - 1), null, true);
+ $prev = $phpcsfile->findprevious(PHP_CodeSniffer_tokens::$emptyTokens, ($stackptr - 1), null, true);
// Skip for non final class.
if ($prev === false || $tokens[$prev]['code'] !== T_FINAL) {
/**
* This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-codeanalysis
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-codeanalysis
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* interface that defines multiple methods but the implementation only needs some
* of them.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class moodle_sniffs_codeanalysis_unusedfunctionparametersniff implements php_codesniffer_sniff
}
$params = array();
- foreach ($phpcsfile->getmethodParameters($stackptr) as $param) {
+ foreach ($phpcsfile->getmethodparameters($stackptr) as $param) {
$params[$param['name']] = $stackptr;
}
return;
} else if ($code === T_RETURN && $emptyBody === true) {
// Return statement and an empty body indicate an interface method.
- $tmp = $phpcsfile->findNext(PHP_CodeSniffer_tokens::$emptyTokens, ($next + 1), null, true);
+ $tmp = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, ($next + 1), null, true);
if ($tmp === false) {
return;
}
return;
}
- $tmp = $phpcsfile->findNext(PHP_CodeSniffer_tokens::$emptyTokens, ($tmp + 1), null, true);
+ $tmp = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, ($tmp + 1), null, true);
// There is a return <token>.
if ($tmp !== false && $tokens[$tmp] === T_SEMICOLON) {
}
-
-?>
/**
* This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-codeanalysis
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-codeanalysis
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* }
* </code>
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_codeanalysis_uselessoverridingmethodsniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_codeanalysis_uselessoverridingmethodsniff implements php_codesniffer_sniff {
/**
// Get all parameters from method signature.
$signature = array();
- foreach ($phpcsfile->getmethodParameters($stackptr) as $param) {
+ foreach ($phpcsfile->getmethodparameters($stackptr) as $param) {
$signature[] = $param['name'];
}
}
// Find next non empty token index, should be double colon.
- $next = $phpcsfile->findNext(PHP_CodeSniffer_tokens::$emptyTokens, ($next + 1), null, true);
+ $next = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, ($next + 1), null, true);
// Skip for invalid code.
if ($next === false || $tokens[$next]['code'] !== T_DOUBLE_COLON) {
}
// Find next non empty token index, should be the function name.
- $next = $phpcsfile->findNext(PHP_CodeSniffer_tokens::$emptyTokens, ($next + 1), null, true);
+ $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) {
}
// Find next non empty token index, should be the open parenthesis.
- $next = $phpcsfile->findNext(PHP_CodeSniffer_tokens::$emptyTokens, ($next + 1), null, true);
+ $next = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, ($next + 1), null, true);
// Skip for invalid code.
if ($next === false || $tokens[$next]['code'] !== T_OPEN_PARENTHESIS) {
return;
}
- $validParameterTypes = array(
+ $validparametertypes = array(
T_VARIABLE,
T_LNUMBER,
T_CONSTANT_ENCAPSED_STRING,
}
}
- $next = $phpcsfile->findNext(PHP_CodeSniffer_tokens::$emptyTokens, ($next + 1), null, true);
+ $next = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, ($next + 1), null, true);
if ($next === false || $tokens[$next]['code'] !== T_SEMICOLON) {
return;
}
}
-
-?>
/**
* Parses and verifies the doc comments for classes.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-commenting
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-commenting
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (class_exists('PHP_CodeSniffer_CommentParser_ClassCommentParser', true) === false) {
* <li>Check required and optional tags and the format of their content.</li>
* </ul>
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class moodle_sniffs_commenting_classcommentsniff extends moodle_sniffs_commenting_filecommentsniff {
);
// 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) {
$phpcsfile->adderror("You must use \"/**\" style comments for a $type 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));
+ $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);
+ $prevnoncomment = $phpcsfile->findprevious(T_DOC_COMMENT, ($commentstart - 1), null, true);
if ($prevnoncomment !== false) {
- $prevcomment = $phpcsfile->findPrevious(T_DOC_COMMENT, ($prevnoncomment - 1));
+ $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);
+ $newlinetoken = $phpcsfile->findnext(T_WHITESPACE, ($newlinetoken + 1), $stackptr, false, $phpcsfile->eolChar);
if ($newlinetoken !== false) {
// Blank line between the class and the doc block.
try {
$this->commentparser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsfile);
$this->commentparser->parse();
+
} catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
$line = ($e->getlinewithinComment() + $commentstart);
$phpcsfile->adderror($e->getMessage(), $line);
$long = $comment->getlongcomment();
if (empty($long) === false) {
- $between = $comment->getWhiteSpaceBetween();
+ $between = $comment->getWhiteSpacebetween();
$newlinebetween = substr_count($between, $phpcsfile->eolChar);
if ($newlinebetween !== 2) {
/**
* Parses and verifies the doc comments for files.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-commenting
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-commenting
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (class_exists('PHP_CodeSniffer_CommentParser_ClassCommentParser', true) === false) {
* <li>Check required and optional tags and the format of their content.</li>
* </ul>
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
'category' => array(
'required' => false,
'allow_multiple' => false,
- 'order_text' => 'precedes @package',
+ 'order_text' => 'precedes @subpackage',
),
'package' => array(
'required' => true,
'subpackage' => array(
'required' => false,
'allow_multiple' => false,
- 'order_text' => 'follows @package',
+ 'order_text' => 'follows @subpackage',
),
'author' => array(
'required' => false,
'allow_multiple' => true,
- 'order_text' => 'follows @subpackage (if used) or @package',
+ 'order_text' => 'follows @subsubpackage (if used) or @subpackage',
),
'copyright' => array(
'required' => true,
),
);
+ protected $core_folders = array('admin', 'backup', 'blog', 'calendar', 'course',
+ 'error', 'group', 'iplookup', 'lib', 'login',
+ 'message', 'mnet', 'my', 'notes', 'pix', 'rss',
+ 'tag', 'user', 'userpix');
/**
* Returns an array of tokens this test wants to listen for.
// We are only interested if this is the first open tag.
if ($stackptr !== 0) {
- if ($phpcsfile->findPrevious(T_OPEN_TAG, ($stackptr - 1)) !== false) {
+ if ($phpcsfile->findprevious(T_OPEN_TAG, ($stackptr - 1)) !== false) {
return;
}
}
if ($tagelement instanceof PHP_CodeSniffer_CommentParser_Singleelement) {
if ($tagelement->getcontent() !== '') {
- return (strlen($tagname) + substr_count($tagelement->getWhitespaceBeforecontent(), ' '));
+ 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(), ' '));
+ return (strlen($tagname) + substr_count($tagelement->getwhitespacebeforevalue(), ' '));
}
}
protected function processpackage($errorpos) {
global $CFG;
$package = $this->commentparser->getPackage();
+
$filename = str_replace($CFG->dirroot, '', $this->currentfile->getfilename());
+ $path_parts = explode('/', $filename);
- // Replace slashes or backslashes in file path with dashes
- $expected_package = strtolower(str_replace('/', '-', $filename));
+ if (in_array($path_parts[1], $this->core_folders)) {
+ $expected_package = 'moodlecore';
- if (strpos($expected_package, '-')) {
- $expected_package = strtolower(str_replace('\\', '-', $filename));
- }
+ } else {
+
+ // Replace slashes or backslashes in file path with dashes
+ $expected_package = strtolower(str_replace('/', '-', $filename));
- // Strip off last part: the name of the searched file
- $expected_package = substr($expected_package, 0, strrpos($expected_package, '-'));
+ if (strpos($expected_package, '-')) {
+ $expected_package = strtolower(str_replace('\\', '-', $filename));
+ }
- // Remove first dash if present
- $expected_package = ltrim($expected_package, '-');
+ // Strip off last part: the name of the searched file
+ $expected_package = substr($expected_package, 0, strrpos($expected_package, '-'));
+
+ // Remove first dash if present
+ $expected_package = ltrim($expected_package, '-');
+ }
if ($package !== null) {
$content = $package->getcontent();
}
} else {
- $error = '@package tag must contain a name';
+ $error = '@subpackage tag must contain a name';
$this->currentfile->adderror($error, $errorpos);
}
}
$validname = trim($newname, '_');
$error = "Subpackage name \"$content\" is not valid; consider \"$validname\" instead";
- $this->currentfile->adderror($error, $errorpos);
+ // $this->currentfile->adderror($error, $errorpos);
}
} else {
/**
* Parses and verifies the doc comments for functions.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-commenting
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-commenting
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (class_exists('PHP_CodeSniffer_CommentParser_FunctionCommentParser', true) === false) {
* <li>There is a blank newline after the short description.</li>
* <li>There is a blank newline between the long and short description.</li>
* <li>There is a blank newline between the long description and tags.</li>
- * <li>Parameter names represent those in the method.</li>
- * <li>Parameter comments are in the correct order</li>
- * <li>Parameter comments are complete</li>
+ * <li>parameter names represent those in the method.</li>
+ * <li>parameter comments are in the correct order</li>
+ * <li>parameter comments are complete</li>
* <li>A space is present before the first and after the last parameter</li>
* <li>A return type exists</li>
* <li>There must be one blank line between body and headline comments.</li>
* <li>Any throw tag must have an exception class.</li>
* </ul>
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_commenting_functioncommentsniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_commenting_functioncommentsniff implements php_codesniffer_sniff {
/**
* The name of the method that we are currently processing.
*
* @var PHP_CodeSniffer_Comment_Parser_FunctionCommentParser
*/
- protected $commentParser = null;
+ protected $commentparser = null;
/**
* The current PHP_CodeSniffer_File object we are processing.
*
* @var PHP_CodeSniffer_File
*/
- protected $currentFile = null;
+ protected $currentfile = null;
/**
*
* @return array
*/
- public function register()
- {
+ public function register() {
return array(T_FUNCTION);
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$find = array(
T_COMMENT,
T_DOC_COMMENT,
T_OPEN_TAG,
);
- $commentend = $phpcsfile->findPrevious($find, ($stackptr - 1));
+ $commentend = $phpcsfile->findprevious($find, ($stackptr - 1));
if ($commentend === false) {
return;
}
- $this->currentFile = $phpcsfile;
+ $this->currentfile = $phpcsfile;
$tokens = $phpcsfile->gettokens();
// If the token that we found was a class or a function, then this
$error = 'You must use "/**" style comments for a function comment';
$phpcsfile->adderror($error, $stackptr);
return;
+
} else if ($code !== T_DOC_COMMENT) {
$phpcsfile->adderror('Missing function doc comment', $stackptr);
return;
$ignore[] = T_WHITESPACE;
$ignore[] = T_ABSTRACT;
$ignore[] = T_FINAL;
- $prevtoken = $phpcsfile->findPrevious($ignore, ($stackptr - 1), null, true);
+ $prevtoken = $phpcsfile->findprevious($ignore, ($stackptr - 1), null, true);
+
if ($prevtoken !== $commentend) {
$phpcsfile->adderror('Missing function doc comment', $stackptr);
return;
$this->_functiontoken = $stackptr;
- foreach ($tokens[$stackptr]['conditions'] as $condPtr => $condition) {
+ foreach ($tokens[$stackptr]['conditions'] as $condptr => $condition) {
+
if ($condition === T_CLASS || $condition === T_INTERFACE) {
- $this->_classtoken = $condPtr;
+ $this->_classtoken = $condptr;
break;
}
}
// 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) {
+
+ if ($stackptr === 0 || $phpcsfile->findprevious(T_OPEN_TAG, ($prevtoken - 1)) === false) {
$phpcsfile->adderror('Missing function doc comment', $stackptr);
return;
}
$this->_methodname = $phpcsfile->getDeclarationname($stackptr);
try {
- $this->commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsfile);
- $this->commentParser->parse();
+ $this->commentparser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsfile);
+ $this->commentparser->parse();
+
} catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
$line = ($e->getlinewithinComment() + $commentstart);
$phpcsfile->adderror($e->getMessage(), $line);
return;
}
- $comment = $this->commentParser->getComment();
+ $comment = $this->commentparser->getComment();
+
if (is_null($comment) === true) {
$error = 'Function doc comment is empty';
$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;
- $newlineSpan = strspn($short, $phpcsfile->eolChar);
- if ($short !== '' && $newlineSpan > 0) {
- $line = ($newlineSpan > 1) ? 'newlines' : 'newline';
+ $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));
}
// 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 must be exactly one blank line between descriptions in function comment';
$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) {
+ $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));
*
* @return void
*/
- protected function processThrows($commentstart)
- {
- if (count($this->commentParser->getThrows()) === 0) {
+ protected function processthrows($commentstart) {
+
+ if (count($this->commentparser->getthrows()) === 0) {
return;
}
- foreach ($this->commentParser->getThrows() as $throw) {
+ foreach ($this->commentparser->getthrows() as $throw) {
- $exception = $throw->getValue();
+ $exception = $throw->getvalue();
$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);
}
}
-
}
*
* @return void
*/
- protected function processReturn($commentstart, $commentend)
- {
+ protected function processreturn($commentstart, $commentend) {
// Skip constructor and destructor.
$classname = '';
+
if ($this->_classtoken !== null) {
- $classname = $this->currentFile->getDeclarationname($this->_classtoken);
+ $classname = $this->currentfile->getdeclarationname($this->_classtoken);
$classname = strtolower(ltrim($classname, '_'));
}
if ($isspecialmethod === false && $methodname !== $classname) {
// Report missing return tag.
- if ($this->commentParser->getReturn() === null) {
+ if ($this->commentparser->getreturn() === null) {
$error = 'Missing @return tag in function comment';
- $this->currentFile->adderror($error, $commentend);
- } else if (trim($this->commentParser->getReturn()->getRawcontent()) === '') {
+ $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.
*
*
* @return void
*/
- protected function processParams($commentstart)
- {
- $realParams = $this->currentFile->getmethodParameters($this->_functiontoken);
+ protected function processparams($commentstart) {
+ $realparams = $this->currentfile->getmethodparameters($this->_functiontoken);
- $params = $this->commentParser->getParams();
- $foundParams = array();
+ $params = $this->commentparser->getparams();
+ $foundparams = array();
if (empty($params) === false) {
- $lastParm = (count($params) - 1);
- if (substr_count($params[$lastParm]->getWhitespaceAfter(), $this->currentFile->eolChar) !== 2) {
+ $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.
+ // parameters must appear immediately after the comment.
if ($params[0]->getOrder() !== 2) {
- $error = 'Parameters must appear immediately after the comment';
+ $error = 'parameters must appear immediately after the comment';
$errorpos = ($params[0]->getline() + $commentstart);
- $this->currentFile->adderror($error, $errorpos);
+ $this->currentfile->adderror($error, $errorpos);
}
- $previousParam = null;
- $spaceBeforeVar = 10000;
- $spaceBeforeComment = 10000;
- $longestType = 0;
- $longestVar = 0;
+ $previousparam = null;
+ $spacebeforevar = 10000;
+ $spacebeforecomment = 10000;
+ $longesttype = 0;
+ $longestvar = 0;
foreach ($params as $param) {
- $paramComment = trim($param->getComment());
+ $paramcomment = trim($param->getcomment());
$errorpos = ($param->getline() + $commentstart);
// Make sure that there is only one space before the var type.
- if ($param->getWhitespaceBeforeType() !== ' ') {
+ 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();
- $paramname = ($param->getVarname() !== '') ? $param->getVarname() : '[ UNKNOWN ]';
+ $paramname = ($param->getvarname() !== '') ? $param->getvarname() : '[ UNKNOWN ]';
- if ($previousParam !== null) {
- $previousname = ($previousParam->getVarname() !== '') ? $previousParam->getVarname() : 'UNKNOWN';
+ if ($previousparam !== null) {
+ $previousname = ($previousparam->getvarname() !== '') ? $previousparam->getvarname() : 'UNKNOWN';
// Check to see if the parameters align properly.
- if ($param->alignsVariableWith($previousParam) === false) {
+ 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);
+ $this->currentfile->addwarning($error, $errorpos);
}
- if ($param->alignsCommentWith($previousParam) === false) {
+ if ($param->alignsCommentWith($previousparam) === false) {
$error = 'The comments for parameters '.$previousname.' ('.($pos - 1).') and '.$paramname.' ('.$pos.') do not align';
- $this->currentFile->addwarning($error, $errorpos);
+ $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;
+ if (isset($realparams[($pos - 1)]) === true) {
+ $realname = $realparams[($pos - 1)]['name'];
+ $foundparams[] = $realname;
// Append ampersand to name if passing by reference.
- if ($realParams[($pos - 1)]['pass_by_reference'] === true) {
+ if ($realparams[($pos - 1)]['pass_by_reference'] === true) {
$realname = '&'.$realname;
}
- if ($realname !== $param->getVarname()) {
+ 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() === '') {
+ if ($param->gettype() === '') {
$error = 'Missing type at position '.$pos;
- $this->currentFile->adderror($error, $errorpos);
+ $this->currentfile->adderror($error, $errorpos);
}
- if ($paramComment === '') {
+ if ($paramcomment === '') {
$error = 'Missing comment for param "'.$paramname.'" at position '.$pos;
- $this->currentFile->adderror($error, $errorpos);
+ $this->currentfile->adderror($error, $errorpos);
}
- $previousParam = $param;
+ $previousparam = $param;
}
- if ($spaceBeforeVar !== 1 && $spaceBeforeVar !== 10000 && $spaceBeforeComment !== 10000) {
+ if ($spacebeforevar !== 1 && $spacebeforevar !== 10000 && $spacebeforecomment !== 10000) {
$error = 'Expected 1 space after the longest type';
- $this->currentFile->adderror($error, $longestType);
+ $this->currentfile->adderror($error, $longesttype);
}
- if ($spaceBeforeComment !== 1 && $spaceBeforeComment !== 10000) {
+ if ($spacebeforecomment !== 1 && $spacebeforecomment !== 10000) {
$error = 'Expected 1 space after the longest variable name';
- $this->currentFile->adderror($error, $longestVar);
+ $this->currentfile->adderror($error, $longestvar);
}
}
$realnames = array();
- foreach ($realParams as $realParam) {
- $realnames[] = $realParam['name'];
+
+ foreach ($realparams as $realparam) {
+ $realnames[] = $realparam['name'];
}
// Report and missing comments.
- $diff = array_diff($realnames, $foundParams);
- foreach ($diff as $neededParam) {
+ $diff = array_diff($realnames, $foundparams);
+
+ foreach ($diff as $neededparam) {
+
if (count($params) !== 0) {
$errorpos = ($params[(count($params) - 1)]->getline() + $commentstart);
+
} else {
$errorpos = $commentstart;
}
- $error = 'Doc comment for "'.$neededParam.'" missing';
- $this->currentFile->adderror($error, $errorpos);
+ $error = 'Doc comment for "'.$neededparam.'" missing';
+ $this->currentfile->adderror($error, $errorpos);
}
-
}
-
-
}
-
-?>
/**
* php_codesniffer_sniffs_moodle_commenting_inlinecommentsniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-commenting
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-commenting
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
*
* Checks that no perl-style comments are used.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_commenting_inlinecommentsniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_commenting_inlinecommentsniff implements php_codesniffer_sniff {
/**
*
* @return array
*/
- public function register()
- {
+ public function register() {
return array(T_COMMENT);
-
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
if ($tokens[$stackptr]['content']{0} === '#') {
$error .= ' or "/* comment */" instead.';
$phpcsfile->adderror($error, $stackptr);
}
-
}
-
-
}
-
-?>
/**
* Verifies that control statements conform to their coding standards.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-controlstructures
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-controlstructures
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (class_exists('PHP_CodeSniffer_Standards_AbstractPatternSniff', true) === false) {
/**
* Verifies that control statements conform to their coding standards.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_controlstructures_controlsignaturesniff extends php_codesniffer_standards_abstractpatternsniff
-{
+class moodle_sniffs_controlstructures_controlsignaturesniff extends php_codesniffer_standards_abstractpatternsniff {
/**
* Constructs a moodle_sniffs_controlstructures_controlsignaturesniff.
*/
- public function __construct()
- {
+ public function __construct() {
parent::__construct(true);
-
}
*
* @return array(string)
*/
- protected function getPatterns()
- {
- return array(
- 'do {EOL...} while (...);EOL',
- 'while (...) {EOL',
- 'for (...) {EOL',
- 'if (...) {EOL',
- 'foreach (...) {EOL',
- '} else if (...) {EOL',
- '} else {EOL',
- 'do {EOL',
- );
-
+ protected function getpatterns() {
+ return array('do {EOL...} while (...);EOL',
+ 'while (...) {EOL',
+ 'for (...) {EOL',
+ 'if (...) {EOL',
+ 'foreach (...) {EOL',
+ '} else if (...) {EOL',
+ '} else {EOL',
+ 'do {EOL',
+ 'try {EOL...} catch (...) {EOL...}');
}
-
-
}
-
-?>
/**
* moodle_sniffs_controlstructures_elseifdeclarationsniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-controlstructures
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-controlstructures
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Verifies that there are not elseif statements. The else and the if should
* be separated by a space.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_controlstructures_elseifdeclarationsniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_controlstructures_elseifdeclarationsniff implements php_codesniffer_sniff {
/**
*
* @return array
*/
- public function register()
- {
+ public function register() {
return array(T_ELSEIF);
-
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$error = 'Usage of ELSEIF not allowed. Use ELSE IF instead.';
$phpcsfile->adderror($error, $stackptr);
-
}
-
-
}
-
-
-?>
/**
* moodle_sniffs_controlstructures_inlinecontrolstructuresniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-controlstructures
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-controlstructures
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
*
* Verifies that inline control statements are not present.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_controlstructures_inlinecontrolstructuresniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_controlstructures_inlinecontrolstructuresniff implements php_codesniffer_sniff {
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
- public $supportedtokenizers = array(
- 'PHP',
- 'JS',
- );
+ public $supportedtokenizers = array('PHP', 'JS');
/**
* If true, an error will be thrown; otherwise a warning.
*
* @return array
*/
- public function register()
- {
- return array(
- T_IF,
- T_ELSE,
- T_FOREACH,
- T_WHILE,
- T_DO,
- T_SWITCH,
- T_FOR,
- );
-
+ public function register() {
+ return array(T_IF,
+ T_ELSE,
+ T_FOREACH,
+ T_WHILE,
+ T_DO,
+ T_SWITCH,
+ T_FOR);
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
if (isset($tokens[$stackptr]['scope_opener']) === false) {
if ($tokens[$stackptr]['code'] === T_WHILE) {
// This could be from a DO WHILE, which doesn't have an opening brace.
- $lastcontent = $phpcsfile->findPrevious(T_WHITESPACE, ($stackptr - 1), null, true);
+ $lastcontent = $phpcsfile->findprevious(T_WHITESPACE, ($stackptr - 1), null, true);
+
if ($tokens[$lastcontent]['code'] === T_CLOSE_CURLY_BRACKET) {
$brace = $tokens[$lastcontent];
+
if (isset($brace['scope_condition']) === true) {
$condition = $tokens[$brace['scope_condition']];
+
if ($condition['code'] === T_DO) {
return;
}
return;
}
-
}
-
-
}
-
-?>
/**
* File including class for sniffing out the closing PHP tag
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-files
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-files
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Class for sniffing out the closing PHP tag
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class moodle_sniffs_files_closingphptagsniff implements php_codesniffer_sniff {
$tokens = $phpcsfile->gettokens();
$last_token = end($tokens);
+
if ($last_token['code'] === T_CLOSE_TAG) {
$error = 'Closing PHP tag is not required at the end of the file. Please remove.';
$phpcsfile->addwarning($error, $stackptr);
/**
* moodle_sniffs_files_includingfilesniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-files
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-files
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* require_once is used elsewhere. Also checks that brackets do not surround
* the file being included.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_files_includingfilesniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_files_includingfilesniff implements php_codesniffer_sniff {
/**
* Conditions that should use include_once
*
* @var array(int)
*/
- private static $_conditions = array(
- T_IF,
- T_ELSE,
- T_ELSEIF,
- T_SWITCH,
- );
+ private static $_conditions = array(T_IF, T_ELSE, T_ELSEIF, T_SWITCH);
/**
*
* @return array
*/
- public function register()
- {
- return array(
- T_INCLUDE_ONCE,
- T_REQUIRE_ONCE,
- T_REQUIRE,
- T_INCLUDE,
- );
-
+ public function register() {
+ return array(T_INCLUDE_ONCE, T_REQUIRE_ONCE, T_REQUIRE, T_INCLUDE);
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
- //$nexttoken = $phpcsfile->findNext(PHP_CodeSniffer_tokens::$emptyTokens, ($stackptr + 1), null, true);
+ //$nexttoken = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, ($stackptr + 1), null, true);
//if ($tokens[$nexttoken]['code'] === T_OPEN_PARENTHESIS) {
// $error = '"'.$tokens[$stackptr]['content'].'"';
// $error .= ' is a statement, not a function; ';
// $phpcsfile->adderror($error, $stackptr);
//}
- $inCondition = (count($tokens[$stackptr]['conditions']) !== 0) ? true : false;
+ $incondition = (count($tokens[$stackptr]['conditions']) !== 0) ? true : false;
// Check to see if this including statement is within the parenthesis of a condition.
// If that's the case then we need to process it as being within a condition, as they
// are checking the return value.
if (isset($tokens[$stackptr]['nested_parenthesis']) === true) {
+
foreach ($tokens[$stackptr]['nested_parenthesis'] as $left => $right) {
+
if (isset($tokens[$left]['parenthesis_owner']) === true) {
- $inCondition = true;
+ $incondition = true;
}
}
}
// Check to see if they are assigning the return value of this including call.
// If they are then they are probably checking it, so its conditional.
- $previous = $phpcsfile->findPrevious(PHP_CodeSniffer_tokens::$emptyTokens, ($stackptr - 1), null, true);
+ $previous = $phpcsfile->findprevious(PHP_CodeSniffer_tokens::$emptyTokens, ($stackptr - 1), null, true);
+
if (in_array($tokens[$previous]['code'], PHP_CodeSniffer_tokens::$assignmentTokens) === true) {
// The have assigned the return value to it, so its conditional.
- $inCondition = true;
+ $incondition = true;
}
- $tokenCode = $tokens[$stackptr]['code'];
- if ($inCondition === true) {
+ $tokencode = $tokens[$stackptr]['code'];
+
+ if ($incondition === true) {
// We are inside a conditional statement. We need an include_once.
- if ($tokenCode === T_REQUIRE_ONCE) {
+ if ($tokencode === T_REQUIRE_ONCE) {
$error = 'File is being conditionally included; ';
$error .= 'use "include_once" instead';
$phpcsfile->adderror($error, $stackptr);
- } else if ($tokenCode === T_REQUIRE) {
+
+ } else if ($tokencode === T_REQUIRE) {
$error = 'File is being conditionally included; ';
$error .= 'use "include" instead';
$phpcsfile->adderror($error, $stackptr);
}
+
} else {
// We are unconditionally including, we need a require_once.
- if ($tokenCode === T_INCLUDE_ONCE) {
+ if ($tokencode === T_INCLUDE_ONCE) {
$error = 'File is being unconditionally included; ';
$error .= 'use "require_once" instead';
$phpcsfile->adderror($error, $stackptr);
- } else if ($tokenCode === T_INCLUDE) {
+
+ } else if ($tokencode === T_INCLUDE) {
$error = 'File is being unconditionally included; ';
$error .= 'use "require" instead';
$phpcsfile->adderror($error, $stackptr);
}
}
-
}
-
-
}
-
-?>
/**
* moodle_sniffs_files_lineendingssniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-files
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-files
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
*
* Checks that end of line characters are correct.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_files_lineendingssniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_files_lineendingssniff implements php_codesniffer_sniff {
/**
* The valid EOL character.
*
* @return array
*/
- public function register()
- {
+ public function register() {
return array(T_OPEN_TAG);
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
// We are only interested if this is the first open tag.
if ($stackptr !== 0) {
- if ($phpcsfile->findPrevious(T_OPEN_TAG, ($stackptr - 1)) !== false) {
+
+ if ($phpcsfile->findprevious(T_OPEN_TAG, ($stackptr - 1)) !== false) {
return;
}
}
}
}
-
-
}
-
-?>
/**
* moodle_sniffs_files_linelengthsniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-files
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-files
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* characters in length and errors if they are over 100. Both these
* figures can be changed by extending this sniff in your own standard.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_files_linelengthsniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_files_linelengthsniff implements php_codesniffer_sniff {
/**
* The limit that the length of a line should not exceed.
*
* @var int
*/
- protected $lineLimit = 120;
+ protected $linelimit = 120;
/**
* The limit that the length of a line must not exceed.
*
* @var int
*/
- protected $absolutelineLimit = 200;
+ protected $absolutelinelimit = 200;
/**
*
* @return array
*/
- public function register()
- {
+ public function register() {
return array(T_OPEN_TAG);
-
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $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;
}
$currentline = 1;
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);
+ $this->checklinelength($phpcsfile, ($tokencount - 1), $currentlinecontent);
$currentlinecontent = $tokens[$tokencount]['content'];
$currentline++;
}
}
- $this->checklineLength($phpcsfile, ($tokencount - 1), $currentlinecontent);
+ $this->checklinelength($phpcsfile, ($tokencount - 1), $currentlinecontent);
}
*
* @return void
*/
- protected function checklineLength(PHP_CodeSniffer_File $phpcsfile, $stackptr, $linecontent)
- {
+ protected function checklinelength(PHP_CodeSniffer_File $phpcsfile, $stackptr, $linecontent) {
// If the content is a CVS or SVN id in a version tag, or it is
// a license tag with a name and URL, there is nothing the
// developer can do to shorten the line, so don't throw errors.
if (preg_match('|@version[^\$]+\$Id|', $linecontent) === 0 && preg_match('|@license|', $linecontent) === 0) {
- $lineLength = strlen($linecontent);
- if ($this->absolutelineLimit > 0 && $lineLength > $this->absolutelineLimit) {
- $error = 'line exceeds maximum limit of '.$this->absolutelineLimit." characters; contains $lineLength characters";
+ $linelength = strlen($linecontent);
+
+ if ($this->absolutelinelimit > 0 && $linelength > $this->absolutelinelimit) {
+ $error = 'line exceeds maximum limit of '.$this->absolutelinelimit." characters; contains $linelength characters";
$phpcsfile->adderror($error, $stackptr);
- } else if ($lineLength > $this->lineLimit) {
- $warning = 'line exceeds '.$this->lineLimit." characters; contains $lineLength characters";
+
+ } else if ($linelength > $this->linelimit) {
+ $warning = 'line exceeds '.$this->linelimit." characters; contains $linelength characters";
$phpcsfile->addwarning($warning, $stackptr);
}
}
-
}
-
-
}
-
-?>
/**
* moodle_sniffs_formatting_spaceaftercastsniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-formatting
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-formatting
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
*
* Ensures there is a single space after cast tokens.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_formatting_spaceaftercastsniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_formatting_spaceaftercastsniff implements php_codesniffer_sniff {
/**
*
* @return array
*/
- public function register()
- {
+ public function register() {
return PHP_CodeSniffer_tokens::$castTokens;
-
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
if ($tokens[($stackptr + 1)]['code'] !== T_WHITESPACE) {
$error = 'A cast statement must be followed by a single space';
$phpcsfile->adderror($error, $stackptr);
}
-
}
-
-
}
-
-?>
/**
* moodle_sniffs_functions_functioncallargumentspacingsniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-functions
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-functions
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
*
* Checks that calls to methods and functions are spaced correctly.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_functions_functioncallargumentspacingsniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_functions_functioncallargumentspacingsniff implements php_codesniffer_sniff {
/**
*
* @return array
*/
- public function register()
- {
+ public function register() {
return array(T_STRING);
-
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
// Skip tokens that are the names of functions or classes
// "myFunction" is T_STRING but we should skip because it is not a
// function or method *call*.
$functionname = $stackptr;
- $functionKeyword = $phpcsfile->findPrevious(PHP_CodeSniffer_tokens::$emptyTokens, ($stackptr - 1), null, true);
- if ($tokens[$functionKeyword]['code'] === T_FUNCTION || $tokens[$functionKeyword]['code'] === T_CLASS) {
+ $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);
- if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
+ $openbracket = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, ($functionname + 1), null, true);
+ if ($tokens[$openbracket]['code'] !== T_OPEN_PARENTHESIS) {
return;
}
- $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
+ $closebracket = $tokens[$openbracket]['parenthesis_closer'];
+
+ $nextseparator = $openbracket;
- $nextSeperator = $openBracket;
- while (($nextSeperator = $phpcsfile->findNext(array(T_COMMA, T_VARIABLE), ($nextSeperator + 1), $closeBracket)) !== false) {
+ while (($nextseparator = $phpcsfile->findnext(array(T_COMMA, T_VARIABLE), ($nextseparator + 1), $closebracket)) !== false) {
// Make sure the comma or variable belongs directly to this function call,
// and is not inside a nested function call or array.
- $brackets = $tokens[$nextSeperator]['nested_parenthesis'];
- $lastBracket = array_pop($brackets);
- if ($lastBracket !== $closeBracket) {
+ $brackets = $tokens[$nextseparator]['nested_parenthesis'];
+ $lastbracket = array_pop($brackets);
+
+ if ($lastbracket !== $closebracket) {
continue;
}
- if ($tokens[$nextSeperator]['code'] === T_COMMA) {
- if ($tokens[($nextSeperator - 1)]['code'] === T_WHITESPACE) {
+ if ($tokens[$nextseparator]['code'] === T_COMMA) {
+
+ if ($tokens[($nextseparator - 1)]['code'] === T_WHITESPACE) {
$error = 'Space found before comma in function call';
$phpcsfile->addwarning($error, $stackptr);
}
- if ($tokens[($nextSeperator + 1)]['code'] !== T_WHITESPACE) {
+ if ($tokens[($nextseparator + 1)]['code'] !== T_WHITESPACE) {
$error = 'No space found after comma in function call';
$phpcsfile->addwarning($error, $stackptr);
+
} else {
// If there is a newline in the space, then the must be formatting
// each argument on a newline, which is valid, so ignore it.
- if (strpos($tokens[($nextSeperator + 1)]['content'], $phpcsfile->eolChar) === false) {
- $space = strlen($tokens[($nextSeperator + 1)]['content']);
+ if (strpos($tokens[($nextseparator + 1)]['content'], $phpcsfile->eolChar) === false) {
+ $space = strlen($tokens[($nextseparator + 1)]['content']);
+
if ($space > 1) {
$error = 'Expected 1 space after comma in function call; ';
$error .= $space.' found';
}
}
}
+
} else {
// token is a variable.
- $nexttoken = $phpcsfile->findNext(PHP_CodeSniffer_tokens::$emptyTokens, ($nextSeperator + 1), $closeBracket, true);
+ $nexttoken = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, ($nextseparator + 1), $closebracket, true);
+
if ($nexttoken !== false) {
+
if ($tokens[$nexttoken]['code'] === T_EQUAL) {
+
if (($tokens[($nexttoken - 1)]['code']) !== T_WHITESPACE) {
$error = 'Expected 1 space before = sign of default value';
$phpcsfile->addwarning($error, $stackptr);
}
}
}
-
}
-
-
}
-
-?>
/**
* moodle_sniffs_functions_functioncallsignaturesniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-functions
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-functions
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* moodle_sniffs_functions_functioncallsignaturesniff.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_functions_functioncallsignaturesniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_functions_functioncallsignaturesniff implements php_codesniffer_sniff {
/**
*
* @return array
*/
- public function register()
- {
+ public function register() {
return array(T_STRING);
-
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
// Find the next non-empty token.
- $next = $phpcsfile->findNext(PHP_CodeSniffer_tokens::$emptyTokens, ($stackptr + 1), null, true);
+ $next = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, ($stackptr + 1), null, true);
if ($tokens[$next]['code'] !== T_OPEN_PARENTHESIS) {
// Not a function call.
}
// Find the previous non-empty token.
- $previous = $phpcsfile->findPrevious(PHP_CodeSniffer_tokens::$emptyTokens, ($stackptr - 1), null, true);
+ $previous = $phpcsfile->findprevious(PHP_CodeSniffer_tokens::$emptyTokens, ($stackptr - 1), null, true);
+
if ($tokens[$previous]['code'] === T_FUNCTION) {
// It's a function definition, not a function call.
return;
if ($tokens[($closer - 1)]['code'] === T_WHITESPACE) {
// Checking this: $value = my_function(...[*]).
- $between = $phpcsfile->findNext(T_WHITESPACE, ($next + 1), null, true);
+ $between = $phpcsfile->findnext(T_WHITESPACE, ($next + 1), null, true);
// Only throw an error if there is some content between the parenthesis.
// IE. Checking for this: $value = my_function().
}
}
- $next = $phpcsfile->findNext(T_WHITESPACE, ($closer + 1), null, true);
+ $next = $phpcsfile->findnext(T_WHITESPACE, ($closer + 1), null, true);
if ($tokens[$next]['code'] === T_SEMICOLON) {
+
if (in_array($tokens[($closer + 1)]['code'], PHP_CodeSniffer_tokens::$emptyTokens) === true) {
$error = 'Space after closing parenthesis of function call prohibited';
$phpcsfile->adderror($error, $closer);
}
}
-
}
-
-
}
-?>
/**
* moodle_sniffs_functions_functiondeclarationsniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-functions
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-functions
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (class_exists('PHP_CodeSniffer_Standards_AbstractPatternSniff', true) === false) {
*
* Checks the function declaration is correct.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_functions_functiondeclarationsniff extends php_codesniffer_standards_abstractpatternsniff
-{
+class moodle_sniffs_functions_functiondeclarationsniff extends php_codesniffer_standards_abstractpatternsniff {
/**
*
* @return array
*/
- protected function getPatterns()
- {
+ protected function getpatterns() {
return array(
'function abc(...) {',
'abstract function abc(...);'
);
-
}
-
-
}
-
-?>
/**
* moodle_sniffs_functions_lowercasefunctionkeywordssniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-functions
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-functions
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
*
* Ensures all class keywords are lowercase.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_functions_lowercasefunctionkeywordssniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_functions_lowercasefunctionkeywordssniff implements php_codesniffer_sniff {
/**
*
* @return array
*/
- public function register()
- {
- return array(
- T_FUNCTION,
- T_PUBLIC,
- T_PRIVATE,
- T_PROTECTED,
- T_STATIC,
- );
-
+ public function register() {
+ return array(T_FUNCTION, T_PUBLIC, T_PRIVATE, T_PROTECTED, T_STATIC);
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
$content = $tokens[$stackptr]['content'];
+
if ($content !== strtolower($content)) {
$type = strtoupper($content);
$expected = strtolower($content);
$error = "$type keyword must be lowercase; expected \"$expected\" but found \"$content\"";
$phpcsfile->adderror($error, $stackptr);
}
-
}
-
-
}
-
-?>
/**
* moodle_sniffs_functions_validdefaultvaluesniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-functions
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-functions
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* A Sniff to ensure that parameters defined for a function that have a default
* value come at the end of the function signature.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_functions_validdefaultvaluesniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_functions_validdefaultvaluesniff implements php_codesniffer_sniff {
/**
*
* @return array
*/
- public function register()
- {
+ public function register() {
return array(T_FUNCTION);
-
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
$argstart = $tokens[$stackptr]['parenthesis_opener'];
// 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;
+ $defaultfound = false;
+
+ $nextarg = $argstart;
- $nextArg = $argstart;
- while (($nextArg = $phpcsfile->findNext(T_VARIABLE, ($nextArg + 1), $argend)) !== false) {
- $argHasDefault = self::_argHasDefault($phpcsfile, $nextArg);
- if (($argHasDefault === false) && ($defaultFound === true)) {
+ 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';
$error .= ' of the argument list';
- $phpcsfile->adderror($error, $nextArg);
+ $phpcsfile->adderror($error, $nextarg);
return;
}
- if ($argHasDefault === true) {
- $defaultFound = true;
+ if ($arghasdefault === true) {
+ $defaultfound = true;
}
}
* Returns true if the passed argument has a default value.
*
* @param PHP_CodeSniffer_File $phpcsfile The file being scanned.
- * @param int $argPtr The position of the argument
+ * @param int $argptr The position of the argument
* in the stack.
*
* @return bool
*/
- private static function _argHasDefault(PHP_CodeSniffer_File $phpcsfile, $argPtr)
- {
+ private static function _arghasdefault(PHP_CodeSniffer_File $phpcsfile, $argptr) {
$tokens = $phpcsfile->gettokens();
- $nexttoken = $phpcsfile->findNext(PHP_CodeSniffer_tokens::$emptyTokens, ($argPtr + 1), null, true);
+ $nexttoken = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, ($argptr + 1), null, true);
+
if ($tokens[$nexttoken]['code'] !== T_EQUAL) {
return false;
}
}
-
-?>
/**
* moodle_sniffs_namingconventions_uppercaseconstantnamesniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-namingconventions
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-namingconventions
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
*
* Ensures that constant names are all uppercase.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_namingconventions_uppercaseconstantnamesniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_namingconventions_uppercaseconstantnamesniff implements php_codesniffer_sniff {
/**
*
* @return array
*/
- public function register()
- {
+ public function register() {
return array(T_STRING);
-
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
$constname = $tokens[$stackptr]['content'];
// If the next non-whitespace token after this token
// is not an opening parenthesis then it is not a function call.
- $openBracket = $phpcsfile->findNext(array(T_WHITESPACE), ($stackptr + 1), null, true);
- if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
- $functionKeyword = $phpcsfile->findPrevious(array(T_WHITESPACE, T_COMMA, T_COMMENT, T_STRING), ($stackptr - 1), null, true);
-
- $declarations = array(
- T_FUNCTION,
- T_CLASS,
- T_INTERFACE,
- T_IMPLEMENTS,
- T_EXTENDS,
- T_INSTANCEOF,
- T_NEW,
- );
- if (in_array($tokens[$functionKeyword]['code'], $declarations) === true) {
+ $openbracket = $phpcsfile->findnext(array(T_WHITESPACE), ($stackptr + 1), null, true);
+
+ if ($tokens[$openbracket]['code'] !== T_OPEN_PARENTHESIS) {
+ $functionkeyword = $phpcsfile->findprevious(array(T_WHITESPACE, T_COMMA, T_COMMENT, T_STRING),
+ ($stackptr - 1), null, true);
+
+ $declarations = array(T_FUNCTION, T_CLASS, T_INTERFACE, T_IMPLEMENTS, T_EXTENDS, T_INSTANCEOF, T_NEW);
+
+ if (in_array($tokens[$functionkeyword]['code'], $declarations) === true) {
// This is just a declaration; no constants here.
return;
}
- if ($tokens[$functionKeyword]['code'] === T_CONST) {
+ 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";
+ $error = 'Class constants must be uppercase; expected '.strtoupper($constname).
+ " but found $constname";
$phpcsfile->adderror($error, $stackptr);
}
}
// Is this a class name?
- $nextPtr = $phpcsfile->findNext(array(T_WHITESPACE), ($stackptr + 1), null, true);
- if ($tokens[$nextPtr]['code'] === T_DOUBLE_COLON) {
+ $nextptr = $phpcsfile->findnext(array(T_WHITESPACE), ($stackptr + 1), null, true);
+
+ if ($tokens[$nextptr]['code'] === T_DOUBLE_COLON) {
return;
}
// Is this a type hint?
- if ($tokens[$nextPtr]['code'] === T_VARIABLE) {
+ if ($tokens[$nextptr]['code'] === T_VARIABLE) {
return;
- } else if ($phpcsfile->isReference($nextPtr) === true) {
+
+ } else if ($phpcsfile->isReference($nextptr) === true) {
return;
}
// Is this a member var name?
- $prevPtr = $phpcsfile->findPrevious(array(T_WHITESPACE), ($stackptr - 1), null, true);
- if ($tokens[$prevPtr]['code'] === T_OBJECT_OPERATOR) {
+ $prevptr = $phpcsfile->findprevious(array(T_WHITESPACE), ($stackptr - 1), null, true);
+
+ if ($tokens[$prevptr]['code'] === T_OBJECT_OPERATOR) {
return;
}
// Is this an instance of declare()
- $prevPtr = $phpcsfile->findPrevious(array(T_WHITESPACE, T_OPEN_PARENTHESIS), ($stackptr - 1), null, true);
- if ($tokens[$prevPtr]['code'] === T_DECLARE) {
+ $prevptr = $phpcsfile->findprevious(array(T_WHITESPACE, T_OPEN_PARENTHESIS), ($stackptr - 1), null, true);
+
+ if ($tokens[$prevptr]['code'] === T_DECLARE) {
return;
}
*/
// The next non-whitespace token must be the constant name.
- $constPtr = $phpcsfile->findNext(array(T_WHITESPACE), ($openBracket + 1), null, true);
- if ($tokens[$constPtr]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
+ $constptr = $phpcsfile->findnext(array(T_WHITESPACE), ($openbracket + 1), null, true);
+
+ if ($tokens[$constptr]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
return;
}
- $constname = $tokens[$constPtr]['content'];
+ $constname = $tokens[$constptr]['content'];
+
if (strtoupper($constname) !== $constname) {
$error = 'Constants must be uppercase; expected '.strtoupper($constname)." but found $constname";
$phpcsfile->adderror($error, $stackptr);
}
}
-
}
-
-
}
-
-?>
/**
* moodle_sniffs_namingconventions_validclassnamesniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-namingconventions
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-namingconventions
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Ensures class and interface names start with a capital letter
* and use _ separators.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_namingconventions_validclassnamesniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_namingconventions_validclassnamesniff implements php_codesniffer_sniff {
/**
*
* @return array
*/
- public function register()
- {
- return array(
- T_CLASS,
- T_INTERFACE,
- );
-
+ public function register() {
+ return array(T_CLASS, T_INTERFACE);
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
- $classname = $phpcsfile->findNext(T_STRING, $stackptr);
+ $classname = $phpcsfile->findnext(T_STRING, $stackptr);
$name = trim($tokens[$classname]['content']);
// Make sure that the word is all lowercase
$error = ucfirst($tokens[$stackptr]['content']).' name is not valid, must be all lower-case';
$phpcsfile->adderror($error, $stackptr);
}
-
}
-
-
}
-
-
-?>
/**
* moodle_sniffs_namingconventions_validfunctionnamesniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-namingconventions
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-namingconventions
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (class_exists('PHP_CodeSniffer_Standards_AbstractScopeSniff', true) === false) {
* Ensures method names are correct depending on whether they are public
* or private, and that functions are named correctly.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_namingconventions_validfunctionnamesniff extends php_codesniffer_standards_abstractscopesniff
-{
+class moodle_sniffs_namingconventions_validfunctionnamesniff extends php_codesniffer_standards_abstractscopesniff {
/**
* A list of all PHP magic methods.
*
* @var array
*/
- private $_magicFunctions = array(
- 'autoload',
- );
+ private $_magicfunctions = array('autoload');
/**
* Constructs a moodle_sniffs_namingconventions_validfunctionnamesniff.
*/
- public function __construct()
- {
+ public function __construct() {
parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true);
-
}
* @param PHP_CodeSniffer_File $phpcsfile The file being processed.
* @param int $stackptr The position where this token was
* found.
- * @param int $currScope The position of the current scope.
+ * @param int $currscope The position of the current scope.
*
* @return void
*/
- protected function processtokenwithinScope(PHP_CodeSniffer_File $phpcsfile, $stackptr, $currScope)
- {
- $classname = $phpcsfile->getDeclarationname($currScope);
- $methodname = $phpcsfile->getDeclarationname($stackptr);
+ protected function processtokenwithinscope(PHP_CodeSniffer_File $phpcsfile, $stackptr, $currscope) {
+ $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";
+ $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);
}
return;
}
- $methodProps = $phpcsfile->getmethodProperties($stackptr);
- $isPublic = ($methodProps['scope'] === 'private') ? false : true;
- $scope = $methodProps['scope'];
- $scopeSpecified = $methodProps['scope_specified'];
+ $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 ($scopeSpecified === true) {
+
+ if ($scopespecified === true) {
$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";
// No numbers accepted
if (preg_match('/[0-9]+/', $methodname)) {
- if ($scopeSpecified === true) {
+
+ if ($scopespecified === true) {
$error = ucfirst($scope)." method name \"$classname::$methodname\" must only contain letters";
} else {
$error = "Method name \"$classname::$methodname\" must only contain letters";
*
* @return void
*/
- protected function processtokenOutsideScope(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
- $functionname = $phpcsfile->getDeclarationname($stackptr);
+ protected function processtokenoutsidescope(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
+ $functionname = $phpcsfile->getdeclarationname($stackptr);
// Is this a magic function. IE. is prefixed with "__".
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";
+ $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';
$phpcsfile->adderror($error, $stackptr);
}
}
-
-?>
/**
* moodle_sniffs_namingconventions_validvariablenamesniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-namingconventions
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-namingconventions
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (class_exists('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false) {
*
* Checks the naming of member variables.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_namingconventions_validvariablenamesniff extends php_codesniffer_standards_abstractvariablesniff
-{
+class moodle_sniffs_namingconventions_validvariablenamesniff extends php_codesniffer_standards_abstractvariablesniff {
private $allowed_global_vars = array('CFG', 'SESSION', 'USER', 'COURSE', 'SITE', 'PAGE', 'DB', 'THEME',
'_SERVER', '_GET', '_POST', '_FILES', '_REQUEST', '_SESSION',
*
* @return void
*/
- protected function processMemberVar(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ 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";
$phpcsfile->adderror($error, $stackptr);
}
// Must not be preceded by 'var' keyword
- $keyword = $phpcsfile->findPrevious(T_VAR, $stackptr);
+ $keyword = $phpcsfile->findprevious(T_VAR, $stackptr);
+
if ($tokens[$keyword]['line'] == $tokens[$stackptr]['line']) {
- $error = "The 'var' keyword is not permitted. Visibility must be explicitly declared with public, private or protected";
+ $error = "The 'var' keyword is not permitted." .
+ 'Visibility must be explicitly declared with public, private or protected';
$phpcsfile->adderror($error, $stackptr);
return;
}
-
}
*
* @return void
*/
- protected function processVariable(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ 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";
$phpcsfile->adderror($error, $stackptr);
return;
}
}
-
}
/**
* moodle_sniffs_php_disallowshortopentagsniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-php
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-php
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
*
* Makes sure that shorthand PHP open tags are not used.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_php_disallowshortopentagsniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_php_disallowshortopentagsniff implements php_codesniffer_sniff {
/**
*
* @return array
*/
- public function register()
- {
- return array(
- T_OPEN_TAG,
- T_OPEN_TAG_WITH_ECHO,
- );
-
+ public function register() {
+ return array(T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO);
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
// If short open tags are off, then any short open tags will be converted
// to inline_html tags so we can just ignore them.
// If its on, then we want to ban the use of them.
}
if ($opentag['code'] === T_OPEN_TAG_WITH_ECHO) {
- $nextVar = $tokens[$phpcsfile->findNext(PHP_CodeSniffer_tokens::$emptyTokens, ($stackptr + 1), null, true)];
+ $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);
}
-
}
-
-
}
-
-?>
/**
* moodle_sniffs_php_lowercaseconstantsniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-php
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-php
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
*
* Checks that all uses of true, false and null are lowerrcase.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_php_lowercaseconstantsniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_php_lowercaseconstantsniff implements php_codesniffer_sniff {
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
- public $supportedtokenizers = array(
- 'PHP',
- 'JS',
- );
+ public $supportedtokenizers = array('PHP', 'JS');
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
- public function register()
- {
- return array(
- T_TRUE,
- T_FALSE,
- T_NULL,
- );
+ public function register() {
+ return array(T_TRUE, T_FALSE, T_NULL);
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
$keyword = $tokens[$stackptr]['content'];
+
if (strtolower($keyword) !== $keyword) {
- $error = 'TRUE, FALSE and NULL must be lowercase; expected "'.strtolower($keyword).'" but found "'.$keyword.'"';
+ $error = 'TRUE, FALSE and NULL must be lowercase; expected "'.
+ strtolower($keyword).'" but found "'.$keyword.'"';
$phpcsfile->adderror($error, $stackptr);
}
-
}
-
-
}
-
-?>
/**
* moodle_sniffs_php_lowercasephpfunctionssniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-php
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-php
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
*
* Ensures all calls to inbuilt PHP functions are lowercase.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_php_lowercasephpfunctionssniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_php_lowercasephpfunctionssniff implements php_codesniffer_sniff {
/**
*
* @return array
*/
- public function register()
- {
+ public function register() {
return array(
T_ISSET,
T_ECHO,
T_DECLARE,
T_STRING,
);
-
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
if ($tokens[$stackptr]['code'] !== T_STRING) {
$content = $tokens[$stackptr]['content'];
+
if ($content !== strtolower($content)) {
$type = strtoupper($content);
$expected = strtolower($content);
}
// Make sure this is a function call.
- $next = $phpcsfile->findNext(T_WHITESPACE, ($stackptr + 1), null, true);
+ $next = $phpcsfile->findnext(T_WHITESPACE, ($stackptr + 1), null, true);
+
if ($next === false) {
// Not a function call.
return;
return;
}
- $prev = $phpcsfile->findPrevious(T_WHITESPACE, ($stackptr - 1), null, true);
+ $prev = $phpcsfile->findprevious(T_WHITESPACE, ($stackptr - 1), null, true);
+
if ($tokens[$prev]['code'] === T_FUNCTION) {
// Function declaration, not a function call.
return;
// user defined global functions can exist, except for
// PHP_CodeSniffer ones.
$content = $tokens[$stackptr]['content'];
+
if (function_exists($content) === false) {
return;
}
if ($content !== strtolower($content)) {
$expected = strtolower($content);
- $error = "Calls to inbuilt PHP functions must be lowercase; expected \"$expected\" but found \"$content\"";
+ $error = "Calls to inbuilt PHP functions must be lowercase; expected \"$expected\" but found \"$content\"";
$phpcsfile->adderror($error, $stackptr);
}
-
}
-
-
}
-
-?>
/**
* moodle_sniffs_strings_doublequoteusagesniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-strings
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-strings
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
*
* Makes sure that any use of Double Quotes ("") are warranted.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_strings_doublequoteusagesniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_strings_doublequoteusagesniff implements php_codesniffer_sniff {
/**
*
* @return array
*/
- public function register()
- {
- return array(
- T_CONSTANT_ENCAPSED_STRING,
- );
-
+ public function register() {
+ return array(T_CONSTANT_ENCAPSED_STRING);
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
- $workingString = $tokens[$stackptr]['content'];
+ $workingstring = $tokens[$stackptr]['content'];
// Check if it's a double quoted string.
- if (strpos($workingString, '"') === false) {
+ if (strpos($workingstring, '"') === false) {
return;
}
// Make sure it's not a part of a string started above.
// If it is, then we have already checked it.
- if ($workingString[0] !== '"') {
+ if ($workingstring[0] !== '"') {
return;
}
// Work through the following tokens, in case this string is stretched
// over multiple Lines.
for ($i = ($stackptr + 1); $i < $phpcsfile->numTokens; $i++) {
+
if ($tokens[$i]['type'] !== 'T_CONSTANT_ENCAPSED_STRING') {
break;
}
-
- $workingString .= $tokens[$i]['content'];
+ $workingstring .= $tokens[$i]['content'];
}
- $allowedChars = array(
+ $allowedchars = array(
'\n',
'\r',
'\f',
'\v',
'\x',
'\'',
+ '$'
);
- foreach ($allowedChars as $testChar) {
- if (strpos($workingString, $testChar) !== false) {
+ foreach ($allowedchars as $testchar) {
+
+ if (strpos($workingstring, $testchar) !== false) {
return;
}
}
- $error = "String $workingString does not require double quotes; use single quotes instead";
+ $error = "String $workingstring does not require double quotes; use single quotes instead";
$phpcsfile->addwarning($error, $stackptr);
-
}
-
-
}
-
-?>
/**
* moodle_sniffs_strings_echoedstringssniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-strings
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-strings
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Makes sure that any strings that are "echoed" are not enclosed in brackets
* like a function call.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_strings_echoedstringssniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_strings_echoedstringssniff implements php_codesniffer_sniff {
/**
*
* @return array
*/
- public function register()
- {
+ public function register() {
return array(T_ECHO);
-
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
- $firstcontent = $phpcsfile->findNext(array(T_WHITESPACE), ($stackptr + 1), null, true);
+ $firstcontent = $phpcsfile->findnext(array(T_WHITESPACE), ($stackptr + 1), null, true);
// If the first non-whitespace token is not an opening parenthesis, then we are not concerned.
if ($tokens[$firstcontent]['code'] !== T_OPEN_PARENTHESIS) {
return;
}
- $endOfStatement = $phpcsfile->findNext(array(T_SEMICOLON), $stackptr, null, false);
+ $endofstatement = $phpcsfile->findnext(array(T_SEMICOLON), $stackptr, null, false);
// If the token before the semi-colon is not a closing parenthesis, then we are not concerned.
- if ($tokens[($endOfStatement - 1)]['code'] !== T_CLOSE_PARENTHESIS) {
+ if ($tokens[($endofstatement - 1)]['code'] !== T_CLOSE_PARENTHESIS) {
return;
}
- if (($phpcsfile->findNext(PHP_CodeSniffer_tokens::$operators, $stackptr, $endOfStatement, false)) === false) {
+ if (($phpcsfile->findnext(PHP_CodeSniffer_tokens::$operators, $stackptr, $endofstatement, false)) === false) {
// There are no arithmetic operators in this.
$error = 'Echoed strings should not be bracketed';
$phpcsfile->adderror($error, $stackptr);
}
-
}
-
-
}
-
-?>
/**
* moodle_sniffs_whitespace_controlstructureblanklinesniff
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-whitespace
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-whitespace
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
*
* Checks that there is a blank line before control structures
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class moodle_sniffs_whitespace_controlstructureblanklinesniff implements php_codesniffer_sniff {
// 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);
+ $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);
+ // Exception: do {EOL...} while (...);
+ if ($tokens[$stackptr]['code'] == T_WHILE && $tokens[($stackptr - 1)]['code'] == T_CLOSE_CURLY_BRACKET) {
+ // Ignore do...while (see above)
+ } else {
+ $phpcsfile->addWarning('You should add a blank line before control structures', $stackptr);
+ }
}
}
}
/**
* moodle_sniffs_whitespace_disallowtabindentsniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-whitespace
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-whitespace
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
*
* Throws errors if tabs are used for indentation.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_whitespace_disallowtabindentsniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_whitespace_disallowtabindentsniff implements php_codesniffer_sniff {
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
- public $supportedtokenizers = array(
- 'PHP',
- 'JS',
- );
+ public $supportedtokenizers = array('PHP', 'JS');
/**
*
* @return array
*/
- public function register()
- {
+ public function register() {
return array(T_WHITESPACE);
-
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
// Make sure this is whitespace used for indentation.
$line = $tokens[$stackptr]['line'];
+
if ($stackptr > 0 && $tokens[($stackptr - 1)]['line'] === $line) {
return;
}
$error = 'Spaces must be used to indent lines; tabs are not allowed';
$phpcsfile->adderror($error, $stackptr);
}
-
}
-
-
}
-
-?>
/**
* Verifies that class members are spaced correctly.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-whitespace
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-whitespace
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (class_exists('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false) {
/**
* Verifies that class members are spaced correctly.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_whitespace_membervarspacingsniff extends php_codesniffer_standards_abstractvariablesniff
-{
+class moodle_sniffs_whitespace_membervarspacingsniff extends php_codesniffer_standards_abstractvariablesniff {
/**
*
* @return void
*/
- protected function processMemberVar(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ protected function processmembervar(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
// There needs to be 1 blank line before the var, not counting comments.
$prevlinetoken = null;
+
for ($i = ($stackptr - 1); $i > 0; $i--) {
+
if (in_array($tokens[$i]['code'], PHP_CodeSniffer_tokens::$commentTokens) === true) {
// Skip comments.
continue;
+
} else if (strpos($tokens[$i]['content'], $phpcsfile->eolChar) === false) {
// Not the end of the line.
continue;
+
} else {
// If this is a WHITESPACE token, and the token right before
// it is a DOC_COMMENT, then it is just the newline after the
// member var's comment, and can be skipped.
- if ($tokens[$i]['code'] === T_WHITESPACE && in_array($tokens[($i - 1)]['code'], PHP_CodeSniffer_tokens::$commentTokens) === true) {
+ if ($tokens[$i]['code'] === T_WHITESPACE &&
+ in_array($tokens[($i - 1)]['code'], PHP_CodeSniffer_tokens::$commentTokens) === true) {
continue;
}
// Never found the previous line, which means
// there are 0 blank lines before the member var.
$foundlines = 0;
+
} else {
- $prevcontent = $phpcsfile->findPrevious(array(T_WHITESPACE, T_DOC_COMMENT), $prevlinetoken, null, true);
+ $prevcontent = $phpcsfile->findprevious(array(T_WHITESPACE, T_DOC_COMMENT), $prevlinetoken, null, true);
$foundlines = ($tokens[$prevlinetoken]['line'] - $tokens[$prevcontent]['line']);
}
if ($foundlines !== 1) {
// $phpcsfile->adderror("Expected 1 blank line before member var; $foundlines found", $stackptr);
}
-
}
*
* @return void
*/
- protected function processVariable(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ protected function processvariable(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
// We don't care about normal variables.
return;
-
}
*
* @return void
*/
- protected function processVariableInString(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ protected function processvariableinstring(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
// We don't care about normal variables.
return;
-
}
-
-
}
-
-?>
/**
* moodle_sniffs_whitespace_scopeclosingbracesniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-whitespace
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-whitespace
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
*
* Checks that the closing braces of scopes are aligned correctly.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_whitespace_scopeclosingbracesniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_whitespace_scopeclosingbracesniff implements php_codesniffer_sniff {
/**
*
* @return array
*/
- public function register()
- {
+ public function register() {
return PHP_CodeSniffer_tokens::$scopeOpeners;
-
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
// If this is an inline condition (ie. there is no scope opener), then
// 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) {
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'];
+ $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);
+ $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);
}
// Check now that the closing brace is lined up correctly.
- $braceIndent = $tokens[$scopeend]['column'];
- $isBreakCloser = ($tokens[$scopeend]['code'] === T_BREAK);
- if (in_array($tokens[$stackptr]['code'], array(T_CASE, T_DEFAULT)) === true && $isBreakCloser === true) {
+ $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);
+ if ($braceindent !== ($startcolumn + 4)) {
+ $error = 'Break statement indented incorrectly; expected '.($startcolumn + 3).' spaces, found '.
+ ($braceindent - 1);
$phpcsfile->adderror($error, $scopeend);
}
+
} else {
+
if (in_array($tokens[$stackptr]['code'], array(T_CASE, T_DEFAULT))) {
- $startColumn -= 4;
+ $startcolumn -= 4;
}
- if ($braceIndent !== $startColumn) {
- $error = 'Closing brace indented incorrectly; expected '.($startColumn - 1).' spaces, found '.($braceIndent - 1);
+ if ($braceindent !== $startcolumn) {
+ $error = 'Closing brace indented incorrectly; expected '.($startcolumn - 1).' spaces, found '.
+ ($braceindent - 1);
$phpcsfile->adderror($error, $scopeend);
}
}
-
}
-
-
}
-
-?>
/**
* moodle_sniffs_whitespace_scopeindentsniff.
*
- * @package lib-pear-php-codesniffer-standards-moodle-sniffs-whitespace
- * @copyright 2008 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ * @subpackage lib-pear-php-codesniffer-standards-moodle-sniffs-whitespace
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* is indented correctly. This sniff will throw errors if tabs are used
* for indentation rather than spaces.
*
- * @copyright 2008 Nicolas Connault
+ * @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class moodle_sniffs_whitespace_scopeindentsniff implements php_codesniffer_sniff
-{
+class moodle_sniffs_whitespace_scopeindentsniff implements php_codesniffer_sniff {
/**
* The number of spaces code should be indented.
*
* @var array(int)
*/
- protected $nonIndentingScopes = array();
+ protected $nonindentingscopes = array();
/**
*
* @return array
*/
- public function register()
- {
+ public function register() {
return PHP_CodeSniffer_tokens::$scopeOpeners;
-
}
*
* @return void
*/
- public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
- {
+ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
// If this is an inline condition (ie. there is no scope opener), then
}
if ($tokens[$stackptr]['code'] === T_ELSE) {
- $next = $phpcsfile->findNext(PHP_CodeSniffer_tokens::$emptyTokens, ($stackptr + 1), null, true);
+ $next = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, ($stackptr + 1), null, true);
// We will handle the T_IF token in another call to process.
+
if ($tokens[$next]['code'] === T_IF) {
return;
}
// Find the first token on this line.
$firsttoken = $stackptr;
+
for ($i = $stackptr; $i >= 0; $i--) {
// Record the first code token on the line.
if (in_array($tokens[$i]['code'], PHP_CodeSniffer_tokens::$emptyTokens) === false) {
// Based on the conditions that surround this token, determine the
// indent that we expect this current content to be.
- $expectedIndent = $this->calculateExpectedIndent($tokens, $firsttoken);
+ $expectedindent = $this->calculateexpectedindent($tokens, $firsttoken);
- if ($tokens[$firsttoken]['column'] !== $expectedIndent) {
+ if ($tokens[$firsttoken]['column'] !== $expectedindent) {
$error = 'line indented incorrectly; expected ';
- $error .= ($expectedIndent - 1).' spaces, found ';
+ $error .= ($expectedindent - 1).' spaces, found ';
$error .= ($tokens[$firsttoken]['column'] - 1);
$phpcsfile->adderror($error, $stackptr);
}
- $scopeOpener = $tokens[$stackptr]['scope_opener'];
- $scopeCloser = $tokens[$stackptr]['scope_closer'];
+ $scopeopener = $tokens[$stackptr]['scope_opener'];
+ $scopecloser = $tokens[$stackptr]['scope_closer'];
// Some scopes are expected not to have indents.
- if (in_array($tokens[$firsttoken]['code'], $this->nonIndentingScopes) === false) {
- $indent = ($expectedIndent + $this->indent);
+ if (in_array($tokens[$firsttoken]['code'], $this->nonindentingscopes) === false) {
+ $indent = ($expectedindent + $this->indent);
+
} else {
- $indent = $expectedIndent;
+ $indent = $expectedindent;
}
$newline = false;
- $commentOpen = false;
- $inHereDoc = false;
+ $commentopen = false;
+ $inheredoc = false;
// Only loop over the content beween the opening and closing brace, not
// the braces themselves.
- for ($i = ($scopeOpener + 1); $i < $scopeCloser; $i++) {
+ for ($i = ($scopeopener + 1); $i < $scopecloser; $i++) {
// If this token is another scope, skip it as it will be handled by
// another call to this sniff.
if (in_array($tokens[$i]['code'], PHP_CodeSniffer_tokens::$scopeOpeners) === true) {
+
if (isset($tokens[$i]['scope_opener']) === true) {
$i = $tokens[$i]['scope_closer'];
+
} else {
// If this token does not have a scope_opener indice, then
// it's probably an inline scope, so let's skip to the next
// semicolon. Inline scopes include inline if's, abstract methods etc.
- $nexttoken = $phpcsfile->findNext(T_SEMICOLON, $i, $scopeCloser);
+ $nexttoken = $phpcsfile->findnext(T_SEMICOLON, $i, $scopecloser);
+
if ($nexttoken !== false) {
$i = $nexttoken;
}
// If this is a HEREDOC then we need to ignore it as the whitespace
// before the contents within the HEREDOC are considered part of the content.
if ($tokens[$i]['code'] === T_START_HEREDOC) {
- $inHereDoc = true;
+ $inheredoc = true;
continue;
- } else if ($inHereDoc === true) {
+
+ } else if ($inheredoc === true) {
+
if ($tokens[$i]['code'] === T_END_HEREDOC) {
- $inHereDoc = false;
+ $inheredoc = false;
}
continue;
// Special case for non-PHP code.
if ($tokens[$firsttoken]['code'] === T_INLINE_HTML) {
- $trimmedcontentLength = strlen(ltrim($tokens[$firsttoken]['content']));
- if ($trimmedcontentLength === 0) {
+ $trimmedcontentlength = strlen(ltrim($tokens[$firsttoken]['content']));
+
+ if ($trimmedcontentlength === 0) {
continue;
}
- $contentLength = strlen($tokens[$firsttoken]['content']);
- $column = ($contentLength - $trimmedcontentLength + 1);
+ $contentlength = strlen($tokens[$firsttoken]['content']);
+ $column = ($contentlength - $trimmedcontentlength + 1);
}
// Check to see if this constant string spans multiple lines.
// If so, then make sure that the strings on lines other than the
// first line are indented appropriately, based on their whitespace.
if (in_array($tokens[$firsttoken]['code'], PHP_CodeSniffer_tokens::$stringTokens) === true) {
+
if (in_array($tokens[($firsttoken - 1)]['code'], PHP_CodeSniffer_tokens::$stringTokens) === true) {
// If we find a string that directly follows another string
// then its just a string that spans multiple lines, so we
// This is a special condition for T_DOC_COMMENT and C-style
// comments, which contain whitespace between each line.
if (in_array($tokens[$firsttoken]['code'], array(T_COMMENT, T_DOC_COMMENT)) === true) {
-
$content = trim($tokens[$firsttoken]['content']);
+
if (preg_match('|^/\*|', $content) !== 0) {
// Check to see if the end of the comment is on the same line
// as the start of the comment. If it is, then we don't
if (preg_match('|\*/$|', $content) === 0) {
// We don't have to calculate the column for the start
// of the comment as there is a whitespace token before it.
- $commentOpen = true;
+ $commentopen = true;
}
- } else if ($commentOpen === true) {
+
+ } else if ($commentopen === true) {
+
if ($content === '') {
// We are in a comment, but this line has nothing on it
// so let's skip it.
continue;
}
- $contentLength = strlen($tokens[$firsttoken]['content']);
- $trimmedcontentLength = strlen(ltrim($tokens[$firsttoken]['content']));
- $column = ($contentLength - $trimmedcontentLength + 1);
+ $contentlength = strlen($tokens[$firsttoken]['content']);
+ $trimmedcontentlength = strlen(ltrim($tokens[$firsttoken]['content']));
+ $column = ($contentlength - $trimmedcontentlength + 1);
+
if (preg_match('|\*/$|', $content) !== 0) {
- $commentOpen = false;
+ $commentopen = false;
}
}
}
// greater than the relative indent we set above. If it is less,
// an error should be shown.
if ($column !== $indent) {
+
if ($this->exact === true || $column < $indent) {
$error = 'line indented incorrectly; expected ';
+
if ($this->exact === false) {
$error .= 'at least ';
}
}
}
}
-
}
*
* @return int
*/
- protected function calculateExpectedIndent(array $tokens, $stackptr)
- {
- $conditionStack = array();
+ protected function calculateexpectedindent(array $tokens, $stackptr) {
+ $conditionstack = array();
// Empty conditions array (top level structure).
if (empty($tokens[$stackptr]['conditions']) === true) {
return 1;
}
- $tokenConditions = $tokens[$stackptr]['conditions'];
- foreach ($tokenConditions as $id => $condition) {
+ $tokenconditions = $tokens[$stackptr]['conditions'];
+
+ foreach ($tokenconditions as $id => $condition) {
// If it's an indenting scope ie. it's not in our array of
// scopes that don't indent, add it to our condition stack.
- if (in_array($condition, $this->nonIndentingScopes) === false) {
- $conditionStack[$id] = $condition;
+ if (in_array($condition, $this->nonindentingscopes) === false) {
+ $conditionstack[$id] = $condition;
}
}
- return ((count($conditionStack) * $this->indent) + 1);
-
+ return ((count($conditionstack) * $this->indent) + 1);
}
-
-
}
-
-?>