Repository navigation
Expand file tree
/
Copy pathCodeCloneMap.php
More file actions
127 lines (102 loc) · 2.86 KB
/
Copy pathCodeCloneMap.php
File metadata and controls
127 lines (102 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php declare(strict_types=1);
/*
* This file is part of PHP Copy/Paste Detector (PHPCPD).
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\PHPCPD;
use function count;
use function max;
use function sprintf;
use Countable;
use IteratorAggregate;
final class CodeCloneMap implements Countable, IteratorAggregate
{
/**
* @var CodeClone[]
*/
private array $clones = [];
/**
* @var CodeClone[]
*/
private array $clonesById = [];
private int $numberOfDuplicatedLines = 0;
private int $numberOfLines = 0;
private int $largestCloneSize = 0;
private array $filesWithClones = [];
public function add(CodeClone $clone): void
{
$id = $clone->id();
if (!isset($this->clonesById[$id])) {
$this->clones[] = $clone;
$this->clonesById[$id] = $clone;
} else {
$existClone = $this->clonesById[$id];
foreach ($clone->files() as $file) {
$existClone->add($file);
}
}
$this->numberOfDuplicatedLines += $clone->numberOfLines() * (count($clone->files()) - 1);
foreach ($clone->files() as $file) {
if (!isset($this->filesWithClones[$file->name()])) {
$this->filesWithClones[$file->name()] = true;
}
}
$this->largestCloneSize = max($this->largestCloneSize, $clone->numberOfLines());
}
/**
* @return CodeClone[]
*/
public function clones(): array
{
return $this->clones;
}
public function percentage(): string
{
if ($this->numberOfLines > 0) {
$percent = ($this->numberOfDuplicatedLines / $this->numberOfLines) * 100;
} else {
$percent = 100;
}
return sprintf('%01.2F%%', $percent);
}
public function numberOfLines(): int
{
return $this->numberOfLines;
}
public function addToNumberOfLines(int $numberOfLines): void
{
$this->numberOfLines += $numberOfLines;
}
public function count(): int
{
return count($this->clones);
}
public function numberOfFilesWithClones(): int
{
return count($this->filesWithClones);
}
public function numberOfDuplicatedLines(): int
{
return $this->numberOfDuplicatedLines;
}
public function getIterator(): CodeCloneMapIterator
{
return new CodeCloneMapIterator($this);
}
public function isEmpty(): bool
{
return empty($this->clones);
}
public function averageSize(): float
{
return $this->numberOfDuplicatedLines() / $this->count();
}
public function largestSize(): int
{
return $this->largestCloneSize;
}
}