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
|
|
|
*/
|
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Plugin;
|
2013-07-13 00:19:43 +02:00
|
|
|
|
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;
|
|
|
|
use PHPCensor\ZeroConfigPlugin;
|
2013-10-10 02:01:06 +02:00
|
|
|
|
2013-07-13 00:19:43 +02:00
|
|
|
/**
|
|
|
|
* PHP Loc - Allows PHP Copy / Lines of Code testing.
|
2017-01-10 18:46:19 +01:00
|
|
|
*
|
2013-07-13 00:19:43 +02:00
|
|
|
* @author Johan van der Heide <info@japaveh.nl>
|
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Plugins
|
|
|
|
*/
|
2016-07-11 18:00:04 +02:00
|
|
|
class PhpLoc extends Plugin implements ZeroConfigPlugin
|
2013-07-13 00:19:43 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $directory;
|
|
|
|
|
2017-01-11 16:15:54 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function pluginName()
|
|
|
|
{
|
|
|
|
return 'php_loc';
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
2016-07-11 18:00:04 +02:00
|
|
|
* {@inheritdoc}
|
2014-12-08 12:25:33 +01:00
|
|
|
*/
|
2016-07-22 09:05:34 +02:00
|
|
|
public function __construct(Builder $builder, Build $build, array $options = [])
|
2013-07-13 00:19:43 +02:00
|
|
|
{
|
2016-07-22 09:05:34 +02:00
|
|
|
parent::__construct($builder, $build, $options);
|
2016-07-11 18:00:04 +02:00
|
|
|
|
2016-07-22 09:05:34 +02:00
|
|
|
$this->directory = $this->builder->buildPath;
|
2014-07-29 16:31:27 +02:00
|
|
|
|
|
|
|
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 = '';
|
2015-10-23 05:18:22 +02:00
|
|
|
|
2016-07-22 09:05:34 +02:00
|
|
|
if (count($this->builder->ignore)) {
|
2015-10-23 05:18:22 +02:00
|
|
|
$map = function ($item) {
|
|
|
|
return ' --exclude ' . rtrim($item, DIRECTORY_SEPARATOR);
|
2013-07-13 00:19:43 +02:00
|
|
|
};
|
|
|
|
|
2016-07-22 09:05:34 +02:00
|
|
|
$ignore = array_map($map, $this->builder->ignore);
|
2013-07-13 00:19:43 +02:00
|
|
|
$ignore = implode('', $ignore);
|
|
|
|
}
|
|
|
|
|
2016-07-22 09:05:34 +02:00
|
|
|
$phploc = $this->builder->findBinary('phploc');
|
2013-10-08 09:50:10 +02:00
|
|
|
|
2016-07-22 09:05:34 +02:00
|
|
|
$success = $this->builder->executeCommand($phploc . ' %s "%s"', $ignore, $this->directory);
|
|
|
|
$output = $this->builder->getLastOutput();
|
2013-10-08 08:21:46 +02:00
|
|
|
|
|
|
|
if (preg_match_all('/\((LOC|CLOC|NCLOC|LLOC)\)\s+([0-9]+)/', $output, $matches)) {
|
2016-04-21 06:58:09 +02:00
|
|
|
$data = [];
|
2013-10-08 08:21:46 +02:00
|
|
|
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
|
|
|
}
|