Issues (1142)

public/php/lib/Db.php (14 issues)

1
<?php
2
/* Database utility functions */
3
namespace AL\Db;
4
5
/**
6
 * Get an error context message
7
 *
8
 * @param  $context Human friendly context message
0 ignored issues
show
The type AL\Db\Human was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
 * @return An error context string
0 ignored issues
show
The type AL\Db\An was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
 */
11
function get_error_context($context) {
12
    return "(Context: $context)";
0 ignored issues
show
Bug Best Practice introduced by
The expression return '(Context: '.$context.')' returns the type string which is incompatible with the documented return type AL\Db\An.
Loading history...
13
}
14
15
/**
16
 * Prepare a statement for the given query and parameters and handle errors
17
 *
18
 * @param  $context A human-friendly string giving the context the query is running
0 ignored issues
show
The type AL\Db\A was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
 *  in. This will be output in error messages if something goes wrong
20
 * @param  $mysqli Database connection
0 ignored issues
show
The type AL\Db\Database was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
 * @param  $query The SQL query to execute, using ? placeholders for parameters
22
 * @param  $bind_string The string representing the types of parameters to bind
0 ignored issues
show
The type AL\Db\The was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
 *  (See mysqli_bind_param()), e.g. "ss" for 2 strings. Pass NULL if the query
24
 *  doesn't have parameters
25
 * @param  $params The list of parameters to bind. Pass NULL if the query doesn't
26
 *  have parameters
27
 * @return A prepared statement that has been executed
28
 */
29
function execute_query($context, $mysqli, $query, $bind_string, ...$params) {
30
    $err_ctx = get_error_context($context);
31
32
    $stmt = $mysqli->prepare($query)
33
        or die("Error preparing query [$query] $err_ctx: ".$mysqli->error);
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
34
35
    if ($bind_string !== null) {
36
        $stmt->bind_param($bind_string, ...$params)
37
            or die("Error binding parameters $err_ctx");
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
38
    } elseif (strstr($query, "?")) {
39
        die("Error: The query [$query] contained parameters (?) but didn't have a bind string $err_ctx");
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
40
    }
41
42
    $stmt->execute()
43
        or die("Error executing statement for [$query] $err_ctx: ".$mysqli->error);
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
44
45
    return $stmt;
46
}
47
48
/**
49
 * Bind variables to a prepared statement and handle errors
50
 *
51
 * @param $context A human-friendly string giving the context the query is running
52
 *  in. This will be output in error messages if something goes wrong
53
 * @param $stmt Prepared statement to bind results on
0 ignored issues
show
The type AL\Db\Prepared was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
54
 * @param $params Variables to bind to the statement result
0 ignored issues
show
The type AL\Db\Variables was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
55
 */
56
function bind_result($context, $stmt, &...$params) {
57
    $err_ctx = get_error_context($context);
58
59
    $stmt->bind_result(...$params)
60
        or die("Error binding results $err_ctx");
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
61
}
62
63
/**
64
 * Assemble multiple constraints into a SQL string with the proper
65
 * WHERE / AND syntax depending on the number of constraints
66
 * @param $constraints A list of constraints, like "id = ?" or
67
 *  "name LIKE ?".
68
 * @return String A SQL string with constraints suitable
69
 *  to use in a SQL query
70
 */
71
function assemble_constraints($constraints) {
72
    $query = "";
73
    if (count($constraints) > 1) {
74
        $query .= " WHERE ".array_shift($constraints);
75
        $query .= " AND ".join($constraints, " AND ");
0 ignored issues
show
' AND ' of type string is incompatible with the type array|null expected by parameter $array of join(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
        $query .= " AND ".join($constraints, /** @scrutinizer ignore-type */ " AND ");
Loading history...
76
    } elseif (count($constraints) == 1) {
77
        $query .= " WHERE ".array_shift($constraints);
78
    }
79
80
    return $query;
81
}
82