New plugin to support PHP TAL linting.

Closes #701
This commit is contained in:
Stephen Ball 2014-12-11 14:41:00 +00:00 committed by Dan Cryer
parent 83199441bb
commit 9ced5a4097
5 changed files with 355 additions and 3 deletions

View file

@ -68,6 +68,8 @@ class Lint implements PHPCI\Plugin
$success = true;
$php = $this->phpci->findBinary('php');
$this->phpci->logExecOutput(false);
foreach ($this->directories as $dir) {
if (!$this->lintDirectory($php, $dir)) {
@ -76,6 +78,8 @@ class Lint implements PHPCI\Plugin
}
$this->phpci->quiet = false;
$this->phpci->logExecOutput(true);
return $success;
}

266
PHPCI/Plugin/PhpTalLint.php Normal file
View file

@ -0,0 +1,266 @@
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2014, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link https://www.phptesting.org/
*/
namespace PHPCI\Plugin;
use PHPCI;
use PHPCI\Builder;
use PHPCI\Model\Build;
/**
* PHPTAL Lint Plugin - Provides access to PHPTAL lint functionality.
* @author Stephen Ball <phpci@stephen.rebelinblue.com>
* @package PHPCI
* @subpackage Plugins
*/
class PhpTalLint implements PHPCI\Plugin
{
protected $directories;
protected $recursive = true;
protected $suffixes;
protected $ignore;
/**
* @var \PHPCI\Builder
*/
protected $phpci;
/**
* @var \PHPCI\Model\Build
*/
protected $build;
/**
* @var string The path to a file contain custom phptal_tales_ functions
*/
protected $tales;
/**
* @var int
*/
protected $allowed_warnings;
/**
* @var int
*/
protected $allowed_errors;
/**
* @var array The results of the lint scan
*/
protected $failedPaths = array();
/**
* Standard Constructor
*
* @param Builder $phpci
* @param Build $build
* @param array $options
*/
public function __construct(Builder $phpci, Build $build, array $options = array())
{
$this->phpci = $phpci;
$this->build = $build;
$this->directories = array('');
$this->suffixes = array('zpt');
$this->ignore = $phpci->ignore;
$this->allowed_warnings = 0;
$this->allowed_errors = 0;
if (!empty($options['directory'])) {
$this->directories = array($options['directory']);
}
if (isset($options['suffixes'])) {
$this->suffixes = (array)$options['suffixes'];
}
$this->setOptions($options);
}
/**
* Handle this plugin's options.
* @param $options
*/
protected function setOptions($options)
{
foreach (array('directories', 'tales', 'allowed_warnings', 'allowed_errors') as $key) {
if (array_key_exists($key, $options)) {
$this->{$key} = $options[$key];
}
}
}
/**
* Executes phptal lint
*/
public function execute()
{
$this->phpci->quiet = true;
$this->phpci->logExecOutput(false);
foreach ($this->directories as $dir) {
$this->lintDirectory($dir);
}
$this->phpci->quiet = false;
$this->phpci->logExecOutput(true);
$errors = 0;
$warnings = 0;
foreach ($this->failedPaths as $path) {
if ($path['type'] == 'error') {
$errors++;
} else {
$warnings++;
}
}
$this->build->storeMeta('phptallint-warnings', $warnings);
$this->build->storeMeta('phptallint-errors', $errors);
$this->build->storeMeta('phptallint-data', $this->failedPaths);
$success = true;
if ($this->allowed_warnings != -1 && $warnings > $this->allowed_warnings) {
$success = false;
}
if ($this->allowed_errors != -1 && $errors > $this->allowed_errors) {
$success = false;
}
return $success;
}
/**
* Lint an item (file or directory) by calling the appropriate method.
* @param $item
* @param $itemPath
* @return bool
*/
protected function lintItem($item, $itemPath)
{
$success = true;
if ($item->isFile() && in_array(strtolower($item->getExtension()), $this->suffixes)) {
if (!$this->lintFile($itemPath)) {
$success = false;
}
} elseif ($item->isDir() && $this->recursive && !$this->lintDirectory($itemPath . '/')) {
$success = false;
}
return $success;
}
/**
* Run phptal lint against a directory of files.
* @param $path
* @return bool
*/
protected function lintDirectory($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 (!$this->lintItem($item, $itemPath)) {
$success = false;
}
}
return $success;
}
/**
* Run phptal lint against a specific file.
* @param $path
* @return bool
*/
protected function lintFile($path)
{
$success = true;
list($suffixes, $tales) = $this->getFlags();
// FIXME: Find a way to clean this up
$lint = dirname(__FILE__) . '/../../vendor/phptal/phptal/tools/phptal_lint.php';
$cmd = '/usr/bin/env php ' . $lint . ' %s %s "%s"';
$this->phpci->executeCommand($cmd, $suffixes, $tales, $this->phpci->buildPath . $path);
$output = $this->phpci->getLastOutput();
// FIXME: This is very messy, clean it up
if (preg_match('/Found (.+?) (error|warning)/i', $output, $matches)) {
$rows = explode(PHP_EOL, $output);
unset($rows[0]);
unset($rows[1]);
unset($rows[2]);
unset($rows[3]);
foreach ($rows as $row) {
$name = basename($path);
$row = str_replace('(use -i to include your custom modifier functions)', '', $row);
$message = str_replace($name . ': ', '', $row);
$parts = explode(' (line ', $message);
$message = trim($parts[0]);
$line = str_replace(')', '', $parts[1]);
$this->failedPaths[] = array(
'file' => $path,
'line' => $line,
'type' => $matches[2],
'message' => $message
);
}
$success = false;
}
return $success;
}
/**
* Process options and produce an arguments string for PHPTAL Lint.
* @return array
*/
protected function getFlags()
{
$tales = '';
if (!empty($this->tales)) {
$tales = ' -i ' . $this->phpci->buildPath . $this->tales;
}
$suffixes = '';
if (count($this->suffixes)) {
$suffixes = ' -e ' . implode(',', $this->suffixes);
}
return array($suffixes, $tales);
}
}

View file

@ -63,6 +63,7 @@
"atoum/atoum": "Atoum",
"jakub-onderka/php-parallel-lint": "Parallel Linting Tool",
"behat/behat": "Behat BDD Testing",
"hipchat/hipchat-php": "Hipchat integration"
"hipchat/hipchat-php": "Hipchat integration",
"phptal/phptal": "PHPTAL templating engine"
}
}

View file

@ -0,0 +1,79 @@
var phptalPlugin = ActiveBuild.UiPlugin.extend({
id: 'build-phptal',
css: 'col-lg-6 col-md-12 col-sm-12 col-xs-12',
title: 'PHPTAL Lint',
lastData: null,
box: true,
rendered: false,
register: function() {
var self = this;
var query = ActiveBuild.registerQuery('phptallint-data', -1, {key: 'phptallint-data'})
$(window).on('phptallint-data', function(data) {
self.onUpdate(data);
});
$(window).on('build-updated', function() {
if (!self.rendered) {
query();
}
});
},
render: function() {
return $('<table class="table" id="phptal-data">' +
'<thead>' +
'<tr>' +
' <th>File</th>' +
' <th>Line</th>' +
' <th>Message</th>' +
'</tr>' +
'</thead><tbody></tbody></table>');
},
onUpdate: function(e) {
if (!e.queryData) {
$('#build-phptal').hide();
return;
}
this.rendered = true;
this.lastData = e.queryData;
var errors = this.lastData[0].meta_value;
var tbody = $('#phptal-data tbody');
tbody.empty();
if (errors.length == 0) {
$('#build-phptal').hide();
return;
}
for (var i in errors) {
var file = errors[i].file;
if (ActiveBuild.fileLinkTemplate) {
var fileLink = ActiveBuild.fileLinkTemplate.replace('{FILE}', file);
fileLink = fileLink.replace('{LINE}', errors[i].line);
file = '<a target="_blank" href="'+fileLink+'">' + file + '</a>';
}
var row = $('<tr>' +
'<td>'+file+'</td>' +
'<td>'+errors[i].line+'</td>' +
'<td>'+errors[i].message+'</td></tr>');
if (errors[i].type == 'error') {
row.addClass('danger');
}
tbody.append(row);
}
$('#build-phptal').show();
}
});
ActiveBuild.registerPlugin(new phptalPlugin());

View file

@ -8,7 +8,9 @@ var warningsPlugin = ActiveBuild.UiPlugin.extend({
'phpcs-errors': 'PHPCS Errors',
'phplint-errors': 'PHPLint Errors',
'phpunit-errors': 'PHPUnit Errors',
'phpdoccheck-warnings': 'PHP Docblock Checker Warnings'
'phpdoccheck-warnings': 'PHP Docblock Checker Warnings',
'phptallint-errors': 'PHPTAL Lint Errors',
'phptallint-warnings': 'PHPTAL Lint Warnings'
},
data: {},
displayOnUpdate: false,
@ -22,7 +24,7 @@ var warningsPlugin = ActiveBuild.UiPlugin.extend({
queries.push(ActiveBuild.registerQuery(key, -1, {num_builds: 10, key: key}));
}
$(window).on('phpmd-warnings phpcs-warnings phpcs-errors phplint-errors phpunit-errors phpdoccheck-warnings', function(data) {
$(window).on('phpmd-warnings phpcs-warnings phptallint-warnings phptallint-errors phpcs-errors phplint-errors phpunit-errors phpdoccheck-warnings', function(data) {
self.onUpdate(data);
});