GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Database   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 114
Duplicated Lines 14.04 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 23.81%

Importance

Changes 0
Metric Value
dl 16
loc 114
ccs 10
cts 42
cp 0.2381
rs 10
c 0
b 0
f 0
wmc 12
lcom 2
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A __clone() 0 4 1
C getInstance() 0 28 8
A query() 8 8 1
A exec() 8 8 1

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
 * Kotori.php
4
 *
5
 * A Tiny Model-View-Controller PHP Framework
6
 *
7
 * This content is released under the Apache 2 License
8
 *
9
 * Copyright (c) 2015-2017 Kotori Technology. All rights reserved.
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 *     http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23
24
/**
25
 * Database Class
26
 *
27
 * @package     Kotori
28
 * @subpackage  Core
29
 * @author      Kokororin
30
 * @link        https://kotori.love
31
 */
32
namespace Kotori\Core;
33
34
use Exception;
35
use Kotori\Debug\Hook;
36
use Kotori\Exception\DatabaseException;
37
use Medoo\Medoo;
38
use PDOException;
39
40
class Database extends Medoo
41
{
42
    /**
43
     * SQL queries
44
     *
45
     * @var array
46
     */
47
    public static $queries = [];
48
49
    /**
50
     * Instance Handle
51
     *
52
     * @var array
53
     */
54
    protected static $instance;
55
56
    /**
57
     * Disable Clone
58
     *
59
     * @return boolean
60
     */
61
    public function __clone()
62
    {
63
        return false;
64
    }
65
66
    /**
67
     * Get singleton
68
     *
69
     * @param  string  $key
70
     * @return object
71
     *
72
     * @throws \Kotori\Exception\DatabaseException
73
     */
74 2
    public static function getInstance($key = null)
75
    {
76 2
        $config = Container::get('config')->get('db');
77 2
        if (!is_array($config) || count($config) == 0) {
78 2
            return null;
79
        } elseif ($key == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $key of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
80
            $dbKeys = array_keys($config);
81
            if (isset($dbKeys[0])) {
82
                $key = $dbKeys[0];
83
            } else {
84
                return null;
85
            }
86
        }
87
88
        Container::get('config')->set('selected_db_key', $key);
89
90
        if (!isset(self::$instance[$key])) {
91
            try {
92
                self::$instance[$key] = new self($config[$key]);
93
            } catch (PDOException $e) {
94
                throw new DatabaseException($e);
95
            } catch (Exception $e) {
96
                throw new DatabaseException($e);
97
            }
98
        }
99
100
        return self::$instance[$key];
101
    }
102
103
    /**
104
     * Class constructor
105
     *
106
     * Initialize Database.
107
     */
108
    public function __construct(array $options = [])
109
    {
110
        parent::__construct([
111
            'database_type' => $options['type'],
112
            'database_name' => $options['name'],
113
            'server' => $options['host'],
114
            'username' => $options['user'],
115
            'password' => $options['pwd'],
116
            'charset' => $options['charset'],
117
            'port' => $options['port'],
118
        ]);
119
        Hook::listen(__CLASS__);
120
    }
121
122
    /**
123
     * medoo::query()
124
     *
125
     * @param  string $query
126
     * @param  array  $map
127
     * @return \PDOStatement
128
     */
129 View Code Duplication
    public function query($query, $map = [])
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...
130
    {
131
        $statement = parent::exec($query, $map);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (exec() instead of query()). Are you sure this is correct? If so, you might want to change this to $this->exec().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
Comprehensibility Best Practice introduced by
The expression parent::exec($query, $map); of type false|PDOStatement adds false to the return on line 135 which is incompatible with the return type documented by Kotori\Core\Database::query of type PDOStatement. It seems like you forgot to handle an error condition.
Loading history...
132
        $lastSQL = parent::last();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (last() instead of query()). Are you sure this is correct? If so, you might want to change this to $this->last().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
133
        Container::get('logger')->info($lastSQL);
134
        array_push(self::$queries, $lastSQL);
135
        return $statement;
136
    }
137
138
    /**
139
     * medoo:exec()
140
     *
141
     * @param  string $query
142
     * @param  array  $map
143
     * @return \PDOStatement
144
     */
145 4 View Code Duplication
    public function exec($query, $map = [])
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...
146
    {
147 4
        $statement = parent::exec($query, $map);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression parent::exec($query, $map); of type false|PDOStatement adds false to the return on line 151 which is incompatible with the return type documented by Kotori\Core\Database::exec of type PDOStatement. It seems like you forgot to handle an error condition.
Loading history...
148 4
        $lastSQL = parent::last();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (last() instead of exec()). Are you sure this is correct? If so, you might want to change this to $this->last().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
149 4
        Container::get('logger')->info($lastSQL);
150 4
        array_push(self::$queries, $lastSQL);
151 4
        return $statement;
152
    }
153
}
154