From 4cbfc06022f840c1064c380497cddd2877950c72 Mon Sep 17 00:00:00 2001 From: Dan Cryer Date: Wed, 9 Oct 2013 17:21:33 +0100 Subject: [PATCH] Adding a more standard PHP Lint plugin. Fixes #111 --- PHPCI/Plugin/Lint.php | 104 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 PHPCI/Plugin/Lint.php diff --git a/PHPCI/Plugin/Lint.php b/PHPCI/Plugin/Lint.php new file mode 100644 index 00000000..c8c1b880 --- /dev/null +++ b/PHPCI/Plugin/Lint.php @@ -0,0 +1,104 @@ + + * @package PHPCI + * @subpackage Plugins + */ +class Lint implements \PHPCI\Plugin +{ + protected $directories; + protected $recursive = true; + protected $ignore; + protected $phpci; + + public function __construct(Builder $phpci, Build $build, array $options = array()) + { + $this->phpci = $phpci; + $this->directories = array(''); + $this->ignore = $phpci->ignore; + + if (!empty($options['directory'])) { + $this->directories[] = $options['directory']; + } + + if (!empty($options['directories'])) { + $this->directories = $options['directories']; + } + + if (array_key_exists('recursive', $options)) { + $this->recursive = $options['recursive']; + } + } + + /** + * Executes parallel lint + */ + public function execute() + { + $this->phpci->quiet = true; + $success = true; + + $php = $this->phpci->findBinary('php'); + + foreach ($this->directories as $dir) { + if (!$this->lintDirectory($php, $dir)) { + $success = false; + } + } + + $this->phpci->quiet = false; + + return $success; + } + + protected function lintDirectory($php, $path) + { + $success = true; + $directory = new \DirectoryIterator($this->phpci->buildPath . $path); + + foreach ($directory as $item) { + if ($item->isDot()) { + continue; + } + + $itemPath = $path . $item->getFilename(); + + if (in_array($itemPath, $this->ignore)) { + continue; + } + + if ($item->isFile() && $item->getExtension() == 'php' && !$this->lintFile($php, $itemPath)) { + $success = false; + } else if ($item->isDir() && $this->recursive && !$this->lintDirectory($php, $itemPath . '/')) { + $success = false; + } + } + + return $success; + } + + protected function lintFile($php, $path) + { + $success = true; + + if (!$this->phpci->executeCommand($php . ' -l "%s"', $this->phpci->buildPath . $path)) { + $this->phpci->logFailure($path); + $success = false; + } + + return $success; + } +}