• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemsdk / phpcpd / #12

13 Apr 2025 04:42PM UTC coverage: 75.711%. Remained the same
#12

push

DKravtsov
phpcpd 8.1.1 release. Available installation via composer.

692 of 914 relevant lines covered (75.71%)

3.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.69
/src/CodeCloneMap.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Systemsdk\PhpCPD;
6

7
use Countable;
8
use IteratorAggregate;
9

10
use function count;
11
use function max;
12
use function sprintf;
13

14
final class CodeCloneMap implements Countable, IteratorAggregate
15
{
16
    /**
17
     * @var array<int, CodeClone>
18
     */
19
    private array $clones = [];
20

21
    /**
22
     * @var array<string, CodeClone>
23
     */
24
    private array $clonesById = [];
25

26
    private int $numberOfDuplicatedLines = 0;
27

28
    private int $numberOfLines = 0;
29

30
    private int $largestCloneSize = 0;
31

32
    /**
33
     * @var array<string, bool>
34
     */
35
    private array $filesWithClones = [];
36

37
    public function add(CodeClone $clone): void
38
    {
39
        $id = $clone->id();
9✔
40

41
        if (!isset($this->clonesById[$id])) {
9✔
42
            $this->clones[] = $clone;
9✔
43
            $this->clonesById[$id] = $clone;
9✔
44
        } else {
45
            $existClone = $this->clonesById[$id];
1✔
46

47
            foreach ($clone->files() as $file) {
1✔
48
                $existClone->add($file);
1✔
49
            }
50
        }
51

52
        $this->numberOfDuplicatedLines += $clone->numberOfLines() * (count($clone->files()) - 1);
9✔
53

54
        foreach ($clone->files() as $file) {
9✔
55
            if (!isset($this->filesWithClones[$file->name()])) {
9✔
56
                $this->filesWithClones[$file->name()] = true;
9✔
57
            }
58
        }
59

60
        $this->largestCloneSize = max($this->largestCloneSize, $clone->numberOfLines());
9✔
61
    }
62

63
    /**
64
     * @return array<int, CodeClone>
65
     */
66
    public function clones(): array
67
    {
68
        return $this->clones;
13✔
69
    }
70

71
    public function percentage(): string
72
    {
73
        if ($this->numberOfLines > 0) {
×
74
            $percent = ($this->numberOfDuplicatedLines / $this->numberOfLines) * 100;
×
75
        } else {
76
            $percent = 100;
×
77
        }
78

79
        return sprintf('%01.2F%%', $percent);
×
80
    }
81

82
    public function numberOfLines(): int
83
    {
84
        return $this->numberOfLines;
×
85
    }
86

87
    public function addToNumberOfLines(int $numberOfLines): void
88
    {
89
        $this->numberOfLines += $numberOfLines;
12✔
90
    }
91

92
    public function count(): int
93
    {
94
        return count($this->clones);
×
95
    }
96

97
    public function numberOfFilesWithClones(): int
98
    {
99
        return count($this->filesWithClones);
×
100
    }
101

102
    public function numberOfDuplicatedLines(): int
103
    {
104
        return $this->numberOfDuplicatedLines;
×
105
    }
106

107
    public function getIterator(): CodeCloneMapIterator
108
    {
109
        return new CodeCloneMapIterator($this);
1✔
110
    }
111

112
    public function isEmpty(): bool
113
    {
114
        return empty($this->clones);
×
115
    }
116

117
    public function averageSize(): float
118
    {
119
        return $this->numberOfDuplicatedLines() / $this->count();
×
120
    }
121

122
    public function largestSize(): int
123
    {
124
        return $this->largestCloneSize;
×
125
    }
126
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc