1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SavinMikhail\CommentsDensity\Baseline\Storage; |
6
|
|
|
|
7
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\CommentDTO; |
|
|
|
|
8
|
|
|
|
9
|
|
|
use const DIRECTORY_SEPARATOR; |
10
|
|
|
|
11
|
|
|
final class TreePhpBaselineStorage implements BaselineStorageInterface |
12
|
|
|
{ |
13
|
|
|
private string $path; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array<mixed> |
17
|
|
|
*/ |
18
|
|
|
private array $baselineData = []; |
19
|
|
|
|
20
|
5 |
|
public function init(string $path): void |
21
|
|
|
{ |
22
|
5 |
|
$this->path = $path; |
23
|
5 |
|
if (!file_exists($path)) { |
24
|
5 |
|
file_put_contents($path, '<?php return [];'); |
25
|
|
|
} |
26
|
5 |
|
$this->baselineData = include $path; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param CommentDTO[] $comments |
31
|
|
|
*/ |
32
|
2 |
|
public function setComments(array $comments): void |
33
|
|
|
{ |
34
|
2 |
|
foreach ($comments as $comment) { |
35
|
2 |
|
$pathParts = explode(DIRECTORY_SEPARATOR, ltrim($comment->file, DIRECTORY_SEPARATOR)); |
36
|
2 |
|
$this->addCommentToTree($this->baselineData, $pathParts, $comment); |
37
|
|
|
} |
38
|
2 |
|
file_put_contents($this->path, '<?php return ' . var_export($this->baselineData, true) . ';'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param CommentDTO[] $comments |
43
|
|
|
* @return CommentDTO[] |
44
|
|
|
*/ |
45
|
1 |
|
public function filterComments(array $comments): array |
46
|
|
|
{ |
47
|
1 |
|
$filteredComments = array_filter($comments, function (CommentDTO $comment): bool { |
48
|
1 |
|
$pathParts = explode(DIRECTORY_SEPARATOR, ltrim($comment->file, DIRECTORY_SEPARATOR)); |
49
|
|
|
|
50
|
1 |
|
return !$this->commentExistsInTree($this->baselineData, $pathParts, $comment->line); |
51
|
1 |
|
}); |
52
|
|
|
|
53
|
1 |
|
return array_values($filteredComments); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array<mixed> $tree |
58
|
|
|
* @param string[] $pathParts |
59
|
|
|
*/ |
60
|
3 |
|
private function addCommentToTree(array &$tree, array $pathParts, CommentDTO $comment): void |
61
|
|
|
{ |
62
|
3 |
|
$currentPart = array_shift($pathParts); |
63
|
3 |
|
if (!isset($tree[$currentPart])) { |
64
|
3 |
|
$tree[$currentPart] = []; |
65
|
|
|
} |
66
|
3 |
|
if (empty($pathParts)) { |
67
|
3 |
|
$tree[$currentPart][$comment->line] = [ |
68
|
3 |
|
'comment' => $comment->content, |
69
|
3 |
|
'type' => $comment->commentType, |
70
|
3 |
|
]; |
71
|
|
|
|
72
|
3 |
|
return; |
73
|
|
|
} |
74
|
3 |
|
$this->addCommentToTree($tree[$currentPart], $pathParts, $comment); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array<mixed> $tree |
79
|
|
|
* @param string[] $pathParts |
80
|
|
|
*/ |
81
|
2 |
|
private function commentExistsInTree(array $tree, array $pathParts, int $line): bool |
82
|
|
|
{ |
83
|
2 |
|
$currentPart = array_shift($pathParts); |
84
|
|
|
|
85
|
2 |
|
if (!isset($tree[$currentPart])) { |
86
|
1 |
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
if (empty($pathParts)) { |
90
|
2 |
|
return isset($tree[$currentPart][$line]); |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
return $this->commentExistsInTree($tree[$currentPart], $pathParts, $line); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths