From 84f1ffb8a2055fcbffede07b7f8c6efdd062fd84 Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Wed, 20 May 2009 08:14:38 +0000 Subject: [PATCH] MDL-19247 Reverted CodeSniffer.php and CLI.php to original state, and subclassed them in MoodleCodeSniffer.php and MoodleCLI.php in order to reproduce the small changes. Also updated runsniffer script. --- lib/pear/PHP/CodeSniffer.php | 11 ---- lib/pear/PHP/CodeSniffer/CLI.php | 7 ++- lib/pear/PHP/CodeSniffer/MoodleCLI.php | 87 ++++++++++++++++++++++++++ lib/pear/PHP/MoodleCodeSniffer.php | 87 ++++++++++++++++++++++++++ lib/pear/PHP/runsniffer | 20 +++--- 5 files changed, 189 insertions(+), 23 deletions(-) create mode 100644 lib/pear/PHP/CodeSniffer/MoodleCLI.php create mode 100644 lib/pear/PHP/MoodleCodeSniffer.php diff --git a/lib/pear/PHP/CodeSniffer.php b/lib/pear/PHP/CodeSniffer.php index 2c1efaf1fdb..5d463414fe5 100644 --- a/lib/pear/PHP/CodeSniffer.php +++ b/lib/pear/PHP/CodeSniffer.php @@ -542,17 +542,6 @@ class PHP_CodeSniffer $di = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); } - // MOODLE CODE: If thirdpartylibs.xml is found, add these values to the ignored array - // first iteration to find thirdpartylibs.xml - foreach ($di as $file) { - if ($file->getFileName() == 'thirdpartylibs.xml') { - $xml = simplexml_load_file($file->getPathName()); - foreach ($xml->library as $libobject) { - $this->ignorePatterns[] = (string) $libobject->location; - } - } - } - foreach ($di as $file) { $filePath = realpath($file->getPathname()); diff --git a/lib/pear/PHP/CodeSniffer/CLI.php b/lib/pear/PHP/CodeSniffer/CLI.php index b13bd63ab6b..566b6da07ef 100644 --- a/lib/pear/PHP/CodeSniffer/CLI.php +++ b/lib/pear/PHP/CodeSniffer/CLI.php @@ -388,7 +388,7 @@ class PHP_CodeSniffer_CLI // Try to get the default from the config system. $standard = PHP_CodeSniffer::getConfigData('default_standard'); if ($standard === null) { - $standard = 'Moodle'; + $standard = 'PEAR'; } } @@ -416,7 +416,7 @@ class PHP_CodeSniffer_CLI */ public function printUsage() { - echo 'Usage: phpcs [-nwlvi] [--report=]'.PHP_EOL; + echo 'Usage: phpcs [-nwlvi] [--report=] [--standard=]'.PHP_EOL; echo ' [--config-set key value] [--config-delete key] [--config-show]'.PHP_EOL; echo ' [--generator=] [--extensions=]'.PHP_EOL; echo ' [--ignore=] [--tab-width=] ...'.PHP_EOL; @@ -432,6 +432,7 @@ class PHP_CodeSniffer_CLI echo ' (only valid if checking a directory)'.PHP_EOL; echo ' A comma separated list of patterns that are used'.PHP_EOL; echo ' to ignore directories and files'.PHP_EOL; + echo ' The name of the coding standard to use'.PHP_EOL; echo ' The number of spaces each tab represents'.PHP_EOL; echo ' The name of a doc generator to use'.PHP_EOL; echo ' (forces doc generation instead of checking)'.PHP_EOL; @@ -457,7 +458,7 @@ class PHP_CodeSniffer_CLI } else { $lastStandard = array_pop($installedStandards); if ($numStandards === 1) { - echo "The only coding standard installed is $lastStandard".PHP_EOL; + echo 'The only coding standard installed is $lastStandard'.PHP_EOL; } else { $standardList = implode(', ', $installedStandards); $standardList .= ' and '.$lastStandard; diff --git a/lib/pear/PHP/CodeSniffer/MoodleCLI.php b/lib/pear/PHP/CodeSniffer/MoodleCLI.php new file mode 100644 index 00000000000..781edb85cd1 --- /dev/null +++ b/lib/pear/PHP/CodeSniffer/MoodleCLI.php @@ -0,0 +1,87 @@ +. +/** + * Subclass of lib/pear/PHP/CodeSniffer/CLI.php + * + * Simple modifications to the CLI class to only use the Moodle Standard + * + * @package lib-pear-php-codesniffer + * @copyright 2008 Nicolas Connault + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +if (is_file(dirname(__FILE__).'/../MoodleCodeSniffer.php') === true) { + include_once(dirname(__FILE__).'/../MoodleCodeSniffer.php'); +} else { + include_once('PHP/MoodleCodeSniffer.php'); +} + +require_once('PHP/CodeSniffer/CLI.php'); + +/** + * A class to process command line phpcs scripts. Modified for use within Moodle + * + * @category lib-pear-php-codesniffer + * @copyright 2009 Nicolas Connault + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class moodle_codesniffer_cli extends php_codesniffer_cli { + /** + * Modified to return Moodle only + * + * @param string $standard The standard to validate. + * + * @return string + */ + public function validatestandard($standard) { + return 'Moodle'; + } + + /** + * Prints out the usage information for this script. + * + * Modified by removing the --standard option + * + * @return void + */ + public function printusage() { + echo 'Usage: phpcs [-nwlvi] [--report=]'.PHP_EOL; + echo ' [--config-set key value] [--config-delete key] [--config-show]'.PHP_EOL; + echo ' [--generator=] [--extensions=]'.PHP_EOL; + echo ' [--ignore=] [--tab-width=] ...'.PHP_EOL; + echo ' -n Do not print warnings'.PHP_EOL; + echo ' -w Print both warnings and errors (on by default)'.PHP_EOL; + echo ' -l Local directory only, no recursion'.PHP_EOL; + echo ' -v[v][v] Print verbose output'.PHP_EOL; + echo ' -i Show a list of installed coding standards'.PHP_EOL; + echo ' --help Print this help message'.PHP_EOL; + echo ' --version Print version information'.PHP_EOL; + echo ' One or more files and/or directories to check'.PHP_EOL; + echo ' A comma separated list of file extensions to check'.PHP_EOL; + echo ' (only valid if checking a directory)'.PHP_EOL; + echo ' A comma separated list of patterns that are used'.PHP_EOL; + echo ' to ignore directories and files'.PHP_EOL; + echo ' The number of spaces each tab represents'.PHP_EOL; + echo ' The name of a doc generator to use'.PHP_EOL; + echo ' (forces doc generation instead of checking)'.PHP_EOL; + echo ' Print either the "full", "xml", "checkstyle",'.PHP_EOL; + echo ' "csv" or "summary" report'.PHP_EOL; + echo ' (the "full" report is printed by default)'.PHP_EOL; + + }//end printUsage() + +} diff --git a/lib/pear/PHP/MoodleCodeSniffer.php b/lib/pear/PHP/MoodleCodeSniffer.php new file mode 100644 index 00000000000..f1f04571f86 --- /dev/null +++ b/lib/pear/PHP/MoodleCodeSniffer.php @@ -0,0 +1,87 @@ +. +/** + * Sub-class of lib/pear/PHP/CodeSniffer.php + * + * Modified to read thirdpartylibs.xml from a recursed directory and apply + * its contents to the ignored list. + * + * @package lib-pear-php-codesniffer + * @copyright 2008 Nicolas Connault + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +require_once('PHP/CodeSniffer/CLI.php'); + +if (class_exists('PHP_CodeSniffer', true) === false) { + throw new Exception('Class PHP_CodeSniffer not found'); +} + +class moodle_codesniffer extends php_codesniffer { + public function processFiles($dir, $local=false) { + try { + if ($local === true) { + $di = new DirectoryIterator($dir); + } else { + $di = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); + } + + // MOODLE CODE: If thirdpartylibs.xml is found, add these values to the ignored array + // first iteration to find thirdpartylibs.xml + foreach ($di as $file) { + if ($file->getFileName() == 'thirdpartylibs.xml') { + $xml = simplexml_load_file($file->getPathName()); + foreach ($xml->library as $libobject) { + $this->ignorePatterns[] = (string) $libobject->location; + } + } + } + + foreach ($di as $file) { + $filePath = realpath($file->getPathname()); + + if (is_dir($filePath) === true) { + continue; + } + + // Check that the file's extension is one we are checking. + // Note that because we are doing a whole directory, we + // are strick about checking the extension and we don't + // let files with no extension through. + $fileParts = explode('.', $file); + $extension = array_pop($fileParts); + if ($extension === $file) { + continue; + } + + if (isset($this->allowedFileExtensions[$extension]) === false) { + continue; + } + + $this->processFile($filePath); + }//end foreach + } catch (Exception $e) { + $trace = $e->getTrace(); + $filename = $trace[0]['args'][0]; + $error = 'An error occurred during processing; checking has been aborted. The error message was: '.$e->getMessage(); + + $phpcsFile = new PHP_CodeSniffer_File($filename, $this->listeners, $this->allowedFileExtensions); + $this->addFile($phpcsFile); + $phpcsFile->addError($error, null); + return; + } + } +} diff --git a/lib/pear/PHP/runsniffer b/lib/pear/PHP/runsniffer index fc3e3e666c6..d77aac81996 100755 --- a/lib/pear/PHP/runsniffer +++ b/lib/pear/PHP/runsniffer @@ -1,27 +1,29 @@ #!/usr/bin/php checkRequirements(); $numErrors = $phpcs->process();