minimalize modification

This commit is contained in:
Pélisset Vincent 2017-12-14 12:43:28 +01:00 committed by Dmitry Khomutov
parent e8d043fdfd
commit 8bfdea3073
No known key found for this signature in database
GPG key ID: EC19426474B37AAC

View file

@ -9,40 +9,55 @@ use PHPCensor\Plugin;
use PHPCensor\ZeroConfigPluginInterface; use PHPCensor\ZeroConfigPluginInterface;
/** /**
* Technical Debt Plugin - Checks for existence of "TODO", "FIXME", etc. * Technical Debt Plugin - Checks for existence of "TODO", "FIXME", etc.
* *
* @author James Inman <james@jamesinman.co.uk> * @author James Inman <james@jamesinman.co.uk>
*/ */
class TechnicalDebt extends Plugin implements ZeroConfigPluginInterface class TechnicalDebt extends Plugin implements ZeroConfigPluginInterface
{ {
/** /**
* @var array * @var array
*/ */
protected $suffixes; protected $suffixes;
/** /**
* @var string * @var string
*/ */
protected $directory; protected $directory;
/** /**
* @var int * @var int
*/ */
protected $allowed_errors; protected $allowed_errors;
/** /**
* @var array - paths to ignore * @var array - paths to ignore
*/ */
protected $ignore; protected $ignore;
/** /**
* @var array - terms to search for * @var array - terms to search for
*/ */
protected $searches; protected $searches;
/**
* @var array - lines of . and X to visualize errors
*/
protected $errorPerFile = []; protected $errorPerFile = [];
/**
* @var int
*/
protected $currentLineSize = 0; protected $currentLineSize = 0;
/**
* @var int
*/
protected $lineNumber = 0; protected $lineNumber = 0;
/**
* @var int
*/
protected $numberOfAnalysedFile = 0; protected $numberOfAnalysedFile = 0;
/** /**
@ -53,7 +68,7 @@ class TechnicalDebt extends Plugin implements ZeroConfigPluginInterface
return 'technical_debt'; return 'technical_debt';
} }
/** /**
* Store the statu of the file : * Store the status of the file :
* . : checked no errors * . : checked no errors
* X : checked with one or more errr * X : checked with one or more errr
* *
@ -95,9 +110,9 @@ class TechnicalDebt extends Plugin implements ZeroConfigPluginInterface
return $string; return $string;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function __construct(Builder $builder, Build $build, array $options = []) public function __construct(Builder $builder, Build $build, array $options = [])
{ {
parent::__construct($builder, $build, $options); parent::__construct($builder, $build, $options);
@ -123,11 +138,11 @@ class TechnicalDebt extends Plugin implements ZeroConfigPluginInterface
$this->setOptions($options); $this->setOptions($options);
} }
/** /**
* Handle this plugin's options. * Handle this plugin's options.
* *
* @param $options * @param $options
*/ */
protected function setOptions($options) protected function setOptions($options)
{ {
foreach (['directory', 'ignore', 'allowed_errors'] as $key) { foreach (['directory', 'ignore', 'allowed_errors'] as $key) {
@ -137,15 +152,15 @@ class TechnicalDebt extends Plugin implements ZeroConfigPluginInterface
} }
} }
/** /**
* Check if this plugin can be executed. * Check if this plugin can be executed.
* *
* @param string $stage * @param string $stage
* @param Builder $builder * @param Builder $builder
* @param Build $build * @param Build $build
* *
* @return boolean * @return boolean
*/ */
public static function canExecute($stage, Builder $builder, Build $build) public static function canExecute($stage, Builder $builder, Build $build)
{ {
if ($stage == Build::STAGE_TEST) { if ($stage == Build::STAGE_TEST) {
@ -155,9 +170,9 @@ class TechnicalDebt extends Plugin implements ZeroConfigPluginInterface
return false; return false;
} }
/** /**
* Runs the plugin * Runs the plugin
*/ */
public function execute() public function execute()
{ {
$success = true; $success = true;
@ -174,11 +189,11 @@ class TechnicalDebt extends Plugin implements ZeroConfigPluginInterface
return $success; return $success;
} }
/** /**
* Gets the number and list of errors returned from the search * Gets the number and list of errors returned from the search
* *
* @return integer * @return integer
*/ */
protected function getErrorList() protected function getErrorList()
{ {
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->directory)); $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->directory));
@ -186,10 +201,10 @@ class TechnicalDebt extends Plugin implements ZeroConfigPluginInterface
$this->builder->logDebug("Ignored path: ".json_encode($this->ignore, true)); $this->builder->logDebug("Ignored path: ".json_encode($this->ignore, true));
$errorCount = 0; $errorCount = 0;
/** @var \SplFileInfo $file */ /** @var \SplFileInfo $file */
foreach ($iterator as $file) { foreach ($iterator as $file) {
$filePath = $file->getRealPath(); $filePath = $file->getRealPath();
$extension = $file->getExtension(); $extension = $file->getExtension();
$ignored = false; $ignored = false;
foreach ($this->suffixes as $suffix) { foreach ($this->suffixes as $suffix) {
@ -217,12 +232,14 @@ class TechnicalDebt extends Plugin implements ZeroConfigPluginInterface
if (!$ignored) { if (!$ignored) {
$handle = fopen($filePath, "r"); $handle = fopen($filePath, "r");
$lineNumber = 1; $lineNumber = 1;
$found=false; $errorInFile=false;
while (false === feof($handle)) { while (false === feof($handle)) {
$line = fgets($handle); $line = fgets($handle);
foreach ($this->searches as $search) { foreach ($this->searches as $search) {
if ($technicalDeptLine = trim(strstr($line, $search))) { if ($technicalDeptLine = trim(strstr($line, $search))) {
$fileName = str_replace($this->directory, '', $filePath); $fileName = str_replace($this->directory, '', $filePath);
$this->build->reportError( $this->build->reportError(
$this->builder, $this->builder,
'technical_debt', 'technical_debt',
@ -231,14 +248,16 @@ class TechnicalDebt extends Plugin implements ZeroConfigPluginInterface
$fileName, $fileName,
$lineNumber $lineNumber
); );
$found=true;
$errorInFile=true;
$errorCount++; $errorCount++;
} }
} }
$lineNumber++; $lineNumber++;
} }
fclose($handle); fclose ($handle);
if ($found === true) {
if ($errorInFile === true) {
$this->buildLogString('X'); $this->buildLogString('X');
} else { } else {
$this->buildLogString('.'); $this->buildLogString('.');