php-censor/src/PHPCensor/Plugin/PhpParallelLint.php

132 lines
3.2 KiB
PHP
Raw Normal View History

<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Plugin;
2016-07-19 20:28:11 +02:00
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
2017-01-13 16:35:41 +01:00
use PHPCensor\ZeroConfigPluginInterface;
2013-10-10 02:01:06 +02:00
/**
* Php Parallel Lint Plugin - Provides access to PHP lint functionality.
*
2017-03-04 16:39:56 +01:00
* @author Vaclav Makes <vaclav@makes.cz>
*/
2017-01-13 16:35:41 +01:00
class PhpParallelLint extends Plugin implements ZeroConfigPluginInterface
{
/**
* @var string
*/
protected $directory;
/**
* @var array - paths to ignore
*/
protected $ignore;
/**
* @var string - comma separated list of file extensions
*/
protected $extensions;
2017-01-11 16:15:54 +01:00
/**
* @return string
*/
public static function pluginName()
{
return 'php_parallel_lint';
}
/**
* $options['directory'] Output Directory. Default: %BUILDPATH%
* $options['filename'] Phar Filename. Default: build.phar
* $options['extensions'] Filename extensions. Default: php
* $options['stub'] Stub Content. No Default Value
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
2016-07-11 18:00:04 +02:00
$this->directory = $this->builder->buildPath;
$this->ignore = $this->builder->ignore;
$this->extensions = 'php';
if (isset($options['directory'])) {
$this->directory = $this->builder->buildPath.$options['directory'];
}
if (isset($options['ignore'])) {
$this->ignore = $options['ignore'];
}
if (isset($options['extensions'])) {
// Only use if this is a comma delimited list
$pattern = '/^([a-z]+)(,\ *[a-z]*)*$/';
if (preg_match($pattern, $options['extensions'])) {
$this->extensions = str_replace(' ', '', $options['extensions']);
}
}
}
/**
* Check if this plugin can be executed.
*
* @param $stage
* @param Builder $builder
* @param Build $build
*
* @return bool
*/
public static function canExecute($stage, Builder $builder, Build $build)
{
if ($stage == 'test') {
return true;
}
return false;
}
/**
* Executes parallel lint
*/
public function execute()
{
list($ignore) = $this->getFlags();
$phplint = $this->builder->findBinary('parallel-lint');
$cmd = $phplint . ' -e %s' . ' %s "%s"';
$success = $this->builder->executeCommand(
$cmd,
$this->extensions,
$ignore,
$this->directory
);
$output = $this->builder->getLastOutput();
2016-04-21 06:58:09 +02:00
$matches = [];
if (preg_match_all('/Parse error\:/', $output, $matches)) {
$this->build->storeMeta('phplint-errors', count($matches[0]));
}
return $success;
}
/**
* Produce an argument string for PHP Parallel Lint.
* @return array
*/
protected function getFlags()
{
2016-04-21 06:58:09 +02:00
$ignoreFlags = [];
foreach ($this->ignore as $ignoreDir) {
$ignoreFlags[] = '--exclude "' . $this->builder->buildPath . $ignoreDir . '"';
}
$ignore = implode(' ', $ignoreFlags);
2016-04-21 06:58:09 +02:00
return [$ignore];
}
}