2013-07-13 00:19:43 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
2014-05-12 18:26:17 +02:00
|
|
|
* @copyright Copyright 2014, Block 8 Limited.
|
2013-07-13 00:19:43 +02:00
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
2014-05-12 18:26:17 +02:00
|
|
|
* @link https://www.phptesting.org/
|
2013-07-13 00:19:43 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace PHPCI\Plugin;
|
|
|
|
|
2014-04-25 11:17:39 +02:00
|
|
|
use PHPCI;
|
2013-10-10 02:01:06 +02:00
|
|
|
use PHPCI\Builder;
|
|
|
|
use PHPCI\Model\Build;
|
|
|
|
|
2013-07-13 00:19:43 +02:00
|
|
|
/**
|
|
|
|
* PHP Loc - Allows PHP Copy / Lines of Code testing.
|
|
|
|
* @author Johan van der Heide <info@japaveh.nl>
|
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Plugins
|
|
|
|
*/
|
2014-04-25 11:17:39 +02:00
|
|
|
class PhpLoc implements PHPCI\Plugin, PHPCI\ZeroConfigPlugin
|
2013-07-13 00:19:43 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $directory;
|
|
|
|
/**
|
|
|
|
* @var \PHPCI\Builder
|
|
|
|
*/
|
|
|
|
protected $phpci;
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Check if this plugin can be executed.
|
|
|
|
* @param $stage
|
|
|
|
* @param Builder $builder
|
|
|
|
* @param Build $build
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-04-25 11:17:39 +02:00
|
|
|
public static function canExecute($stage, Builder $builder, Build $build)
|
|
|
|
{
|
|
|
|
if ($stage == 'test') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Set up the plugin, configure options, etc.
|
|
|
|
* @param Builder $phpci
|
|
|
|
* @param Build $build
|
|
|
|
* @param array $options
|
|
|
|
*/
|
2013-10-10 02:01:06 +02:00
|
|
|
public function __construct(Builder $phpci, Build $build, array $options = array())
|
2013-07-13 00:19:43 +02:00
|
|
|
{
|
|
|
|
$this->phpci = $phpci;
|
2013-10-10 02:01:06 +02:00
|
|
|
$this->build = $build;
|
2014-07-29 16:31:27 +02:00
|
|
|
$this->directory = $phpci->buildPath;
|
|
|
|
|
|
|
|
if (isset($options['directory'])) {
|
|
|
|
$this->directory .= $options['directory'];
|
|
|
|
}
|
2013-07-13 00:19:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs PHP Copy/Paste Detector in a specified directory.
|
|
|
|
*/
|
|
|
|
public function execute()
|
|
|
|
{
|
|
|
|
$ignore = '';
|
|
|
|
if (count($this->phpci->ignore)) {
|
|
|
|
$map = function ($item) {
|
|
|
|
return ' --exclude ' . (substr($item, -1) == '/' ? substr($item, 0, -1) : $item);
|
|
|
|
};
|
|
|
|
$ignore = array_map($map, $this->phpci->ignore);
|
|
|
|
|
|
|
|
$ignore = implode('', $ignore);
|
|
|
|
}
|
|
|
|
|
2013-10-08 09:50:10 +02:00
|
|
|
$phploc = $this->phpci->findBinary('phploc');
|
|
|
|
|
2014-05-02 19:47:08 +02:00
|
|
|
$success = $this->phpci->executeCommand($phploc . ' %s "%s"', $ignore, $this->directory);
|
2013-10-08 08:21:46 +02:00
|
|
|
$output = $this->phpci->getLastOutput();
|
|
|
|
|
|
|
|
if (preg_match_all('/\((LOC|CLOC|NCLOC|LLOC)\)\s+([0-9]+)/', $output, $matches)) {
|
|
|
|
$data = array();
|
|
|
|
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);
|
2013-10-08 08:21:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $success;
|
2013-07-13 00:19:43 +02:00
|
|
|
}
|
2013-10-10 02:01:06 +02:00
|
|
|
}
|