Fixed namespaces (PHPCI -> PHPCensor)
This commit is contained in:
parent
60d74b0b44
commit
60a2b7282a
238 changed files with 1014 additions and 863 deletions
209
src/PHPCensor/Plugin/TechnicalDebt.php
Executable file
209
src/PHPCensor/Plugin/TechnicalDebt.php
Executable file
|
|
@ -0,0 +1,209 @@
|
|||
<?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 PHPCensor\Plugin;
|
||||
|
||||
use PHPCensor;
|
||||
use PHPCensor\Builder;
|
||||
use PHPCensor\Model\Build;
|
||||
|
||||
/**
|
||||
* Technical Debt Plugin - Checks for existence of "TODO", "FIXME", etc.
|
||||
*
|
||||
* @author James Inman <james@jamesinman.co.uk>
|
||||
* @package PHPCI
|
||||
* @subpackage Plugins
|
||||
*/
|
||||
class TechnicalDebt implements PHPCensor\Plugin, PHPCensor\ZeroConfigPlugin
|
||||
{
|
||||
/**
|
||||
* @var \PHPCensor\Builder
|
||||
*/
|
||||
protected $phpci;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $suffixes;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $directory;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $allowed_errors;
|
||||
|
||||
/**
|
||||
* @var string, based on the assumption the root may not hold the code to be
|
||||
* tested, extends the base path
|
||||
*/
|
||||
protected $path;
|
||||
|
||||
/**
|
||||
* @var array - paths to ignore
|
||||
*/
|
||||
protected $ignore;
|
||||
|
||||
/**
|
||||
* @var array - terms to search for
|
||||
*/
|
||||
protected $searches;
|
||||
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \PHPCI\Builder $phpci
|
||||
* @param \PHPCI\Model\Build $build
|
||||
* @param array $options
|
||||
*/
|
||||
public function __construct(Builder $phpci, Build $build, array $options = [])
|
||||
{
|
||||
$this->phpci = $phpci;
|
||||
$this->build = $build;
|
||||
$this->suffixes = ['php'];
|
||||
$this->directory = $phpci->buildPath;
|
||||
$this->path = '';
|
||||
$this->ignore = $this->phpci->ignore;
|
||||
$this->allowed_errors = 0;
|
||||
$this->searches = ['TODO', 'FIXME', 'TO DO', 'FIX ME'];
|
||||
|
||||
if (isset($options['searches']) && is_array($options['searches'])) {
|
||||
$this->searches = $options['searches'];
|
||||
}
|
||||
|
||||
if (isset($options['zero_config']) && $options['zero_config']) {
|
||||
$this->allowed_errors = -1;
|
||||
}
|
||||
|
||||
$this->setOptions($options);
|
||||
|
||||
$this->phpci->logDebug('Plugin options: ' . json_encode($options));
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle this plugin's options.
|
||||
* @param $options
|
||||
*/
|
||||
protected function setOptions($options)
|
||||
{
|
||||
foreach (['directory', 'path', 'ignore', 'allowed_errors'] as $key) {
|
||||
if (array_key_exists($key, $options)) {
|
||||
$this->{$key} = $options[$key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the plugin
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$success = true;
|
||||
$this->phpci->logExecOutput(false);
|
||||
|
||||
$errorCount = $this->getErrorList();
|
||||
|
||||
$this->phpci->log("Found $errorCount instances of " . implode(', ', $this->searches));
|
||||
|
||||
$this->build->storeMeta('technical_debt-warnings', $errorCount);
|
||||
|
||||
if ($this->allowed_errors != -1 && $errorCount > $this->allowed_errors) {
|
||||
$success = false;
|
||||
}
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number and list of errors returned from the search
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getErrorList()
|
||||
{
|
||||
$dirIterator = new \RecursiveDirectoryIterator($this->directory);
|
||||
$iterator = new \RecursiveIteratorIterator($dirIterator, \RecursiveIteratorIterator::SELF_FIRST);
|
||||
$files = [];
|
||||
|
||||
$ignores = $this->ignore;
|
||||
$ignores[] = 'phpci.yml';
|
||||
$ignores[] = '.phpci.yml';
|
||||
|
||||
foreach ($iterator as $file) {
|
||||
$filePath = $file->getRealPath();
|
||||
$skipFile = false;
|
||||
foreach ($ignores as $ignore) {
|
||||
if (stripos($filePath, $ignore) !== false) {
|
||||
$skipFile = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Ignore hidden files, else .git, .sass_cache, etc. all get looped over
|
||||
if (stripos($filePath, DIRECTORY_SEPARATOR . '.') !== false) {
|
||||
$skipFile = true;
|
||||
}
|
||||
|
||||
if ($skipFile == false) {
|
||||
$files[] = $file->getRealPath();
|
||||
}
|
||||
}
|
||||
|
||||
$files = array_filter(array_unique($files));
|
||||
$errorCount = 0;
|
||||
|
||||
foreach ($files as $file) {
|
||||
foreach ($this->searches as $search) {
|
||||
$fileContent = file_get_contents($file);
|
||||
$allLines = explode(PHP_EOL, $fileContent);
|
||||
$beforeString = strstr($fileContent, $search, true);
|
||||
|
||||
if (false !== $beforeString) {
|
||||
$lines = explode(PHP_EOL, $beforeString);
|
||||
$lineNumber = count($lines);
|
||||
$content = trim($allLines[$lineNumber - 1]);
|
||||
|
||||
$errorCount++;
|
||||
|
||||
$fileName = str_replace($this->directory, '', $file);
|
||||
|
||||
$this->build->reportError(
|
||||
$this->phpci,
|
||||
'technical_debt',
|
||||
$content,
|
||||
PHPCensor\Model\BuildError::SEVERITY_LOW,
|
||||
$fileName,
|
||||
$lineNumber
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $errorCount;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue