Commit: ed80f173bef291624545f82ee6128cde1d2fad18

Author: David Persson | Date: 2009-12-26 20:42:32 +0100
Removing support for checks in favor of utilizing PHPca directly.
diff --git a/config/bootstrap.php b/config/bootstrap.php index 8158154..fd2ce3d 100644 --- a/config/bootstrap.php +++ b/config/bootstrap.php @@ -66,10 +66,16 @@ Libraries::add('app', array('bootstrap' => false)); /** * Add libraries from submodules. */ -// Libraries::add('phpca', array( -// 'path' => dirname(__DIR__) . '/libraries/phpca/src', -// 'loader' => '\spriebsch\Loader\Autoloader::autoload' -// )); + +require LITHIUM_APP_PATH . '/libraries/phpca/src/Exceptions.php'; +require LITHIUM_APP_PATH . '/libraries/phpca/src/Loader.php'; + +\spriebsch\PHPca\Loader::registerPath(LITHIUM_APP_PATH . '/libraries/phpca/src'); + +Libraries::add('phpca', array( + 'path' => dirname(__DIR__) . '/libraries/phpca/src', + 'loader' => array('\spriebsch\PHPca\Loader', 'autoload') +)); ?> \ No newline at end of file diff --git a/extensions/command/Syntax.php b/extensions/command/Syntax.php index 70b3d05..0d250fa 100644 --- a/extensions/command/Syntax.php +++ b/extensions/command/Syntax.php @@ -9,30 +9,15 @@ namespace app\extensions\command; -use lithium\core\Libraries; -use lithium\util\Inflector; use lithium\util\String; -use \RecursiveIteratorIterator; -use \RecursiveDirectoryIterator; +use spriebsch\PHPca\Application; +use spriebsch\PHPca\Configuration; +use spriebsch\PHPca\Result; /** * Runs syntax checks against files. */ -class Syntax extends \lithium\console\Command { - - /** - * Comma-separated list of checks to use. - * - * @var string - */ - public $checks = 'phpca'; - - /** - * A regex to exclude paths from being checked. - * - * @var string Regular expression. - */ - public $exclude = '\.'; +class Syntax extends \lithium\console\Command implements \spriebsch\PHPca\ProgressPrinterInterface { /** * Enable output of metrics. @@ -58,8 +43,6 @@ class Syntax extends \lithium\console\Command { * @param string $path Absolute path to file or directory. */ public function run($path) { - $this->checks = explode(',' , $this->checks); - if (!$path = realpath($path)) { $this->error('Not a valid path.'); return false; @@ -71,127 +54,97 @@ class Syntax extends \lithium\console\Command { if (is_dir($this->_project . '/.git')) { $this->_vcs = 'git'; } - $failures = is_file($path) ? $this->_checkFile($path) : $this->_checkDirectory($path); - if ($this->metrics) { - $this->_metrics($failures); - } - return !$failures; - } + $app = new Application(getcwd()); + $app->registerProgressPrinter($this); - public function checks() { - $this->header('Available Checks:'); - $classes = array_unique(Libraries::locate('command.syntax', null, array( - 'recursive' => false - ))); - foreach ($classes as $command) { - $command = explode('\\', $command); - $this->out(' - ' . Inflector::underscore(array_pop($command))); - } - } + $file = LITHIUM_APP_PATH . '/config/phpca_lithium_standard.ini'; - protected function _project($path) { - while ($path && !is_dir($path . '/.git') && !is_dir($path . '/config/bootstrap.php')) { - $path = ($parent = dirname($path)) != $path ? $parent : false; + $config = new Configuration(getcwd()); + $config->setStandard(parse_ini_file($file, true)); + // $config->setConfiguration(array()); + + $php = PHP_BINDIR . '/' . (substr(PHP_OS, 0, 3) == 'WIN' ? 'php.exe' : 'php'); + $result = $app->run($php, $path, $config); + + if ($this->metrics) { + $this->_metrics($result); } - return $path; + return !$result->hasErrors(); } - protected function _checkFile($file) { + public function showProgress($file, Result $result, Application $application) { $message = 'Checking syntax of `' . str_replace($this->_project . '/', null, $file) .'`. '; $this->out($message, false); - $failures = array(); - - foreach ($this->checks as $check) { - $class = Libraries::locate('command.syntax', Inflector::camelize($check)); - $check = new $class(array('request' => $this->request)); - - if (!$check->accepts($file)) { - $this->out("Skipped. ", false); - } elseif ($failures = $check->process($file)) { - $this->out("Failed. ", false); - } else { - $this->out("Passed. ", false); - } - } - $this->nl(); - if ($failures) { - foreach ($failures as &$failure) { - $failure['author'] = $this->_blame($failure); - $this->error(sprintf( - $this->blame ? '%1$4u| %2$3u| %3$20s| %4$s' : '%1$4u| %2$3u| %4$s', - $failure['line'] ?: '??', - $failure['column'] ?: '??', - $failure['author'] ?: '??', - $failure['message'] ?: '??' + if ($result->wasSkipped($file)) { + $this->out('Skipped.'); + } elseif ($result->hasLintError($file)) { + $this->out('Error.'); + $error = $result->getLintError($file); + $this->out(sprintf( + '%1$4u| %2$3u| %3$20s| %4$s', + $error->getLine() ?: '??', + $error->getColumn() ?: '??', + $this->_blame($error) ?: '??', + $error->getMessage() ?: '??' + )); + + } elseif ($result->hasRuleError($file)) { + $this->out('Error.'); + + } elseif ($result->hasViolations($file)) { + $this->out('Failed.'); + + foreach ($result->getViolations($file) as $violation) { + $this->out(sprintf( + '%1$4u| %2$3u| %3$20s| %4$s', + $violation->getLine() ?: '??', + $violation->getColumn() ?: '??', + $this->_blame($violation) ?: '??', + $violation->getMessage() ?: '??' )); } - $this->nl(); - return $failures; + } else { + $this->out('Passed.'); } } - protected function _checkDirectory($directory) { - $base = new RecursiveDirectoryIterator($directory); - $iterator = new RecursiveIteratorIterator($base); - $failures = array(); - - foreach ($iterator as $item) { - $basename = $item->getBasename(); - $file = $item->getPathname(); - - if (preg_match('/\/' . $this->exclude . '/', $file) || $basename == 'empty') { - continue; - } - if ($result = $this->_checkFile($file)) { - $failures = array_merge($failures, $result); - } + protected function _project($path) { + while ($path && !is_dir($path . '/.git') && !is_dir($path . '/config/bootstrap.php')) { + $path = ($parent = dirname($path)) != $path ? $parent : false; } - return $failures; + return $path; } - protected function _metrics($failures) { + protected function _metrics($result) { + $this->nl(); $this->header('Metrics'); $this->nl(); - $total = count($failures); - $byAuthor = array(); - - foreach ($failures as $failure) { - $byAuthor[$failure['author']][$failure['message']][] = $failure; - } - ksort($byAuthor); - - foreach ($byAuthor as $author => $failures) { - if (!$author) { - continue; - } - $this->out($author); - ksort($failures); - - foreach ($failures as $message => $messageFailures) { - $this->out(' - `' . $message . '` ('. count($messageFailures) .')'); - } - $this->nl(); - } + $this->out('Files: ' . $result->getNumberOfFiles()); + $this->out('Skipped: ' . $result->getNumberOfSkippedFiles()); $this->nl(); - $this->out("Total: {$total}"); + $this->out('Lint errors: ' . $result->getNumberOfLintErrors()); + $this->out('Rule errors: ' . $result->getNumberOfRuleErrors()); + $this->out('Violations: ' . $result->getNumberOfViolations()); $this->nl(); } - protected function _blame($failure) { - if (!$this->_vcs == 'git') { + protected function _blame($error) { + if (!$this->blame || !$this->_vcs == 'git') { return null; } $backup = getcwd(); chdir($this->_project); - $lines = count(file($failure['file'])); + $line = $error->getLine(); + $file = $error->getFilename(); + $lines = count(file($file)); $command = 'git blame -L{:start},{:end} --porcelain {:file}'; $replace = array( - 'start' => $failure['line'], - 'end' => $lines == $failure['line'] ? $failure['line'] : $failure['line'] + 1, - 'file' => $failure['file'] + 'start' => $line, + 'end' => $lines == $line ? $line : $line + 1, + 'file' => $file ); exec(String::insert($command, $replace), $output, $return); chdir($backup); diff --git a/extensions/command/syntax/Base.php b/extensions/command/syntax/Base.php deleted file mode 100644 index c3ede58..0000000 --- a/extensions/command/syntax/Base.php +++ /dev/null @@ -1,35 +0,0 @@ -<?php -/** - * Lithium Hooks: A collection of git hooks & scripts that can be used for development in the - * Lithium core and with Lithium applications. - * - * @copyright Copyright 2009, Union of Rad, Inc. (http://union-of-rad.org) - * @license http://opensource.org/licenses/bsd-license.php The BSD License - */ - -namespace app\extensions\command\syntax; - -/** - * This is the base class all verification command need to extend. - */ -abstract class Base extends \lithium\console\Command { - - /** - * Checks if given file can be checked. - * - * @param string $file Absolute path to file. - * @return boolean - */ - abstract public function accepts($file); - - /** - * Process a file by checking it's contents. - * - * @param string $file Absolute path to file. - * @return array|boolean|void `false` if file cannot be processed or - if applicable - - * an array of violation messages. - */ - abstract public function process($file); -} - -?> \ No newline at end of file diff --git a/extensions/command/syntax/Jsl.php b/extensions/command/syntax/Jsl.php deleted file mode 100644 index 153b424..0000000 --- a/extensions/command/syntax/Jsl.php +++ /dev/null @@ -1,45 +0,0 @@ -<?php -/** - * Lithium Hooks: A collection of git hooks & scripts that can be used for development in the - * Lithium core and with Lithium applications. - * - * @copyright Copyright 2009, Union of Rad, Inc. (http://union-of-rad.org) - * @license http://opensource.org/licenses/bsd-license.php The BSD License - */ - -namespace app\extensions\command\syntax; - -use lithium\util\String; - -class Jsl extends \app\extensions\command\syntax\Base { - - public function accepts($file) { - return file_exists($file) && preg_match('/\.js$/', $file); - } - - public function process($file) { - $command = '{:jsl} -nologo -nosummary -nofilelisting -nocontext -process {:file}'; - $replace = array( - 'jsl' => 'jsl', - 'file' => $file - ); - exec(String::insert($command, $replace), $output, $return); - - if ($return != 3) { - return null; - } - $format = function($line) use ($file) { - $regex = '/\((?P<line>\d+)\)\:\s(?P<message>.*)/'; - preg_match($regex, $line, $matches); - return array( - 'file' => $file, - 'line' => isset($matches['line']) ? $matches['line'] : null, - 'column' => null, - 'message' => isset($matches['message']) ? $matches['message'] : null - ); - }; - return array_map($format, $output); - } -} - -?> \ No newline at end of file diff --git a/extensions/command/syntax/Phpca.php b/extensions/command/syntax/Phpca.php deleted file mode 100644 index df96973..0000000 --- a/extensions/command/syntax/Phpca.php +++ /dev/null @@ -1,49 +0,0 @@ -<?php -/** - * Lithium Hooks: A collection of git hooks & scripts that can be used for development in the - * Lithium core and with Lithium applications. - * - * @copyright Copyright 2009, Union of Rad, Inc. (http://union-of-rad.org) - * @license http://opensource.org/licenses/bsd-license.php The BSD License - */ - -namespace app\extensions\command\syntax; - -use lithium\util\String; - -class Phpca extends \app\extensions\command\syntax\Base { - - public function accepts($file) { - return file_exists($file) && preg_match('/\.php$/', $file); - } - - public function process($file) { - $plugin = dirname(dirname(dirname(__DIR__))); - $command = '{:php} {:phpca} -p {:php} --standard {:standard} {:file}'; - $replace = array( - 'php' => PHP_BINDIR . '/' . (substr(PHP_OS, 0, 3) == 'WIN' ? 'php.exe' : 'php'), - 'phpca' => $plugin . '/libraries/phpca/src/phpca.php', - 'standard' => $plugin . '/config/phpca_lithium_standard.ini', - 'file' => $file - ); - exec(String::insert($command, $replace), $output, $return); - - if ($return == 0) { - return null; - } - $format = function($line) use ($file) { - $regex = '/(?P<line>\d+)\|\s*(?P<column>\d+)\|\s+(?P<message>.*)/'; - preg_match($regex, $line, $matches); - - return array( - 'file' => $file, - 'line' => isset($matches['line']) ? $matches['line'] : null, - 'column' => isset($matches['column']) ? $matches['column'] : null, - 'message' => isset($matches['message']) ? $matches['message'] : null - ); - }; - return array_map($format, array_filter(array_slice($output, 9, -3))); - } -} - -?> \ No newline at end of file diff --git a/extensions/command/syntax/Phpl.php b/extensions/command/syntax/Phpl.php deleted file mode 100644 index eebdad4..0000000 --- a/extensions/command/syntax/Phpl.php +++ /dev/null @@ -1,48 +0,0 @@ -<?php -/** - * Lithium Hooks: A collection of git hooks & scripts that can be used for development in the - * Lithium core and with Lithium applications. - * - * @copyright Copyright 2009, Union of Rad, Inc. (http://union-of-rad.org) - * @license http://opensource.org/licenses/bsd-license.php The BSD License - */ - -namespace app\extensions\command\syntax; - -use lithium\util\String; - -class Phpl extends \app\extensions\command\syntax\Base { - - public function accepts($file) { - return file_exists($file) && preg_match('/\.php$/', $file); - } - - public function process($file) { - $command = '{:php} -l {:file} 2> /dev/null'; - $replace = array( - 'php' => 'php', - 'file' => $file - ); - exec(String::insert($command, $replace), $output, $return); - - if ($return == 0) { - return null; - } - $filter = function($failure) { - return !empty($failure) && $failure[0] == 'P'; - }; - $format = function($line) use ($file) { - $regex = '/(?P<message>.*)\sin\s(?P<file>.*)\son\sline\s(?P<line>\d+)/'; - preg_match($regex, $line, $matches); - return array( - 'file' => $file, - 'line' => isset($matches['line']) ? $matches['line'] : null, - 'column' => null, - 'message' => isset($matches['message']) ? $matches['message'] : null - ); - }; - return array_map($format, array_filter($output, $filter)); - } -} - -?> \ No newline at end of file