Skip to content

Commit

Permalink
Fix/improve mysqli stubs
Browse files Browse the repository at this point in the history
* mysqli_commit $flags default value is 0, not -1.
* A number of functions cannot actually return null.
* mysqli_poll parameter names were incorrect, as this function
  has a different signature from select.
* fetch functions apart from fetch_all can return false on failure.
  • Loading branch information
kamil-tekiela authored and nikic committed Jan 6, 2021
1 parent 90081c5 commit 44a311d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 91 deletions.
52 changes: 26 additions & 26 deletions ext/mysqli/mysqli.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function begin_transaction(int $flags = 0, ?string $name = null) {}
public function change_user(string $username, string $password, ?string $database) {}

/**
* @return string|null
* @return string
* @alias mysqli_character_set_name
*/
public function character_set_name() {}
Expand All @@ -51,7 +51,7 @@ public function close() {}
* @return bool
* @alias mysqli_commit
*/
public function commit(int $flags = -1, ?string $name = null) {}
public function commit(int $flags = 0, ?string $name = null) {}

/**
* @return mysqli|null|false
Expand Down Expand Up @@ -101,7 +101,7 @@ public function get_connection_stats() {}
#endif

/**
* @return string|null
* @return string
* @alias mysqli_get_server_info
*/
public function get_server_info() {}
Expand Down Expand Up @@ -152,7 +152,7 @@ public function ping() {}
* @return int|false
* @alias mysqli_poll
*/
public static function poll(?array &$read, ?array &$write, array &$error, int $seconds, int $microseconds = 0) {}
public static function poll(?array &$read, ?array &$error, array &$reject, int $seconds, int $microseconds = 0) {}
#endif

/**
Expand Down Expand Up @@ -342,7 +342,7 @@ public function fetch_field_direct(int $index) {}

#if defined(MYSQLI_USE_MYSQLND)
/**
* @return array|false
* @return array
* @alias mysqli_fetch_all
*/
public function fetch_all(int $mode = MYSQLI_NUM) {}
Expand Down Expand Up @@ -392,7 +392,7 @@ class mysqli_stmt
public function __construct(mysqli $mysql, ?string $query = null) {}

/**
* @return int|false
* @return int
* @alias mysqli_stmt_attr_get
*/
public function attr_get(int $attribute) {}
Expand Down Expand Up @@ -529,11 +529,11 @@ function mysqli_begin_transaction(mysqli $mysql, int $flags = 0, ?string $name =

function mysqli_change_user(mysqli $mysql, string $username, string $password, ?string $database): bool {}

function mysqli_character_set_name(mysqli $mysql): ?string {}
function mysqli_character_set_name(mysqli $mysql): string {}

function mysqli_close(mysqli $mysql): bool {}

function mysqli_commit(mysqli $mysql, int $flags = -1, ?string $name = null): bool {}
function mysqli_commit(mysqli $mysql, int $flags = 0, ?string $name = null): bool {}

function mysqli_connect(
?string $hostname = null,
Expand All @@ -556,7 +556,7 @@ function mysqli_debug(string $options): bool {}

function mysqli_errno(mysqli $mysql): int {}

function mysqli_error(mysqli $mysql): ?string {}
function mysqli_error(mysqli $mysql): string {}

function mysqli_error_list(mysqli $mysql): array {}

Expand All @@ -574,16 +574,16 @@ function mysqli_fetch_field_direct(mysqli_result $result, int $index): object|fa
function mysqli_fetch_lengths(mysqli_result $result): array|false {}

#if defined(MYSQLI_USE_MYSQLND)
function mysqli_fetch_all(mysqli_result $result, int $mode = MYSQLI_NUM): array|false {}
function mysqli_fetch_all(mysqli_result $result, int $mode = MYSQLI_NUM): array {}
#endif

function mysqli_fetch_array(mysqli_result $result, int $mode = MYSQLI_BOTH): array|null|false {}

function mysqli_fetch_assoc(mysqli_result $result): ?array {}
function mysqli_fetch_assoc(mysqli_result $result): array|null|false {}

function mysqli_fetch_object(mysqli_result $result, string $class = "stdClass", array $constructor_args = []): ?object {}
function mysqli_fetch_object(mysqli_result $result, string $class = "stdClass", array $constructor_args = []): object|null|false {}

function mysqli_fetch_row(mysqli_result $result): ?array {}
function mysqli_fetch_row(mysqli_result $result): array|null|false {}

function mysqli_field_count(mysqli $mysql): int {}

Expand Down Expand Up @@ -638,10 +638,16 @@ function mysqli_num_rows(mysqli_result $result): int|string {}
/** @param string|int $value */
function mysqli_options(mysqli $mysql, int $option, $value): bool {}

/**
* @param string|int $value
* @alias mysqli_options
*/
function mysqli_set_opt(mysqli $mysql, int $option, $value): bool {}

function mysqli_ping(mysqli $mysql): bool {}

#if defined(MYSQLI_USE_MYSQLND)
function mysqli_poll(?array &$read, ?array &$write, array &$error, int $seconds, int $microseconds = 0): int|false {}
function mysqli_poll(?array &$read, ?array &$error, array &$reject, int $seconds, int $microseconds = 0): int|false {}
#endif

function mysqli_prepare(mysqli $mysql, string $query): mysqli_stmt|false {}
Expand All @@ -663,6 +669,9 @@ function mysqli_real_connect(

function mysqli_real_escape_string(mysqli $mysql, string $string): string {}

/** @alias mysqli_real_escape_string */
function mysqli_escape_string(mysqli $mysql, string $string): string {}

function mysqli_real_query(mysqli $mysql, string $query): bool {}

#if defined(MYSQLI_USE_MYSQLND)
Expand Down Expand Up @@ -695,7 +704,7 @@ function mysqli_stmt_data_seek(mysqli_stmt $statement, int $offset): void {}

function mysqli_stmt_errno(mysqli_stmt $statement): int {}

function mysqli_stmt_error(mysqli_stmt $statement): ?string {}
function mysqli_stmt_error(mysqli_stmt $statement): string {}

function mysqli_stmt_error_list(mysqli_stmt $statement): array {}

Expand Down Expand Up @@ -735,9 +744,9 @@ function mysqli_stmt_send_long_data(mysqli_stmt $statement, int $param_num, stri

function mysqli_stmt_store_result(mysqli_stmt $statement): bool {}

function mysqli_stmt_sqlstate(mysqli_stmt $statement): ?string {}
function mysqli_stmt_sqlstate(mysqli_stmt $statement): string {}

function mysqli_sqlstate(mysqli $mysql): ?string {}
function mysqli_sqlstate(mysqli $mysql): string {}

function mysqli_ssl_set(
mysqli $mysql,
Expand All @@ -761,12 +770,3 @@ function mysqli_use_result(mysqli $mysql): mysqli_result|false {}
function mysqli_warning_count(mysqli $mysql): int {}

function mysqli_refresh(mysqli $mysql, int $flags): bool {}

/** @alias mysqli_real_escape_string */
function mysqli_escape_string(mysqli $mysql, string $string): string {}

/**
* @param string|int $value
* @alias mysqli_options
*/
function mysqli_set_opt(mysqli $mysql, int $option, $value): bool {}
35 changes: 6 additions & 29 deletions ext/mysqli/mysqli_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -614,17 +614,13 @@ PHP_FUNCTION(mysqli_character_set_name)
{
MY_MYSQL *mysql;
zval *mysql_link;
const char *cs_name;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
RETURN_THROWS();
}

MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
cs_name = mysql_character_set_name(mysql->mysql);
if (cs_name) {
RETURN_STRING(cs_name);
}
RETURN_STRING(mysql_character_set_name(mysql->mysql));
}
/* }}} */

Expand Down Expand Up @@ -801,16 +797,12 @@ PHP_FUNCTION(mysqli_error)
{
MY_MYSQL *mysql;
zval *mysql_link;
const char *err;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
RETURN_THROWS();
}
MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
err = mysql_error(mysql->mysql);
if (err) {
RETURN_STRING(err);
}
RETURN_STRING(mysql_error(mysql->mysql));
}
/* }}} */

Expand Down Expand Up @@ -1337,10 +1329,7 @@ PHP_FUNCTION(mysqli_get_client_info)
}
}

const char * info = mysql_get_client_info();
if (info) {
RETURN_STRING(info);
}
RETURN_STRING(mysql_get_client_info());
}
/* }}} */

Expand Down Expand Up @@ -2151,16 +2140,12 @@ PHP_FUNCTION(mysqli_sqlstate)
{
MY_MYSQL *mysql;
zval *mysql_link;
const char *state;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
RETURN_THROWS();
}
MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
state = mysql_sqlstate(mysql->mysql);
if (state) {
RETURN_STRING(state);
}
RETURN_STRING(mysql_sqlstate(mysql->mysql));
}
/* }}} */

Expand Down Expand Up @@ -2360,17 +2345,13 @@ PHP_FUNCTION(mysqli_stmt_error)
{
MY_STMT *stmt;
zval *mysql_stmt;
const char * err;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
RETURN_THROWS();
}
MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_INITIALIZED);

err = mysql_stmt_error(stmt->stmt);
if (err) {
RETURN_STRING(err);
}
RETURN_STRING(mysql_stmt_error(stmt->stmt));
}
/* }}} */

Expand Down Expand Up @@ -2501,17 +2482,13 @@ PHP_FUNCTION(mysqli_stmt_sqlstate)
{
MY_STMT *stmt;
zval *mysql_stmt;
const char * state;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
RETURN_THROWS();
}
MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);

state = mysql_stmt_sqlstate(stmt->stmt);
if (state) {
RETURN_STRING(state);
}
RETURN_STRING(mysql_stmt_sqlstate(stmt->stmt));
}
/* }}} */

Expand Down

0 comments on commit 44a311d

Please sign in to comment.