Issues (11)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Database/DataBaseQuery.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Class DataBase:
5
 * This class performs the basic CRUD operations which compose of
6
 * various methods such as create, read, update, and delete.
7
 * This class class query the database to achieve its function.
8
 *
9
 * @author: Raimi Ademola <[email protected]>
10
 * @copyright: 2016 Andela
11
 */
12
namespace Demo;
13
14
use PDO;
15
16
17
class DataBaseQuery
18
{
19
    protected $tableName;
20
    protected $splitTableField;
21
    protected $formatTableValues;
22
    protected $dbConnection;
23
24
    /**
25
     * This is a constructor; a default method  that will be called automatically during class 
26
     * instantiation.
27
     */
28
    public function __construct($dbConn = null)
29
    {
30
        if (is_null($dbConn)) {
31
            $this->dbConnection = new DataBaseConnection();
32
        } else {
33
            $this->dbConnection = $dbConn;
34 72
        }
35
    }
36 72
37 51
    /**
38 34
     * This method create or insert new users to the table.
39 72
     *
40
     * @param $associativeArray
41 72
     * @param $tableName
42
     *
43
     * @return array
44
     */
45
    public function create($associativeArrayToCreate, $tableName)
46
    {
47
        $tableFields = [];
48
        $tableValues = [];
49
50
        foreach ($associativeArrayToCreate as $key => $val) {
51 6
            $tableFields[] = $key;
52
            $tableValues[] = $val;
53 6
        }
54 6
        
55
        $unexpectedArray = array_diff($tableFields, $this->getColumnNames($tableName));
56 6
57 6
        if (count($unexpectedArray) < 1) {
58 6
            $sql = 'INSERT INTO '.$tableName;
59 4
            $sql .= '('.$this->splitTableField($tableFields).') ';
60
            $sql .= 'VALUES ('.$this->formatTableValues($tableValues).')';
61 6
62
            $bool = $this->dbConnection->exec($sql);
63 6
64 3
            return $bool;
65 3
        }
66 3
        
67
        throw new FieldUndefinedException('Oops, '.$this->splitTableField($unexpectedArray).' is not defined as a field');
68 3
    }
69
70 3
    /**
71
     * This method read the data in the table name of the id being passed to it.
72
     *
73 3
     * @param $id
74
     * @param $tableName
75
     *
76
     * @return array
77
     */
78
    public static function read($id, $tableName, $dbConn = null)
79
    {
80
        if (is_null($dbConn)) {
81
            $dbConn = new DataBaseConnection();
82
        }
83
84 9
        $sql = $id ? 'SELECT * FROM '.$tableName.' WHERE id = '.$id : 'SELECT * FROM '.$tableName;
85
        $statement = $dbConn->prepare($sql);
86 9
        $statement->execute();
87
        
88
        $results = $statement->fetchAll(PDO::FETCH_ASSOC);
89
90 9
        if (count($results) < 1) {
91 9
            throw new IdNotFoundException('Oops, the id '.$id.' is not in the table, try another id');
92 9
        }
93
94 9
        return $results;
95
    }
96 9
97 3
    /**
98
     * This method delete the table name of the id being passed to it.
99
     *
100 6
     * @param $update Params
101
     * @param $associativeArrayToUpdate
102
     * @param $tableName
103
     *
104
     * @return bool
105
     */
106
    public function update($updateParams, $associativeArrayToUpdate, $tableName)
107
    {
108
        $updateSql = "UPDATE `$tableName` SET ";
109
110
        unset($associativeArrayToUpdate['id']);
111
112 6
        foreach ($associativeArrayToUpdate as $key => $val) {
113
            $tableFields[] = $key;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tableFields was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tableFields = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
114 6
        }
115
116 6
        $unexpectedArray = array_diff($tableFields, $this->getColumnNames($tableName));
0 ignored issues
show
The variable $tableFields does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
117
118 6
        if (count($unexpectedArray) < 1) {
119 6
            $updateSql .= $this->updateArraySql($associativeArrayToUpdate);
120 4
            
121
            foreach ($updateParams as $field => $value) {
122 6
                $updateSql .= " WHERE $field = $value";
123
            }
124 6
125 3
            $statement = $this->dbConnection->exec($updateSql);
126
            
127 3
            return $statement;
128 3
        }
129 2
130
        throw new FieldUndefinedException('Oops, '.$this->splitTableField($unexpectedArray).' is not defined as a field');
131 3
    }
132
133 3
    /**
134
     * This method delete the table name of the id passed to it.
135
     *
136 3
     * @param $id
137
     * @param $tableName
138
     *
139
     * @return bool
140
     */
141
    public static function delete($id, $tableName, $dbConn = null)
142
    {
143
        if (is_null($dbConn)) {
144
            $dbConn = new DataBaseConnection();
145
        }
146
147 9
        $sql = 'SELECT * FROM '.$tableName.' WHERE id = '.$id;
148
        $statement = $dbConn->prepare($sql);
149 9
        $statement->execute();
150
        $results = $statement->fetchAll(PDO::FETCH_ASSOC);
151
152
        if (count($results) < 1) {
153 9
            throw new IdNotFoundException('Oops, the id '.$id.' is not in the database, try another id');
154 9
        }
155 9
156 9
        $sql = 'DELETE FROM '.$tableName.' WHERE id = '.$id;
157
        $statement = $dbConn->exec($sql);
0 ignored issues
show
$statement is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
158 9
159 3
        return true;
160
    }
161
162 6
    /**
163 6
     * This method returns a string form an array by making us of the implode function.
164
     *
165 6
     * @param $tableField
166
     *
167
     * @return string
168
     */
169
    public function splitTableField($tableField)
170
    {
171
        $splitTableField = implode(', ', $tableField);
172
173
        return $splitTableField;
174
    }
175 12
176
    /**
177 12
     * This method returns a string formed fron an array, It format the array.
178
     *
179 12
     * @param $tableValues
180
     *
181
     * @return string
182
     */
183
    public function formatTableValues($tableValues)
184
    {
185
        $formattedValues = [];
186
187
        foreach ($tableValues as $key => $value) {
188
            $formattedValues[] = "'".$value."'";
189 3
        }
190
191 3
        $ValueSql = implode(',', $formattedValues);
192
193 3
        return $ValueSql;
194 3
    }
195 2
196
    /**
197 3
     * This method returns a string formed from an array.
198
     *
199 3
     * @param $array
200
     *
201
     * @return string
202
     */
203
    public function updateArraySql($array)
204
    {
205
        $updatedValues = [];
206
207
        foreach ($array as $key => $val) {
208
            $updatedValues[] = "`$key` = '$val'";
209 6
        }
210
211 6
        $valueSql = implode(',', $updatedValues);
212
213 6
        return $valueSql;
214 6
    }
215 4
216
    /**
217 6
     * This method returns column fields of a particular table.
218
     *
219 6
     * @param $table
220
     *
221
     * @return array
222
     */
223
    public function getColumnNames($table)
224
    {
225
        $tableFields = [];
226
227
        $sql = 'SHOW COLUMNS FROM '.$table;
228
        $stmt = $this->dbConnection->prepare($sql);
229 15
        $stmt->bindValue(':table', $table, PDO::PARAM_STR);
230
        $stmt->execute();
231 15
        $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
232
        
233 15
        foreach ($results as $result) {
234 15
            array_push($tableFields, $result['Field']);
235 15
        }
236 15
237 15
        return $tableFields;
238
    }
239
}
240