1
|
|
|
<?php |
2
|
|
|
namespace phphound\integration; |
3
|
|
|
|
4
|
|
|
use phphound\AnalysisResult; |
5
|
|
|
use phphound\helper\ArrayHelper; |
6
|
|
|
use Sabre\Xml\Reader; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Integration of PHPHound with PHPCopyPasteDetector. |
10
|
|
|
* @see https://github.com/sebastianbergmann/phpcpd |
11
|
|
|
*/ |
12
|
|
View Code Duplication |
class PHPCopyPasteDetector extends AbstractIntegration |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @inheritdoc |
16
|
|
|
*/ |
17
|
|
|
public function getDescription() |
18
|
|
|
{ |
19
|
|
|
return 'PHPCopyPasteDetector'; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @inheritdoc |
24
|
|
|
*/ |
25
|
|
|
public function getIgnoredArgument() |
26
|
|
|
{ |
27
|
|
|
if (!empty($this->ignoredPaths)) { |
28
|
|
|
return '--exclude={' . implode(',', $this->ignoredPaths) . '} '; |
29
|
|
|
} |
30
|
|
|
return ''; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
|
|
public function getCommand($targetPaths) |
37
|
|
|
{ |
38
|
|
|
return $this->binariesPath . 'phpcpd ' |
39
|
|
|
. implode(' ', $targetPaths) . ' ' |
40
|
|
|
. $this->getIgnoredArgument() . '--log-pmd="' |
41
|
|
|
. $this->temporaryFilePath . '"'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
|
|
protected function addIssuesFromXml(Reader $xml) |
48
|
|
|
{ |
49
|
|
|
$xmlArray = $xml->parse(); |
50
|
|
|
|
51
|
|
|
foreach ((array) $xmlArray['value'] as $duplicationTag) { |
52
|
|
|
if ($duplicationTag['name'] != '{}duplication' |
53
|
|
|
|| empty($duplicationTag['value'])) { |
54
|
|
|
continue; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
foreach ((array) $duplicationTag['value'] as $fileTag) { |
58
|
|
|
if ($fileTag['name'] != '{}file') { |
59
|
|
|
continue; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$fileName = $fileTag['attributes']['path']; |
63
|
|
|
$line = $fileTag['attributes']['line']; |
64
|
|
|
$tool = 'PHPCopyPasteDetector'; |
65
|
|
|
$type = 'duplication'; |
66
|
|
|
$message = 'Duplicated code'; |
67
|
|
|
|
68
|
|
|
$this->result->addIssue($fileName, $line, $tool, $type, $message); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.