Passed
Push — master ( bda737...e0ed8e )
by Nils
09:32
created

Database::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 10
rs 10
1
<?php
2
/**
3
 * Teampass - a collaborative passwords manager.
4
 * ---
5
 * This library is distributed in the hope that it will be useful,
6
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8
 * ---
9
 *
10
 * @project   Teampass API
11
 *
12
 * @file      Database.php
13
 * ---
14
 *
15
 * @author    Nils Laumaillé ([email protected])
16
 *
17
 * @copyright 2009-2022 Teampass.net
18
 *
19
 * @license   https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0
20
 * ---
21
 *
22
 * @see       https://www.teampass.net
23
 */
24
class Database
25
{
26
    protected $connection = null;
27
 
28
    public function __construct()
29
    {
30
        try {
31
            $this->connection = new mysqli(DB_HOST, DB_USER, DB_PASSWD_CLEAR, DB_NAME);
32
33
            if ( mysqli_connect_errno()) {
34
                throw new Exception("Could not connect to database.");   
35
            }
36
        } catch (Exception $e) {
37
            throw new Exception($e->getMessage());   
38
        }           
39
    }
40
 
41
    public function select($query = "" , $params = [])
42
    {
43
        try {
44
            $stmt = $this->executeStatement( $query , $params );
45
            $result = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);               
46
            $stmt->close();
47
 
48
            return $result;
49
        } catch(Exception $e) {
50
            throw New Exception( $e->getMessage() );
51
        }
52
        return false;
0 ignored issues
show
Unused Code introduced by
return false is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
53
    }
54
 
55
    private function executeStatement($query = "" , $params = [])
56
    {
57
        try {
58
            $stmt = $this->connection->prepare( $query );
59
 
60
            if($stmt === false) {
61
                throw New Exception("Unable to do prepared statement: " . $query);
62
            }
63
 
64
            if( $params ) {
65
                $stmt->bind_param($params[0], $params[1]);
66
            }
67
 
68
            $stmt->execute();
69
 
70
            return $stmt;
71
        } catch(Exception $e) {
72
            throw New Exception( $e->getMessage() );
73
        }   
74
    }
75
}