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/BulkSql/BulkReplace.php (5 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
namespace League\Database\BulkSql;
4
5
use PDO;
6
use PDOStatement;
7
use League\Database\Traits\BulkSqlTrait;
8
use League\Database\Exceptions\LogicException;
9
10
class BulkReplace extends Base
11
{
12
    use BulkSqlTrait;
13
14
    private $primaryKey = null;
15
16
    private $uniqueKey = null;
17
18
    private $keysChecked = false;
19
20
    const QUERY_TEMPLATE = 'REPLACE INTO %s (%s) VALUES %s';
21
22 View Code Duplication
    protected function buildQuery(): string
0 ignored issues
show
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...
23
    {
24
        $this->resetFields();
25
        $this->resetPreparedItems();
26
27
        $this->checkIsKeysOnTheBeginning();
28
29
        $queryParams = $this->iterateOverItems($this->getPreparedItems(), function ($iteration) {
30
            $prepared = array_map(function ($field, $key) use ($iteration) {
31
                if (in_array($field, $this->getIgnoredColumn(), true)) {
32
                    return $this->getPreparedItems()[$iteration][$field] ?? $this->getPreparedItems()[$iteration][$key];
33
                }
34
35
                return ':'.$field.'_'.$iteration;
36
            }, $this->getFields(), array_keys($this->getFields()));
37
38
            return '('.implode(',', $prepared).')';
39
        });
40
41
        $fields = array_map(function ($field) {
42
            return "`{$field}`";
43
        }, $this->getFields());
44
45
        return sprintf(
46
            self::QUERY_TEMPLATE,
47
            $this->getTable(),
48
            implode(',', $fields),
49
            implode(',', $queryParams)
50
        );
51
    }
52
53 View Code Duplication
    protected function bindValues(PDOStatement $statement)
0 ignored issues
show
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...
54
    {
55
        $this->resetFields();
56
        $this->resetPreparedItems();
57
58
        $this->iterateOverItems($this->getPreparedItems(), function ($iteration) use ($statement) {
59
            foreach ($this->getFields() as $key => $field) {
60
                if (in_array($field, $this->getIgnoredColumn(), true)) {
61
                    return;
62
                }
63
                $value = $this->getPreparedItems()[$iteration][$field] ?? $this->getPreparedItems()[$iteration][$key];
64
                $statement->bindValue(':'.$field.'_'.$iteration, $value);
65
            }
66
        });
67
    }
68
69
    public function getReplacedCount()
70
    {
71
        // TODO: If affected same as total count, can be that all columns was replaced. Rethink this logic
72
        return $this->getAffectedCount() - $this->getTotalItemsCount();
73
    }
74
75
    public function getInsertedCount()
76
    {
77
        return $this->getTotalItemsCount() - $this->getReplacedCount();
78
    }
79
80
    /**
81
     * To replace work proper, need to check if PRIMARY KEY or UNIQUE KEY is in the beginning of array
82
     *
83
     * @throws LogicException
84
     */
85
    private function checkIsKeysOnTheBeginning()
86
    {
87
        if ($this->keysChecked) {
88
            return;
89
        }
90
91
        switch (true) {
92
            case $this->checkKeys($this->getPrimary()):
0 ignored issues
show
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
93
            case $this->checkKeys($this->getUnique()):
0 ignored issues
show
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
94
                break;
95
            default:
0 ignored issues
show
DEFAULT statements must be defined using a colon

As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.

switch ($expr) {
    default: { //wrong
        doSomething();
        break;
    }
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
96
                throw new LogicException('PRIMARY or UNIQUE KEYs should be at the beginning of fields set');
97
        }
98
99
        $this->keysChecked = true;
100
    }
101
102
    /**
103
     * @param array $keys
104
     *
105
     * @return bool     True if on the beginning, False if not
106
     */
107
    private function checkKeys(array $keys) : bool
108
    {
109
        $fields = $this->getFields();
110
        $compare = [];
111
        $count = count($keys);
112
113
        for ($i = 0; $i < $count; $i++) {
114
            $compare[] = array_shift($fields);
115
        }
116
117
        return !array_diff($keys, $compare);
118
    }
119
120
    /**
121
     * Return primary key of the table
122
     *
123
     * @return array
124
     */
125
    private function getPrimary() : array
126
    {
127
        return $this->primaryKey ?: $this->fetchPrimary();
128
    }
129
130
    /**
131
     * Return primary key of the table
132
     *
133
     * @return array
134
     */
135
    private function getUnique() : array
136
    {
137
        return $this->uniqueKey ?: $this->fetchUnique();
138
    }
139
140
    /**
141
     * Fetch primary key of the table
142
     *
143
     * @return array|string
144
     * @throws LogicException
145
     */
146
    private function fetchPrimary()
147
    {
148
        $query = $this->getDbConnection()->query('SHOW KEYS FROM '.$this->getTable().' WHERE Key_name = "PRIMARY"');
149
150
        $keys = array_map(function ($item) {
151
            return $item['Column_name'];
152
        }, $query->fetchAll(PDO::FETCH_ASSOC));
153
154
        if (!count($keys)) {
155
            throw new LogicException('Impossible to use REPLACE proper in Table, that doesn\'t have PRIMARY KEYS');
156
        }
157
158
        return $this->primaryKey = count($keys) == 1 ? $keys[0] : $keys;
159
    }
160
161
    /**
162
     * Fetch unique key of the table
163
     *
164
     * @return array|string
165
     * @throws LogicException
166
     */
167
    private function fetchUnique()
168
    {
169
        $query = $this->getDbConnection()->query(
170
            "SHOW KEYS FROM {$this->getTable()} WHERE Key_name != \"PRIMARY\" AND Non_unique = 0"
171
        );
172
173
        $keys = array_map(function ($item) {
174
            return $item['Column_name'];
175
        }, $query->fetchAll(PDO::FETCH_ASSOC));
176
177
        return $this->uniqueKey = count($keys) == 1 ? $keys[0] : $keys;
178
    }
179
}
180