- ALM used: GitHub
- CI system used (not sure)
- Languages of the repository: PHP
- Error observed:
Define a constant instead of duplicating this literal "SQL STATEMENT" n times.
- Steps to reproduce
Create a couple SQL Queries/Statements in PHP (later used with PDO) for example creating a bunch of tables.
You might end up with repeating TEXT NOT NULL
or whatever you have to pass as table definitions like this example PHP MySQL Create Table
Just repeat that to create several tables rather than one, each table having at least a couple NOT NULL
or whatever else statements you need for the tables.
Copy pasta code:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
anothername VARCHAR(30) NOT NULL,
morename VARCHAR(30) NOT NULL,
evenmore VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";
if ($conn->query($sql) === TRUE) {
echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
I do not consider it smart to create constants(! cluttering global space?) or variables just because I repeat that text in SQL Statements.
Do I miss something?
Or could SonarCloud be improved to not complain on such (I consider) false alarms?