1
|
|
|
<?php |
2
|
|
|
namespace Finder\Logic\Tools; |
3
|
|
|
|
4
|
|
|
use Finder\Logic\AnalysisResult; |
5
|
|
|
use Support\Helps\ArrayHelper; |
6
|
|
|
use Sabre\Xml\Reader; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Integration of CodeAnalyser with CopyPasteDetector. |
10
|
|
|
* |
11
|
|
|
* @see https://github.com/sebastianbergmann/phpcpd |
12
|
|
|
*/ |
13
|
|
View Code Duplication |
class CopyPasteDetector extends AbstractTool |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @inheritdoc |
17
|
|
|
*/ |
18
|
|
|
public function getDescription() |
19
|
|
|
{ |
20
|
|
|
return 'CopyPasteDetector'; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @inheritdoc |
25
|
|
|
*/ |
26
|
|
|
public function getIgnoredArgument() |
27
|
|
|
{ |
28
|
|
|
if (!empty($this->ignoredPaths)) { |
29
|
|
|
return '--exclude={' . implode(',', $this->ignoredPaths) . '} '; |
30
|
|
|
} |
31
|
|
|
return ''; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
|
|
public function getCommand($targetPaths) |
38
|
|
|
{ |
39
|
|
|
return $this->binariesPath . 'phpcpd ' |
40
|
|
|
. implode(' ', $targetPaths) . ' ' |
41
|
|
|
. $this->getIgnoredArgument() . '--log-pmd="' |
42
|
|
|
. $this->temporaryFilePath . '"'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
|
protected function addIssuesFromXml(Reader $xml) |
49
|
|
|
{ |
50
|
|
|
$xmlArray = $xml->parse(); |
51
|
|
|
|
52
|
|
|
foreach ((array) $xmlArray['value'] as $duplicationTag) { |
53
|
|
|
if ($duplicationTag['name'] != '{}duplication' |
54
|
|
|
|| empty($duplicationTag['value']) |
55
|
|
|
) { |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
foreach ((array) $duplicationTag['value'] as $fileTag) { |
60
|
|
|
if ($fileTag['name'] != '{}file') { |
61
|
|
|
continue; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$fileName = $fileTag['attributes']['path']; |
65
|
|
|
$line = $fileTag['attributes']['line']; |
66
|
|
|
$tool = 'CopyPasteDetector'; |
67
|
|
|
$type = 'duplication'; |
68
|
|
|
$message = 'Duplicated code'; |
69
|
|
|
|
70
|
|
|
$this->result->addIssue($fileName, $line, $tool, $type, $message); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
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.