From 68a1cf5d93a968ee3eae67cd708017f3ccbe356a Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Thu, 21 May 2009 09:48:31 +0000 Subject: [PATCH] MDL-19247 Cleaned up most of the sniffer files and adjusted some rules such as do { ... } while (); --- .../Sniffs/Classes/ClassDeclarationSniff.php | 9 +- .../CodeAnalysis/EmptyStatementSniff.php | 9 +- .../ForLoopShouldBeWhileLoopSniff.php | 9 +- .../ForLoopWithTestFunctionCallSniff.php | 11 +- .../CodeAnalysis/JumbledIncrementerSniff.php | 9 +- .../UnconditionalIfStatementSniff.php | 9 +- .../UnnecessaryFinalModifierSniff.php | 11 +- .../UnusedFunctionParameterSniff.php | 17 +- .../UselessOverridingMethodSniff.php | 26 +- .../Sniffs/Commenting/ClassCommentSniff.php | 28 +- .../Sniffs/Commenting/FileCommentSniff.php | 55 ++-- .../Commenting/FunctionCommentSniff.php | 252 +++++++++--------- .../Sniffs/Commenting/InlineCommentSniff.php | 24 +- .../ControlSignatureSniff.php | 43 ++- .../ElseIfDeclarationSniff.php | 25 +- .../InlineControlStructureSniff.php | 50 ++-- .../Sniffs/Files/ClosingPHPTagSniff.php | 10 +- .../Sniffs/Files/IncludingFileSniff.php | 67 ++--- .../Moodle/Sniffs/Files/LineEndingsSniff.php | 25 +- .../Moodle/Sniffs/Files/LineLengthSniff.php | 54 ++-- .../Sniffs/Formatting/SpaceAfterCastSniff.php | 24 +- .../FunctionCallArgumentSpacingSniff.php | 66 ++--- .../Functions/FunctionCallSignatureSniff.php | 33 +-- .../Functions/FunctionDeclarationSniff.php | 20 +- .../LowercaseFunctionKeywordsSniff.php | 33 +-- .../Functions/ValidDefaultValueSniff.php | 47 ++-- .../UpperCaseConstantNameSniff.php | 80 +++--- .../NamingConventions/ValidClassNameSniff.php | 32 +-- .../ValidFunctionNameSniff.php | 66 +++-- .../ValidVariableNameSniff.php | 29 +- .../Sniffs/PHP/DisallowShortOpenTagSniff.php | 34 +-- .../Sniffs/PHP/LowerCaseConstantSniff.php | 38 +-- .../Sniffs/PHP/LowercasePHPFunctionsSniff.php | 34 ++- .../Sniffs/Strings/DoubleQuoteUsageSniff.php | 48 ++-- .../Sniffs/Strings/EchoedStringsSniff.php | 32 +-- .../ControlStructureBlankLineSniff.php | 20 +- .../WhiteSpace/DisallowTabIndentSniff.php | 30 +-- .../WhiteSpace/MemberVarSpacingSniff.php | 38 ++- .../WhiteSpace/ScopeClosingBraceSniff.php | 54 ++-- .../Sniffs/WhiteSpace/ScopeIndentSniff.php | 112 ++++---- 40 files changed, 747 insertions(+), 866 deletions(-) diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Classes/ClassDeclarationSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Classes/ClassDeclarationSniff.php index c0c8d1999bafb..45d13853aaa51 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Classes/ClassDeclarationSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Classes/ClassDeclarationSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -64,7 +65,7 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) { } $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']; diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/EmptyStatementSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/EmptyStatementSniff.php index 42b43ddd2b17d..fc908609ecbbc 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/EmptyStatementSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/EmptyStatementSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -42,7 +43,7 @@ * elsif, for, foreach, if, switch, try * and while. * - * @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 diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/ForLoopShouldBeWhileLoopSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/ForLoopShouldBeWhileLoopSniff.php index 89966fbd1a3a5..110e4553b30ea 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/ForLoopShouldBeWhileLoopSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/ForLoopShouldBeWhileLoopSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -38,7 +39,7 @@ * } * * - * @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 diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/ForLoopWithTestFunctionCallSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/ForLoopWithTestFunctionCallSniff.php index 5a77f699395ed..4b755e8001945 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/ForLoopWithTestFunctionCallSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/ForLoopWithTestFunctionCallSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -41,7 +42,7 @@ * } * * - * @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 @@ -100,7 +101,7 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) // 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'; diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/JumbledIncrementerSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/JumbledIncrementerSniff.php index 0a17fe2a10bb5..f272168ca54f4 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/JumbledIncrementerSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/JumbledIncrementerSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -45,7 +46,7 @@ * } * * - * @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 diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/UnconditionalIfStatementSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/UnconditionalIfStatementSniff.php index bd3f019dfea5c..d54411a55d9ff 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/UnconditionalIfStatementSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/UnconditionalIfStatementSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -42,7 +43,7 @@ * } * * - * @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 diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/UnnecessaryFinalModifierSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/UnnecessaryFinalModifierSniff.php index b10400349c6ae..0ee64447a2430 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/UnnecessaryFinalModifierSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/UnnecessaryFinalModifierSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -38,7 +39,7 @@ * } * * - * @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 @@ -77,7 +78,7 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) } // 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) { diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php index c282731b32942..c9e00c5307a28 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -31,7 +32,7 @@ * 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 @@ -70,7 +71,7 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) } $params = array(); - foreach ($phpcsfile->getmethodParameters($stackptr) as $param) { + foreach ($phpcsfile->getmethodparameters($stackptr) as $param) { $params[$param['name']] = $stackptr; } @@ -91,7 +92,7 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $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; } @@ -101,7 +102,7 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) 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 . if ($tmp !== false && $tokens[$tmp] === T_SEMICOLON) { @@ -140,5 +141,3 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php index bec8aa1a20f00..7730c934587dc 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -38,11 +39,10 @@ * } * * - * @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 { /** @@ -81,7 +81,7 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) // Get all parameters from method signature. $signature = array(); - foreach ($phpcsfile->getmethodParameters($stackptr) as $param) { + foreach ($phpcsfile->getmethodparameters($stackptr) as $param) { $signature[] = $param['name']; } @@ -106,7 +106,7 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) } // 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) { @@ -114,7 +114,7 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) } // 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) { @@ -122,14 +122,14 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) } // 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, @@ -156,7 +156,7 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) } } - $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; } @@ -181,5 +181,3 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Commenting/ClassCommentSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Commenting/ClassCommentSniff.php index 4025c75db191b..8c32bc141643b 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Commenting/ClassCommentSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Commenting/ClassCommentSniff.php @@ -17,9 +17,10 @@ /** * 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) { @@ -46,7 +47,7 @@ *
  • Check required and optional tags and the format of their content.
  • * * - * @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 { @@ -87,7 +88,7 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) { ); // 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); @@ -98,25 +99,25 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $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. @@ -135,6 +136,7 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) { 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); @@ -166,7 +168,7 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) { $long = $comment->getlongcomment(); if (empty($long) === false) { - $between = $comment->getWhiteSpaceBetween(); + $between = $comment->getWhiteSpacebetween(); $newlinebetween = substr_count($between, $phpcsfile->eolChar); if ($newlinebetween !== 2) { diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Commenting/FileCommentSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Commenting/FileCommentSniff.php index 9e3cbb9134e96..2dea546b546f8 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Commenting/FileCommentSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Commenting/FileCommentSniff.php @@ -17,9 +17,10 @@ /** * 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) { @@ -41,7 +42,7 @@ *
  • Check required and optional tags and the format of their content.
  • * * - * @copyright 2008 Nicolas Connault + * @copyright 2009 Nicolas Connault * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -70,7 +71,7 @@ class moodle_sniffs_commenting_filecommentsniff implements php_codesniffer_sniff 'category' => array( 'required' => false, 'allow_multiple' => false, - 'order_text' => 'precedes @package', + 'order_text' => 'precedes @subpackage', ), 'package' => array( 'required' => true, @@ -80,12 +81,12 @@ class moodle_sniffs_commenting_filecommentsniff implements php_codesniffer_sniff '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, @@ -124,6 +125,10 @@ class moodle_sniffs_commenting_filecommentsniff implements php_codesniffer_sniff ), ); + 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. @@ -150,7 +155,7 @@ 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; } } @@ -504,13 +509,13 @@ protected function getindentation($tagname, $tagelement) { 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(), ' ')); } } @@ -567,20 +572,28 @@ protected function processcategory($errorpos) { 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(); @@ -597,7 +610,7 @@ protected function processpackage($errorpos) { } } else { - $error = '@package tag must contain a name'; + $error = '@subpackage tag must contain a name'; $this->currentfile->adderror($error, $errorpos); } } @@ -632,7 +645,7 @@ protected function processsubpackage($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 { diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Commenting/FunctionCommentSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Commenting/FunctionCommentSniff.php index 6360a9ce93033..f48c17392756d 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Commenting/FunctionCommentSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Commenting/FunctionCommentSniff.php @@ -17,9 +17,10 @@ /** * 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) { @@ -35,20 +36,19 @@ *
  • There is a blank newline after the short description.
  • *
  • There is a blank newline between the long and short description.
  • *
  • There is a blank newline between the long description and tags.
  • - *
  • Parameter names represent those in the method.
  • - *
  • Parameter comments are in the correct order
  • - *
  • Parameter comments are complete
  • + *
  • parameter names represent those in the method.
  • + *
  • parameter comments are in the correct order
  • + *
  • parameter comments are complete
  • *
  • A space is present before the first and after the last parameter
  • *
  • A return type exists
  • *
  • There must be one blank line between body and headline comments.
  • *
  • Any throw tag must have an exception class.
  • * * - * @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. @@ -76,14 +76,14 @@ class moodle_sniffs_commenting_functioncommentsniff implements php_codesniffer_s * * @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; /** @@ -91,8 +91,7 @@ class moodle_sniffs_commenting_functioncommentsniff implements php_codesniffer_s * * @return array */ - public function register() - { + public function register() { return array(T_FUNCTION); } @@ -107,8 +106,7 @@ public function register() * * @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, @@ -117,13 +115,13 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) 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 @@ -134,6 +132,7 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) $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; @@ -146,7 +145,8 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) $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; @@ -154,20 +154,23 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) $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; } @@ -177,31 +180,34 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) $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)); } @@ -210,25 +216,30 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) // 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)); @@ -247,23 +258,22 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) * * @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); } } - } @@ -275,12 +285,12 @@ protected function processThrows($commentstart) * * @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, '_')); } @@ -289,19 +299,18 @@ protected function processReturn($commentstart, $commentend) 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. * @@ -310,155 +319,156 @@ protected function processReturn($commentstart, $commentend) * * @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); } - } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Commenting/InlineCommentSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Commenting/InlineCommentSniff.php index f2b8abfc6b42b..b9d70abfff996 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Commenting/InlineCommentSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Commenting/InlineCommentSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -27,11 +28,10 @@ * * 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 { /** @@ -39,10 +39,8 @@ class moodle_sniffs_commenting_inlinecommentsniff implements php_codesniffer_sni * * @return array */ - public function register() - { + public function register() { return array(T_COMMENT); - } @@ -55,8 +53,7 @@ public function register() * * @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} === '#') { @@ -64,10 +61,5 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) $error .= ' or "/* comment */" instead.'; $phpcsfile->adderror($error, $stackptr); } - } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/ControlStructures/ControlSignatureSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/ControlStructures/ControlSignatureSniff.php index 66376674490c2..8ac027d536d47 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/ControlStructures/ControlSignatureSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/ControlStructures/ControlSignatureSniff.php @@ -17,9 +17,10 @@ /** * 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) { @@ -29,20 +30,17 @@ /** * 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); - } @@ -51,22 +49,15 @@ public function __construct() * * @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...}'); } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/ControlStructures/ElseIfDeclarationSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/ControlStructures/ElseIfDeclarationSniff.php index d95da4f3d2aae..0e85987d09bee 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/ControlStructures/ElseIfDeclarationSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/ControlStructures/ElseIfDeclarationSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -28,11 +29,10 @@ * 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 { /** @@ -40,10 +40,8 @@ class moodle_sniffs_controlstructures_elseifdeclarationsniff implements php_code * * @return array */ - public function register() - { + public function register() { return array(T_ELSEIF); - } @@ -56,15 +54,8 @@ public function register() * * @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); - } - - } - - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/ControlStructures/InlineControlStructureSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/ControlStructures/InlineControlStructureSniff.php index 20d8030b3e642..554dd695d42e8 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/ControlStructures/InlineControlStructureSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/ControlStructures/InlineControlStructureSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -27,21 +28,17 @@ * * 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. @@ -55,18 +52,14 @@ class moodle_sniffs_controlstructures_inlinecontrolstructuresniff implements php * * @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); } @@ -79,8 +72,7 @@ public function register() * * @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) { @@ -91,11 +83,14 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) 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; } @@ -113,10 +108,5 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) return; } - } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Files/ClosingPHPTagSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Files/ClosingPHPTagSniff.php index d2cfeb66c1504..42089a8a9c2e4 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Files/ClosingPHPTagSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Files/ClosingPHPTagSniff.php @@ -17,15 +17,16 @@ /** * 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 { @@ -53,6 +54,7 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) { $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); diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Files/IncludingFileSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Files/IncludingFileSniff.php index 08998e891116f..0906259564fa1 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Files/IncludingFileSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Files/IncludingFileSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -29,23 +30,17 @@ * 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); /** @@ -53,15 +48,8 @@ class moodle_sniffs_files_includingfilesniff implements php_codesniffer_sniff * * @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); } @@ -74,11 +62,10 @@ public function register() * * @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; '; @@ -86,55 +73,57 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) // $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); } } - } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Files/LineEndingsSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Files/LineEndingsSniff.php index ec182c08a7f48..e8ce7ca79a7a4 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Files/LineEndingsSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Files/LineEndingsSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -27,11 +28,10 @@ * * 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. @@ -46,8 +46,7 @@ class moodle_sniffs_files_lineendingssniff implements php_codesniffer_sniff * * @return array */ - public function register() - { + public function register() { return array(T_OPEN_TAG); } @@ -62,11 +61,11 @@ public function register() * * @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; } } @@ -83,8 +82,4 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) } } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Files/LineLengthSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Files/LineLengthSniff.php index 7e3e94c569cea..3a79106fd4941 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Files/LineLengthSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Files/LineLengthSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -29,18 +30,17 @@ * 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. @@ -49,7 +49,7 @@ class moodle_sniffs_files_linelengthsniff implements php_codesniffer_sniff * * @var int */ - protected $absolutelineLimit = 200; + protected $absolutelinelimit = 200; /** @@ -57,10 +57,8 @@ class moodle_sniffs_files_linelengthsniff implements php_codesniffer_sniff * * @return array */ - public function register() - { + public function register() { return array(T_OPEN_TAG); - } @@ -73,13 +71,13 @@ public function register() * * @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; } @@ -88,17 +86,19 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) $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); } @@ -112,25 +112,21 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) * * @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); } } - } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Formatting/SpaceAfterCastSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Formatting/SpaceAfterCastSniff.php index 4927f712d4b57..e510e3bd43bae 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Formatting/SpaceAfterCastSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Formatting/SpaceAfterCastSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -27,11 +28,10 @@ * * 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 { /** @@ -39,10 +39,8 @@ class moodle_sniffs_formatting_spaceaftercastsniff implements php_codesniffer_sn * * @return array */ - public function register() - { + public function register() { return PHP_CodeSniffer_tokens::$castTokens; - } @@ -55,8 +53,7 @@ public function register() * * @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) { @@ -69,10 +66,5 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) $error = 'A cast statement must be followed by a single space'; $phpcsfile->adderror($error, $stackptr); } - } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php index 122139212d87e..59f48013bc382 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -27,11 +28,10 @@ * * 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 { /** @@ -39,10 +39,8 @@ class moodle_sniffs_functions_functioncallargumentspacingsniff implements php_co * * @return array */ - public function register() - { + public function register() { return array(T_STRING); - } @@ -55,8 +53,7 @@ public function register() * * @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 @@ -65,44 +62,50 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) // "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'; @@ -110,11 +113,15 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) } } } + } 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); @@ -128,10 +135,5 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) } } } - } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/FunctionCallSignatureSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/FunctionCallSignatureSniff.php index 2da9a624ff955..53514a6c20d03 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/FunctionCallSignatureSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/FunctionCallSignatureSniff.php @@ -17,19 +17,19 @@ /** * 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 { /** @@ -37,10 +37,8 @@ class moodle_sniffs_functions_functioncallsignaturesniff implements php_codesnif * * @return array */ - public function register() - { + public function register() { return array(T_STRING); - } @@ -53,12 +51,11 @@ public function register() * * @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. @@ -71,7 +68,8 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) } // 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; @@ -98,7 +96,7 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) 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(). @@ -111,17 +109,14 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) } } - $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); } } - } - - } -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/FunctionDeclarationSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/FunctionDeclarationSniff.php index 1dcf2d1e9c4d9..87633d35294cc 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/FunctionDeclarationSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/FunctionDeclarationSniff.php @@ -17,9 +17,10 @@ /** * 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) { @@ -31,11 +32,10 @@ * * 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 { /** @@ -43,16 +43,10 @@ class moodle_sniffs_functions_functiondeclarationsniff extends php_codesniffer_s * * @return array */ - protected function getPatterns() - { + protected function getpatterns() { return array( 'function abc(...) {', 'abstract function abc(...);' ); - } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/LowercaseFunctionKeywordsSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/LowercaseFunctionKeywordsSniff.php index 776a3ffd86ac2..f88d545eb9840 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/LowercaseFunctionKeywordsSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/LowercaseFunctionKeywordsSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -27,11 +28,10 @@ * * 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 { /** @@ -39,16 +39,8 @@ class moodle_sniffs_functions_lowercasefunctionkeywordssniff implements php_code * * @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); } @@ -61,21 +53,16 @@ public function register() * * @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); } - } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/ValidDefaultValueSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/ValidDefaultValueSniff.php index 835258a788e54..df5ff4dcc0f2a 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/ValidDefaultValueSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Functions/ValidDefaultValueSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -28,11 +29,10 @@ * 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 { /** @@ -40,10 +40,8 @@ class moodle_sniffs_functions_validdefaultvaluesniff implements php_codesniffer_ * * @return array */ - public function register() - { + public function register() { return array(T_FUNCTION); - } @@ -56,8 +54,7 @@ public function register() * * @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']; @@ -65,20 +62,22 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) // 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; } } @@ -89,15 +88,15 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) * 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; } @@ -108,5 +107,3 @@ private static function _argHasDefault(PHP_CodeSniffer_File $phpcsfile, $argPtr) } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php index 3c4ddb92ea919..92e487d9d4fd8 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -27,11 +28,10 @@ * * 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 { /** @@ -39,10 +39,8 @@ class moodle_sniffs_namingconventions_uppercaseconstantnamesniff implements php_ * * @return array */ - public function register() - { + public function register() { return array(T_STRING); - } @@ -55,8 +53,7 @@ public function register() * * @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']; @@ -67,28 +64,24 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) // 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); } @@ -96,27 +89,31 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $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; } @@ -133,21 +130,18 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) */ // 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); } } - } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/NamingConventions/ValidClassNameSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/NamingConventions/ValidClassNameSniff.php index 9f337aff374a8..b468f2ce9d010 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/NamingConventions/ValidClassNameSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/NamingConventions/ValidClassNameSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -28,11 +29,10 @@ * 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 { /** @@ -40,13 +40,8 @@ class moodle_sniffs_namingconventions_validclassnamesniff implements php_codesni * * @return array */ - public function register() - { - return array( - T_CLASS, - T_INTERFACE, - ); - + public function register() { + return array(T_CLASS, T_INTERFACE); } @@ -59,11 +54,10 @@ public function register() * * @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 @@ -72,11 +66,5 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) $error = ucfirst($tokens[$stackptr]['content']).' name is not valid, must be all lower-case'; $phpcsfile->adderror($error, $stackptr); } - } - - } - - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/NamingConventions/ValidFunctionNameSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/NamingConventions/ValidFunctionNameSniff.php index d0bfb52943c63..c37744160d4de 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/NamingConventions/ValidFunctionNameSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/NamingConventions/ValidFunctionNameSniff.php @@ -17,9 +17,10 @@ /** * 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) { @@ -32,11 +33,10 @@ * 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. @@ -64,18 +64,14 @@ class moodle_sniffs_namingconventions_validfunctionnamesniff extends php_codesni * * @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); - } @@ -85,20 +81,21 @@ public function __construct() * @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); } @@ -115,14 +112,15 @@ protected function processtokenwithinScope(PHP_CodeSniffer_File $phpcsfile, $sta 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"; @@ -134,7 +132,8 @@ protected function processtokenwithinScope(PHP_CodeSniffer_File $phpcsfile, $sta // 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"; @@ -156,15 +155,16 @@ protected function processtokenwithinScope(PHP_CodeSniffer_File $phpcsfile, $sta * * @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); } @@ -191,5 +191,3 @@ protected function processtokenOutsideScope(PHP_CodeSniffer_File $phpcsfile, $st } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/NamingConventions/ValidVariableNameSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/NamingConventions/ValidVariableNameSniff.php index 314e23684ed39..af1868b20c026 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/NamingConventions/ValidVariableNameSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/NamingConventions/ValidVariableNameSniff.php @@ -17,9 +17,10 @@ /** * 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) { @@ -32,11 +33,10 @@ * * 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', @@ -51,10 +51,10 @@ class moodle_sniffs_namingconventions_validvariablenamesniff extends php_codesni * * @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); @@ -62,13 +62,14 @@ protected function processMemberVar(PHP_CodeSniffer_File $phpcsfile, $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; } - } @@ -80,18 +81,18 @@ protected function processMemberVar(PHP_CodeSniffer_File $phpcsfile, $stackptr) * * @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; } } - } diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/PHP/DisallowShortOpenTagSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/PHP/DisallowShortOpenTagSniff.php index 8e02f317eb659..e88658352b899 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/PHP/DisallowShortOpenTagSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/PHP/DisallowShortOpenTagSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -27,11 +28,10 @@ * * 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 { /** @@ -39,13 +39,8 @@ class moodle_sniffs_php_disallowshortopentagsniff implements php_codesniffer_sni * * @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); } @@ -58,8 +53,7 @@ public function register() * * @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. @@ -79,15 +73,11 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) } 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 "adderror($error, $stackptr); } - } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/PHP/LowerCaseConstantSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/PHP/LowerCaseConstantSniff.php index 26ac008cfb914..5dcd21ebedf3b 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/PHP/LowerCaseConstantSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/PHP/LowerCaseConstantSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -27,34 +28,25 @@ * * 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); } @@ -68,19 +60,15 @@ public function register() * * @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); } - } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/PHP/LowercasePHPFunctionsSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/PHP/LowercasePHPFunctionsSniff.php index 57683c8537cfb..e52e3f8b6a1b6 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/PHP/LowercasePHPFunctionsSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/PHP/LowercasePHPFunctionsSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -27,11 +28,10 @@ * * 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 { /** @@ -39,8 +39,7 @@ class moodle_sniffs_php_lowercasephpfunctionssniff implements php_codesniffer_sn * * @return array */ - public function register() - { + public function register() { return array( T_ISSET, T_ECHO, @@ -61,7 +60,6 @@ public function register() T_DECLARE, T_STRING, ); - } @@ -74,12 +72,12 @@ public function register() * * @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); @@ -91,7 +89,8 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) } // 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; @@ -102,7 +101,8 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) 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; @@ -123,19 +123,15 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) // 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); } - } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Strings/DoubleQuoteUsageSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Strings/DoubleQuoteUsageSniff.php index bf20d210e976a..53c2a096383fe 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Strings/DoubleQuoteUsageSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Strings/DoubleQuoteUsageSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -27,11 +28,10 @@ * * 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 { /** @@ -39,12 +39,8 @@ class moodle_sniffs_strings_doublequoteusagesniff implements php_codesniffer_sni * * @return array */ - public function register() - { - return array( - T_CONSTANT_ENCAPSED_STRING, - ); - + public function register() { + return array(T_CONSTANT_ENCAPSED_STRING); } @@ -57,34 +53,33 @@ public function register() * * @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', @@ -92,20 +87,17 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) '\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); - } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Strings/EchoedStringsSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Strings/EchoedStringsSniff.php index d56794b1c13dc..99e8e86569813 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Strings/EchoedStringsSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/Strings/EchoedStringsSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -28,11 +29,10 @@ * 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 { /** @@ -40,10 +40,8 @@ class moodle_sniffs_strings_echoedstringssniff implements php_codesniffer_sniff * * @return array */ - public function register() - { + public function register() { return array(T_ECHO); - } @@ -56,32 +54,26 @@ public function register() * * @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); } - } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/ControlStructureBlankLineSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/ControlStructureBlankLineSniff.php index 4171f8bcf8f57..8456ff2484c0d 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/ControlStructureBlankLineSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/ControlStructureBlankLineSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -27,7 +28,7 @@ * * 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 { @@ -58,14 +59,21 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) { // 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); + } } } } diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/DisallowTabIndentSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/DisallowTabIndentSniff.php index 9c5fc2cefd65a..8a142c137ca76 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/DisallowTabIndentSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/DisallowTabIndentSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -27,21 +28,17 @@ * * 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'); /** @@ -49,10 +46,8 @@ class moodle_sniffs_whitespace_disallowtabindentsniff implements php_codesniffer * * @return array */ - public function register() - { + public function register() { return array(T_WHITESPACE); - } @@ -65,12 +60,12 @@ public function register() * * @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; } @@ -79,10 +74,5 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) $error = 'Spaces must be used to indent lines; tabs are not allowed'; $phpcsfile->adderror($error, $stackptr); } - } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/MemberVarSpacingSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/MemberVarSpacingSniff.php index 1b6ec79a1a8bb..429ea305341d8 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/MemberVarSpacingSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/MemberVarSpacingSniff.php @@ -17,9 +17,10 @@ /** * 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) { @@ -29,11 +30,10 @@ /** * 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 { /** @@ -44,24 +44,28 @@ class moodle_sniffs_whitespace_membervarspacingsniff extends php_codesniffer_sta * * @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; } @@ -74,15 +78,15 @@ protected function processMemberVar(PHP_CodeSniffer_File $phpcsfile, $stackptr) // 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); } - } @@ -94,11 +98,9 @@ protected function processMemberVar(PHP_CodeSniffer_File $phpcsfile, $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; - } @@ -110,14 +112,8 @@ protected function processVariable(PHP_CodeSniffer_File $phpcsfile, $stackptr) * * @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; - } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php index 08f79f3a94c75..f21e0547fcc9b 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -27,11 +28,10 @@ * * 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 { /** @@ -39,10 +39,8 @@ class moodle_sniffs_whitespace_scopeclosingbracesniff implements php_codesniffer * * @return array */ - public function register() - { + public function register() { return PHP_CodeSniffer_tokens::$scopeOpeners; - } @@ -55,8 +53,7 @@ public function register() * * @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 @@ -70,7 +67,9 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) // 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; } @@ -78,14 +77,15 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) // 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); @@ -93,29 +93,29 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) } // 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); } } - } - - } - -?> diff --git a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/ScopeIndentSniff.php b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/ScopeIndentSniff.php index 32ecb681e79cc..ce3f0a617bf41 100644 --- a/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/ScopeIndentSniff.php +++ b/lib/pear/PHP/CodeSniffer/Standards/Moodle/Sniffs/WhiteSpace/ScopeIndentSniff.php @@ -17,9 +17,10 @@ /** * 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 */ /** @@ -29,11 +30,10 @@ * 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. @@ -57,7 +57,7 @@ class moodle_sniffs_whitespace_scopeindentsniff implements php_codesniffer_sniff * * @var array(int) */ - protected $nonIndentingScopes = array(); + protected $nonindentingscopes = array(); /** @@ -65,10 +65,8 @@ class moodle_sniffs_whitespace_scopeindentsniff implements php_codesniffer_sniff * * @return array */ - public function register() - { + public function register() { return PHP_CodeSniffer_tokens::$scopeOpeners; - } @@ -81,8 +79,7 @@ public function register() * * @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 @@ -92,8 +89,9 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) } 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; } @@ -101,6 +99,7 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) // 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) { @@ -115,43 +114,47 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) // 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; } @@ -163,11 +166,13 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) // 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; @@ -189,19 +194,21 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) // 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 @@ -213,8 +220,8 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) // 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 @@ -222,20 +229,23 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) 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; } } } @@ -244,8 +254,10 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) // 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 '; } @@ -257,7 +269,6 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) } } } - } @@ -269,29 +280,24 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) * * @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); } - - } - -?>