diff --git a/.gitignore b/.gitignore index 9de4a71..f3b02d0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ logs -.phpunit.result.cache \ No newline at end of file +.phpunit.result.cache +.php-cs-fixer.cache \ No newline at end of file diff --git a/pg4wp/core.php b/pg4wp/core.php index 4f8f932..1f62efc 100644 --- a/pg4wp/core.php +++ b/pg4wp/core.php @@ -1,39 +1,39 @@ - '// define( ', - 'class wpdb' => 'class wpdb2', - 'new wpdb' => 'new wpdb2', - 'instanceof mysqli_result' => 'instanceof \PgSql\Result', - 'instanceof mysqli' => 'instanceof \PgSql\Connection', - '$this->dbh->connect_errno' => 'wpsqli_connect_error()', - 'mysqli_' => 'wpsqli_', - 'is_resource' => 'wpsqli_is_resource', - ' '', - '?>' => '', -); - -eval(str_replace(array_keys($replaces), array_values($replaces), file_get_contents(ABSPATH . '/wp-includes/class-wpdb.php'))); - -// Create wpdb object if not already done -if (!isset($wpdb) && defined('DB_USER')) { - $wpdb = new wpdb2(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); -} + '// define( ', + 'class wpdb' => 'class wpdb2', + 'new wpdb' => 'new wpdb2', + 'instanceof mysqli_result' => 'instanceof \PgSql\Result', + 'instanceof mysqli' => 'instanceof \PgSql\Connection', + '$this->dbh->connect_errno' => 'wpsqli_connect_error()', + 'mysqli_' => 'wpsqli_', + 'is_resource' => 'wpsqli_is_resource', + ' '', + '?>' => '', +); + +eval(str_replace(array_keys($replaces), array_values($replaces), file_get_contents(ABSPATH . '/wp-includes/class-wpdb.php'))); + +// Create wpdb object if not already done +if (!isset($wpdb) && defined('DB_USER')) { + $wpdb = new wpdb2(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); +} diff --git a/pg4wp/driver_mysql.php b/pg4wp/driver_mysql.php index 1a6233c..3ceceb2 100644 --- a/pg4wp/driver_mysql.php +++ b/pg4wp/driver_mysql.php @@ -178,8 +178,8 @@ function wpsqli_thread_id($connection) /** * Returns whether the client library is thread-safe. * - * This function is a wrapper for the mysqli_thread_safe function. It indicates whether the - * mysqli client library that PHP is using is thread-safe. This is important information when + * This function is a wrapper for the mysqli_thread_safe function. It indicates whether the + * mysqli client library that PHP is using is thread-safe. This is important information when * running PHP in a multi-threaded environment such as with the worker MPM in Apache or when * using multi-threading extensions in PHP. * @@ -246,9 +246,9 @@ function wpsqli_connect_errno() * Returns a string description of the last connect error. * * This function is a wrapper for the mysqli_connect_error function. It provides a textual description - * of the error from the last connection attempt made by mysqli_connect() or mysqli_real_connect(). + * of the error from the last connection attempt made by mysqli_connect() or mysqli_real_connect(). * Unlike mysqli_connect_errno(), which returns an error code, mysqli_connect_error() returns a string - * describing the error. This is useful for error handling, providing more detailed context about + * describing the error. This is useful for error handling, providing more detailed context about * connection problems. * * @return string|null A string that describes the error from the last connection attempt, or NULL @@ -594,14 +594,14 @@ function wpsqli_fetch_object($result, $class = "stdClass", $constructor_args = [ * Fetches one row of data from the result set and returns it as an enumerated array. * Each call to this function will retrieve the next row in the result set, so it's typically * used in a loop to process multiple rows. - * + * * This function is particularly useful when you need to retrieve a row as a simple array * where each column is accessed by an integer index starting at 0. It does not include * column names as keys, which can be marginally faster and less memory intensive than * associative arrays if the column names are not required. - * + * * @param mysqli_result $result The result set returned by a query against the database. - * + * * @return array|null Returns an enumerated array of strings representing the fetched row, * or NULL if there are no more rows in the result set. */ @@ -615,14 +615,14 @@ function wpsqli_fetch_row(mysqli_result $result): ?array * $result object. This function can be used in conjunction with mysqli_fetch_row(), * mysqli_fetch_assoc(), mysqli_fetch_array(), or mysqli_fetch_object() to navigate between * rows in result sets, especially when using buffered result sets. - * + * * This is an important function for situations where you need to access a specific row * directly without iterating over all preceding rows, which can be useful for pagination * or when looking up specific rows by row number. - * + * * @param mysqli_result $result The result set returned by a query against the database. * @param int $row_number The desired row number to seek to. Row numbers are zero-indexed. - * + * * @return bool Returns TRUE on success or FALSE on failure. If the row number is out of range, * it returns FALSE. */ @@ -983,4 +983,4 @@ function wpsqli_poll(&...$args) function wpsqli_reap_async_query($connection) { return mysqli_reap_async_query($connection); -} \ No newline at end of file +} diff --git a/pg4wp/driver_pgsql.php b/pg4wp/driver_pgsql.php index ee39f81..26be48c 100644 --- a/pg4wp/driver_pgsql.php +++ b/pg4wp/driver_pgsql.php @@ -31,7 +31,7 @@ $GLOBALS['pg4wp_conn'] = false; */ function wpsqli_init() { - return new class { + return new class () { public $sslkey; public $sslcert; public $sslca; @@ -48,7 +48,7 @@ function wpsqli_init() * password, database name, port number, socket, and flags, The flags parameter can be used to set different * connection options that can affect the behavior of the connection. * - * @param $connection dummy parameter just for compatibility with mysqli + * @param $connection dummy parameter just for compatibility with mysqli * @param string|null $hostname The host name or an IP address. * @param string|null $username The PostgreSQL user name. * @param string|null $password The password associated with the username. @@ -76,7 +76,7 @@ function wpsqli_real_connect(&$connection, $hostname = null, $username = null, $ if (!empty($password)) { $GLOBALS['pg4wp_connstr'] .= ' password=' . $password; - } + } // SSL parameters if (!empty($connection->sslkey)) { @@ -88,7 +88,7 @@ function wpsqli_real_connect(&$connection, $hostname = null, $username = null, $ } if (!empty($connection->sslca)) { - $GLOBALS['pg4wp_connstr'] .= ' sslrootcert=' . $connection->sslca; + $GLOBALS['pg4wp_connstr'] .= ' sslrootcert=' . $connection->sslca; } if (!empty($connection->sslcapath)) { @@ -157,7 +157,7 @@ function wpsqli_select_db(&$connection, $database) /** * Closes a previously opened database connection. * - * This function is a wrapper for the pg_close function. + * This function is a wrapper for the pg_close function. * // mysqli_close => pg_close (resource $connection): bool * It's important to close connections when they are no longer needed to free up resources on both the web * server and the PostgreSQL server. The function returns TRUE on success or FALSE on failure. @@ -174,7 +174,7 @@ function wpsqli_close(&$connection) /** * Used to establish secure connections using SSL. * - * This function sets up variables on the fake pg connection class which are used when + * This function sets up variables on the fake pg connection class which are used when * connecting to the postgres database with pg_connect * * @param PgSql\Connection $connection The pg connection resource. @@ -208,7 +208,7 @@ function wpsqli_ssl_set(&$connection, $key, $cert, $ca, $capath, $cipher) */ function wpsqli_get_client_info() { - // mysqli_get_client_info => No direct equivalent. + // mysqli_get_client_info => No direct equivalent. // Information can be derived from phpinfo() or phpversion(). return '8.0.35'; // Just want to fool wordpress ... } @@ -288,8 +288,8 @@ function wpsqli_thread_id(&$connection) /** * Returns whether the client library is thread-safe. * - * This function is a wrapper for the pg_thread_safe function. It indicates whether the - * pg client library that PHP is using is thread-safe. This is important information when + * This function is a wrapper for the pg_thread_safe function. It indicates whether the + * pg client library that PHP is using is thread-safe. This is important information when * running PHP in a multi-threaded environment such as with the worker MPM in Apache or when * using multi-threading extensions in PHP. * @@ -359,9 +359,9 @@ function wpsqli_connect_errno() * Returns a string description of the last connect error. * * This function is a wrapper for the pg_connect_error function. It provides a textual description - * of the error from the last connection attempt made by pg_connect() or pg_real_connect(). + * of the error from the last connection attempt made by pg_connect() or pg_real_connect(). * Unlike pg_connect_errno(), which returns an error code, pg_connect_error() returns a string - * describing the error. This is useful for error handling, providing more detailed context about + * describing the error. This is useful for error handling, providing more detailed context about * connection problems. * * @return string|null A string that describes the error from the last connection attempt, or NULL @@ -470,7 +470,7 @@ function wpsqli_rollback(&$connection, $flags = 0, $name = null) * * This function is a wrapper for the pg_query function. The pg_query function performs * a query against the database and returns a result set for successful SELECT queries, or TRUE - * for other successful DML queries such as INSERT, UPDATE, DELETE, etc. + * for other successful DML queries such as INSERT, UPDATE, DELETE, etc. * * @param PgSql\Connection $connection The pg connection resource. * @param string $query The SQL query to be executed. @@ -817,14 +817,14 @@ function wpsqli_fetch_object($result, $class = "stdClass", $constructor_args = [ * Fetches one row of data from the result set and returns it as an enumerated array. * Each call to this function will retrieve the next row in the result set, so it's typically * used in a loop to process multiple rows. - * + * * This function is particularly useful when you need to retrieve a row as a simple array * where each column is accessed by an integer index starting at 0. It does not include * column names as keys, which can be marginally faster and less memory intensive than * associative arrays if the column names are not required. - * + * * @param \PgSql\Result $result The result set returned by a query against the database. - * + * * @return array|null Returns an enumerated array of strings representing the fetched row, * or NULL if there are no more rows in the result set. */ @@ -838,14 +838,14 @@ function wpsqli_fetch_row($result): ?array * $result object. This function can be used in conjunction with pg_fetch_row(), * pg_fetch_assoc(), pg_fetch_array(), or pg_fetch_object() to navigate between * rows in result sets, especially when using buffered result sets. - * + * * This is an important function for situations where you need to access a specific row * directly without iterating over all preceding rows, which can be useful for pagination * or when looking up specific rows by row number. - * + * * @param \PgSql\Result $result The result set returned by a query against the database. * @param int $row_number The desired row number to seek to. Row numbers are zero-indexed. - * + * * @return bool Returns TRUE on success or FALSE on failure. If the row number is out of range, * it returns FALSE. */ diff --git a/pg4wp/rewriters/AlterTableSQLRewriter.php b/pg4wp/rewriters/AlterTableSQLRewriter.php index 563d4e7..0bd820a 100644 --- a/pg4wp/rewriters/AlterTableSQLRewriter.php +++ b/pg4wp/rewriters/AlterTableSQLRewriter.php @@ -58,7 +58,7 @@ class AlterTableSQLRewriter extends AbstractSQLRewriter private function rewriteChangeColumn(string $sql): string { $pattern = '/ALTER TABLE\s+(\w+)\s+CHANGE COLUMN\s+([^\s]+)\s+([^\s]+)\s+([^ ]+)( unsigned|)\s*(NOT NULL|)\s*(default (.+)|)/'; - + if(1 === preg_match($pattern, $sql, $matches)) { $table = $matches[1]; $col = $matches[2]; diff --git a/pg4wp/rewriters/CreateTableSQLRewriter.php b/pg4wp/rewriters/CreateTableSQLRewriter.php index fd31e4e..add5175 100644 --- a/pg4wp/rewriters/CreateTableSQLRewriter.php +++ b/pg4wp/rewriters/CreateTableSQLRewriter.php @@ -30,7 +30,7 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter // For flash-album-gallery plugin 'tinyint' => 'smallint' ]; - + public function rewrite(): string { $sql = $this->original(); diff --git a/pg4wp/rewriters/InsertSQLRewriter.php b/pg4wp/rewriters/InsertSQLRewriter.php index 81f8d74..fe753c7 100644 --- a/pg4wp/rewriters/InsertSQLRewriter.php +++ b/pg4wp/rewriters/InsertSQLRewriter.php @@ -99,7 +99,7 @@ class InsertSQLRewriter extends AbstractSQLRewriter } // Construct the PostgreSQL ON CONFLICT DO UPDATE section - $updateSection = implode(', ', array_map(fn($col) => "$col = EXCLUDED.$col", $updateCols)); + $updateSection = implode(', ', array_map(fn ($col) => "$col = EXCLUDED.$col", $updateCols)); // Construct the PostgreSQL query $postgresSQL = sprintf('%s %s ON CONFLICT (%s) DO UPDATE SET %s', $tableSection, $valuesSection, $primaryKey, $updateSection); diff --git a/tests/parseTest.php b/tests/parseTest.php index 128e65d..fe40a36 100644 --- a/tests/parseTest.php +++ b/tests/parseTest.php @@ -1,4 +1,6 @@ -assertSame($postgresql, $expected); } -} \ No newline at end of file + + protected function setUp(): void + { + global $wpdb; + $wpdb = new class () { + public $categories = "wp_categories"; + public $comments = "wp_comments"; + public $prefix = "wp_"; + public $options = "wp_options"; + }; + } +} diff --git a/tests/verifyAgainstStubsTest.php b/tests/verifyAgainstStubsTest.php index 1a568c8..35c56e4 100644 --- a/tests/verifyAgainstStubsTest.php +++ b/tests/verifyAgainstStubsTest.php @@ -1,4 +1,6 @@ -