2013-05-03 17:02:53 +02:00
|
|
|
<?php
|
2013-05-16 03:16:56 +02:00
|
|
|
/**
|
2014-05-12 18:26:17 +02:00
|
|
|
* 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/
|
|
|
|
*/
|
2013-05-03 17:02:53 +02:00
|
|
|
|
|
|
|
namespace PHPCI\Plugin;
|
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
use PHPCI\Builder;
|
|
|
|
use PHPCI\Model\Build;
|
|
|
|
|
2013-05-16 03:30:48 +02:00
|
|
|
/**
|
|
|
|
* PHP Copy / Paste Detector - Allows PHP Copy / Paste Detector testing.
|
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Plugins
|
|
|
|
*/
|
2013-05-03 17:02:53 +02:00
|
|
|
class PhpCpd implements \PHPCI\Plugin
|
|
|
|
{
|
2013-05-16 16:50:19 +02:00
|
|
|
protected $directory;
|
|
|
|
protected $args;
|
|
|
|
protected $phpci;
|
2014-05-02 15:48:40 +02:00
|
|
|
protected $build;
|
2013-05-03 17:02:53 +02:00
|
|
|
|
2013-09-18 10:54:23 +02:00
|
|
|
/**
|
|
|
|
* @var string, based on the assumption the root may not hold the code to be
|
|
|
|
* tested, exteds the base path
|
|
|
|
*/
|
|
|
|
protected $path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array - paths to ignore
|
|
|
|
*/
|
|
|
|
protected $ignore;
|
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
public function __construct(Builder $phpci, Build $build, array $options = array())
|
2013-05-16 16:50:19 +02:00
|
|
|
{
|
2013-10-10 02:01:06 +02:00
|
|
|
$this->phpci = $phpci;
|
2014-05-02 15:48:40 +02:00
|
|
|
$this->build = $build;
|
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
$this->path = $phpci->buildPath;
|
|
|
|
$this->standard = 'PSR1';
|
|
|
|
$this->ignore = $phpci->ignore;
|
|
|
|
|
|
|
|
if (!empty($options['path'])) {
|
|
|
|
$this->path = $phpci->buildPath . $options['path'];
|
|
|
|
}
|
2013-09-18 10:54:23 +02:00
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
if (!empty($options['standard'])) {
|
|
|
|
$this->standard = $options['standard'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($options['ignore'])) {
|
2014-06-12 18:06:59 +02:00
|
|
|
$this->ignore = $options['ignore'];
|
2013-10-10 02:01:06 +02:00
|
|
|
}
|
2013-05-16 16:50:19 +02:00
|
|
|
}
|
2013-05-03 17:02:53 +02:00
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Runs PHP Copy/Paste Detector in a specified directory.
|
|
|
|
*/
|
2013-05-16 16:50:19 +02:00
|
|
|
public function execute()
|
|
|
|
{
|
|
|
|
$ignore = '';
|
2013-09-18 10:54:23 +02:00
|
|
|
if (count($this->ignore)) {
|
2013-05-16 16:50:19 +02:00
|
|
|
$map = function ($item) {
|
2014-05-19 19:00:31 +02:00
|
|
|
// remove the trailing slash
|
|
|
|
$item = (substr($item, -1) == '/' ? substr($item, 0, -1) : $item);
|
|
|
|
|
|
|
|
if (is_file($this->path . '/' . $item)) {
|
|
|
|
return ' --names-exclude ' . $item;
|
|
|
|
} else {
|
|
|
|
return ' --exclude ' . $item;
|
|
|
|
}
|
|
|
|
|
2013-05-16 16:50:19 +02:00
|
|
|
};
|
2013-09-18 10:54:23 +02:00
|
|
|
$ignore = array_map($map, $this->ignore);
|
2013-05-03 17:02:53 +02:00
|
|
|
|
2013-05-16 16:50:19 +02:00
|
|
|
$ignore = implode('', $ignore);
|
|
|
|
}
|
2013-05-03 17:02:53 +02:00
|
|
|
|
2013-10-08 09:50:10 +02:00
|
|
|
$phpcpd = $this->phpci->findBinary('phpcpd');
|
|
|
|
|
|
|
|
if (!$phpcpd) {
|
|
|
|
$this->phpci->logFailure('Could not find phpcpd.');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-12 18:06:59 +02:00
|
|
|
$tmpfilename = tempnam('/tmp', 'phpcpd');
|
|
|
|
|
2014-07-11 15:07:27 +02:00
|
|
|
$cmd = $phpcpd . ' --log-pmd="%s" %s "%s"';
|
|
|
|
$success = $this->phpci->executeCommand($cmd, $tmpfilename, $ignore, $this->path);
|
2013-10-08 08:21:46 +02:00
|
|
|
|
|
|
|
print $this->phpci->getLastOutput();
|
2014-06-12 18:06:59 +02:00
|
|
|
|
|
|
|
list($errorCount, $data) = $this->processReport(file_get_contents($tmpfilename));
|
|
|
|
$this->build->storeMeta('phpcpd-warnings', $errorCount);
|
|
|
|
$this->build->storeMeta('phpcpd-data', $data);
|
|
|
|
|
|
|
|
unlink($tmpfilename);
|
2013-10-08 08:21:46 +02:00
|
|
|
|
|
|
|
return $success;
|
2013-05-16 16:50:19 +02:00
|
|
|
}
|
2014-06-12 18:06:59 +02:00
|
|
|
|
|
|
|
protected function processReport($xmlString)
|
|
|
|
{
|
|
|
|
$xml = simplexml_load_string($xmlString);
|
|
|
|
|
|
|
|
if ($xml === false) {
|
|
|
|
$this->phpci->log($xmlString);
|
|
|
|
throw new \Exception('Could not process PHPCPD report XML.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$warnings = 0;
|
|
|
|
$data = array();
|
|
|
|
|
|
|
|
foreach ($xml->duplication as $duplication) {
|
|
|
|
foreach ($duplication->file as $file) {
|
|
|
|
$fileName = (string)$file['path'];
|
|
|
|
$fileName = str_replace($this->phpci->buildPath, '', $fileName);
|
|
|
|
|
|
|
|
$data[] = array(
|
|
|
|
'file' => $fileName,
|
|
|
|
'line_start' => (int) $file['line'],
|
|
|
|
'line_end' => (int) $file['line'] + (int) $duplication['lines'],
|
|
|
|
'code' => (string) $duplication->codefragment
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$warnings++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array($warnings, $data);
|
|
|
|
}
|
2013-05-16 16:50:19 +02:00
|
|
|
}
|