|
1
|
|
|
<?php |
|
2
|
|
|
namespace exussum12\CoverageChecker; |
|
3
|
|
|
|
|
4
|
|
|
class Phpcpd implements FileChecker |
|
5
|
|
|
{ |
|
6
|
|
|
protected $file; |
|
7
|
|
|
protected $duplicateCode = []; |
|
8
|
|
|
public function __construct($file) |
|
9
|
|
|
{ |
|
10
|
|
|
$this->file = fopen($file, 'r'); |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
public function getLines() |
|
14
|
|
|
{ |
|
15
|
|
|
$block = []; |
|
16
|
|
|
$this->duplicateCode = []; |
|
17
|
|
|
while (($line = fgets($this->file)) !== false) { |
|
18
|
|
|
if (!$this->hasFileName($line)) { |
|
19
|
|
|
continue; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
if ($this->startOfBlock($line)) { |
|
23
|
|
|
$this->handleEndOfBlock($block); |
|
24
|
|
|
$block = []; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
$block += $this->addFoundBlock($line); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
return $this->duplicateCode; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
public function isValidLine($file, $lineNumber) |
|
35
|
|
|
{ |
|
36
|
|
|
return empty($this->duplicateCode[$file][$lineNumber]); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function handleNotFoundFile() |
|
40
|
|
|
{ |
|
41
|
|
|
return true; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public static function getDescription() |
|
45
|
|
|
{ |
|
46
|
|
|
return "Parses the text output from phpcpd (Copy Paste Detect)"; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private function startOfBlock($line) |
|
50
|
|
|
{ |
|
51
|
|
|
return preg_match('/^\s+-/', $line); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function hasFileName($line) |
|
55
|
|
|
{ |
|
56
|
|
|
return preg_match('/:\d+-\d+/', $line); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private function addFoundBlock($line) |
|
60
|
|
|
{ |
|
61
|
|
|
$matches = []; |
|
62
|
|
|
preg_match('/\s+(?:- )?(?<fileName>.*?):(?<startLine>\d+)-(?<endLine>\d+)$/', $line, $matches); |
|
63
|
|
|
return [$matches['fileName'] => range($matches['startLine'], $matches['endLine'])]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private function handleEndOfBlock($block) |
|
67
|
|
|
{ |
|
68
|
|
|
foreach ($block as $filename => $lines) { |
|
69
|
|
|
foreach ($lines as $lineNumber) { |
|
70
|
|
|
foreach ($block as $duplicate => $dupeLines) { |
|
71
|
|
|
if ($filename == $duplicate) { |
|
72
|
|
|
continue; |
|
73
|
|
|
} |
|
74
|
|
|
$start = reset($dupeLines); |
|
75
|
|
|
$end = end($dupeLines); |
|
76
|
|
|
$message = "Duplicate of " . $duplicate . ':' . $start . '-' . $end; |
|
77
|
|
|
$this->duplicateCode[$filename][$lineNumber][] = $message; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|