1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SavinMikhail\CommentsDensity; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use Doctrine\DBAL\Exception; |
9
|
|
|
use SavinMikhail\CommentsDensity\DTO\Output\OutputDTO; |
|
|
|
|
10
|
|
|
|
11
|
|
|
final class BaselineManager |
12
|
|
|
{ |
13
|
|
|
public function __construct( |
14
|
|
|
private Connection $connection |
15
|
|
|
) { |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @throws Exception |
20
|
|
|
*/ |
21
|
|
|
public function set(OutputDTO $outputDTO): void |
22
|
|
|
{ |
23
|
|
|
foreach ($outputDTO->comments as $comment) { |
24
|
|
|
try { |
25
|
|
|
$this->connection->insert('comments', [ |
26
|
|
|
'file_path' => $comment->file, |
27
|
|
|
'line_number' => $comment->line, |
28
|
|
|
'comment' => $comment->content, |
29
|
|
|
'type' => $comment->commentType, |
30
|
|
|
]); |
31
|
|
|
} catch (Exception $e) { |
32
|
|
|
if ($this->isUniqueConstraintViolation($e)) { |
33
|
|
|
$this->connection->update('comments', [ |
34
|
|
|
'comment' => $comment->content, |
35
|
|
|
'type' => $comment->commentType, |
36
|
|
|
], [ |
37
|
|
|
'file_path' => $comment->file, |
38
|
|
|
'line_number' => $comment->line, |
39
|
|
|
]); |
40
|
|
|
continue; |
41
|
|
|
} |
42
|
|
|
throw $e; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function isUniqueConstraintViolation(Exception $e): bool |
48
|
|
|
{ |
49
|
|
|
return str_contains($e->getMessage(), 'UNIQUE constraint failed'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getAllComments(): array |
53
|
|
|
{ |
54
|
|
|
$query = $this->connection->createQueryBuilder() |
55
|
|
|
->select('*') |
56
|
|
|
->from('comments') |
57
|
|
|
->executeQuery(); |
58
|
|
|
|
59
|
|
|
return $query->fetchAllAssociative(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getCommentsByFilePath(string $filePath): array |
63
|
|
|
{ |
64
|
|
|
$query = $this->connection->createQueryBuilder() |
65
|
|
|
->select('*') |
66
|
|
|
->from('comments') |
67
|
|
|
->where('file_path = :file_path') |
68
|
|
|
->setParameter('file_path', $filePath) |
69
|
|
|
->executeQuery(); |
70
|
|
|
|
71
|
|
|
return $query->fetchAllAssociative(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
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