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

94 lines
2.1 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;
use PHPCensor\Builder;
use PHPCensor\Model\Build;
2016-07-11 18:00:04 +02:00
use PHPCensor\Plugin;
2017-01-13 16:35:41 +01:00
use PHPCensor\ZeroConfigPluginInterface;
2013-10-10 02:01:06 +02:00
/**
* PHP Loc - Allows PHP Copy / Lines of Code testing.
*
2017-03-04 16:39:56 +01:00
* @author Johan van der Heide <info@japaveh.nl>
*/
2017-01-13 16:35:41 +01:00
class PhpLoc extends Plugin implements ZeroConfigPluginInterface
{
/**
* @var string
*/
protected $directory;
2017-01-11 16:15:54 +01:00
/**
* @return string
*/
public static function pluginName()
{
return 'php_loc';
}
/**
* 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;
}
/**
2016-07-11 18:00:04 +02:00
* {@inheritdoc}
*/
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;
if (isset($options['directory'])) {
$this->directory .= $options['directory'];
}
}
/**
* Runs PHP Copy/Paste Detector in a specified directory.
*/
public function execute()
{
$ignore = '';
if (count($this->builder->ignore)) {
$map = function ($item) {
return ' --exclude ' . rtrim($item, DIRECTORY_SEPARATOR);
};
$ignore = array_map($map, $this->builder->ignore);
$ignore = implode('', $ignore);
}
$phploc = $this->builder->findBinary('phploc');
2013-10-08 09:50:10 +02:00
$success = $this->builder->executeCommand($phploc . ' %s "%s"', $ignore, $this->directory);
$output = $this->builder->getLastOutput();
if (preg_match_all('/\((LOC|CLOC|NCLOC|LLOC)\)\s+([0-9]+)/', $output, $matches)) {
2016-04-21 06:58:09 +02:00
$data = [];
foreach ($matches[1] as $k => $v) {
$data[$v] = (int)$matches[2][$k];
}
2013-10-10 02:01:06 +02:00
$this->build->storeMeta('phploc', $data);
}
return $success;
}
2013-10-10 02:01:06 +02:00
}