Passed
Push — main ( 35d689...3d4288 )
by mikhail
03:20
created

BaselineManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
eloc 33
c 3
b 0
f 0
dl 0
loc 70
ccs 0
cts 39
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isUniqueConstraintViolation() 0 3 1
A set() 0 22 4
A init() 0 8 2
A __construct() 0 3 1
A getAllComments() 0 8 1
A getInstance() 0 6 2
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\Database\SQLiteDatabaseManager;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...e\SQLiteDatabaseManager 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
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...
11
12
use function file_exists;
13
use function touch;
14
15
final class BaselineManager
16
{
17
    private static ?self $instance = null;
18
    private Connection $connection;
19
20
    private function __construct()
21
    {
22
        $this->init();
23
    }
24
25
    public static function getInstance(): self
26
    {
27
        if (self::$instance === null) {
28
            self::$instance = new self();
29
        }
30
        return self::$instance;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::instance could return the type null which is incompatible with the type-hinted return SavinMikhail\CommentsDensity\BaselineManager. Consider adding an additional type-check to rule them out.
Loading history...
31
    }
32
33
    private function init(): void
34
    {
35
        $databaseFile = __DIR__ . '/../../comments_density.sqlite';
36
        if (!file_exists($databaseFile)) {
37
            touch($databaseFile);
38
        }
39
        $dbManager = new SQLiteDatabaseManager($databaseFile);
40
        $this->connection = $dbManager->getConnection();
41
    }
42
43
    /**
44
     * @throws Exception
45
     */
46
    public function set(OutputDTO $outputDTO): void
47
    {
48
        foreach ($outputDTO->comments as $comment) {
49
            try {
50
                $this->connection->insert('comments', [
51
                    'file_path' => $comment->file,
52
                    'line_number' => $comment->line,
53
                    'comment' => $comment->content,
54
                    'type' => $comment->commentType,
55
                ]);
56
            } catch (Exception $e) {
57
                if ($this->isUniqueConstraintViolation($e)) {
58
                    $this->connection->update('comments', [
59
                        'comment' => $comment->content,
60
                        'type' => $comment->commentType,
61
                    ], [
62
                        'file_path' => $comment->file,
63
                        'line_number' => $comment->line,
64
                    ]);
65
                    continue;
66
                }
67
                throw $e;
68
            }
69
        }
70
    }
71
72
    private function isUniqueConstraintViolation(Exception $e): bool
73
    {
74
        return str_contains($e->getMessage(), 'UNIQUE constraint failed');
75
    }
76
77
    public function getAllComments(): array
78
    {
79
        $query = $this->connection->createQueryBuilder()
80
            ->select('*')
81
            ->from('comments')
82
            ->executeQuery();
83
84
        return $query->fetchAllAssociative();
85
    }
86
}
87