Sqlite   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 26.79 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 15
loc 56
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A pluginName() 0 4 1
A __construct() 0 11 2
A execute() 15 15 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Fabrica\Tools\Plugin;
4
5
use PDO;
6
use Fabrica\Tools\Builder;
7
use Fabrica\Models\Infra\Ci\Build;
8
use Fabrica\Tools\Plugin;
9
10
/**
11
 * SQLite Plugin — Provides access to a SQLite database.
12
 *
13
 * @author Dmitry Khomutov <[email protected]>
14
 */
15
class Sqlite extends Plugin
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $queries = [];
21
22
    /**
23
     * @var string
24
     */
25
    protected $path;
26
27
    /**
28
     * @return string
29
     */
30
    public static function pluginName()
31
    {
32
        return 'sqlite';
33
    }
34
    
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function __construct(Builder $builder, Build $build, array $options = [])
39
    {
40
        parent::__construct($builder, $build, $options);
41
42
        $buildSettings = $this->builder->getConfig('build_settings');
43
44
        if (isset($buildSettings['sqlite'])) {
45
            $sql        = $buildSettings['sqlite'];
46
            $this->path = $sql['path'];
47
        }
48
    }
49
50
    /**
51
     * Connects to SQLite and runs a specified set of queries.
52
     *
53
     * @return bool
54
     */
55 View Code Duplication
    public function execute()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        try {
58
            $opts = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
59
            $pdo  = new PDO('sqlite:' . $this->path, $opts);
60
61
            foreach ($this->queries as $query) {
62
                $pdo->query($this->builder->interpolate($query));
63
            }
64
        } catch (\Exception $ex) {
65
            $this->builder->logFailure($ex->getMessage());
66
            return false;
67
        }
68
        return true;
69
    }
70
}
71