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

TreePhpBaselineStorage::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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