Passed
Push — main ( b8dd52...35d689 )
by mikhail
03:26
created

BaselineManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 30
c 1
b 0
f 0
dl 0
loc 61
ccs 0
cts 37
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isUniqueConstraintViolation() 0 3 1
A set() 0 22 4
A __construct() 0 3 1
A getAllComments() 0 8 1
A getCommentsByFilePath() 0 10 1
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;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDensity\DTO\Output\OutputDTO was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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