Passed
Push — main ( 2bf89b...450136 )
by mikhail
03:21
created

TreePhpBaselineStorage   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 15
dl 0
loc 30
ccs 0
cts 16
cp 0
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 2
A setComments() 0 9 2
A filterComments() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\Baseline\Storage;
6
7
final class TreePhpBaselineStorage implements BaselineStorageInterface
8
{
9
    private string $path;
10
    private array $baselineData = [];
11
12
    public function init(string $path): void
13
    {
14
        $this->path = $path;
15
        if (!file_exists($path)) {
16
            file_put_contents($path, "<?php return [];");
17
        }
18
        $this->baselineData = include $path;
19
    }
20
21
    public function setComments(array $comments): void
22
    {
23
        foreach ($comments as $comment) {
24
            $this->baselineData[$comment->file][$comment->line] = [
25
                'comment' => $comment->content,
26
                'type' => $comment->commentType,
27
            ];
28
        }
29
        file_put_contents($this->path, "<?php return " . var_export($this->baselineData, true) . ";");
30
    }
31
32
    public function filterComments(array $comments): array
33
    {
34
        return array_filter(
35
            $comments,
36
            fn(array $comment) => !isset($this->baselineData[$comment['file']][$comment['line']])
37
        );
38
    }
39
}
40