style fixes

This commit is contained in:
mattbucci
2024-01-07 23:07:50 +00:00
parent 021aa6d106
commit 94259e408b
10 changed files with 99 additions and 81 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
logs
.phpunit.result.cache
.phpunit.result.cache
.php-cs-fixer.cache

View File

@ -1,39 +1,39 @@
<?php
/**
* @package PostgreSQL_For_Wordpress
* @version $Id$
* @author Hawk__, www.hawkix.net
*/
/**
* This file does all the initialisation tasks
*/
// This is required by class-wpdb so we must load it first
require_once ABSPATH . '/wp-includes/version.php';
require_once ABSPATH . '/wp-includes/cache.php';
require_once ABSPATH . '/wp-includes/l10n.php';
// Load the driver defined in 'db.php'
require_once(PG4WP_ROOT . '/driver_' . DB_DRIVER . '.php');
// This loads up the wpdb class applying appropriate changes to it
$replaces = array(
'define( ' => '// 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',
'<?php' => '',
'?>' => '',
);
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);
}
<?php
/**
* @package PostgreSQL_For_Wordpress
* @version $Id$
* @author Hawk__, www.hawkix.net
*/
/**
* This file does all the initialisation tasks
*/
// This is required by class-wpdb so we must load it first
require_once ABSPATH . '/wp-includes/version.php';
require_once ABSPATH . '/wp-includes/cache.php';
require_once ABSPATH . '/wp-includes/l10n.php';
// Load the driver defined in 'db.php'
require_once(PG4WP_ROOT . '/driver_' . DB_DRIVER . '.php');
// This loads up the wpdb class applying appropriate changes to it
$replaces = array(
'define( ' => '// 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',
'<?php' => '',
'?>' => '',
);
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);
}

View File

@ -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);
}
}

View File

@ -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.
*/

View File

@ -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];

View File

@ -30,7 +30,7 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
// For flash-album-gallery plugin
'tinyint' => 'smallint'
];
public function rewrite(): string
{
$sql = $this->original();

View File

@ -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);

View File

@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
if (!defined('ABSPATH')) {
@ -35,10 +37,10 @@ final class parseTest extends TestCase
protected function setUp(): void
{
global $wpdb;
$wpdb = new class() {
$wpdb = new class () {
public $options = "wp_options";
public $categories = "wp_categories";
};
}
}
}

View File

@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
if (!defined('ABSPATH')) {
@ -31,4 +33,15 @@ final class rewriteTest extends TestCase
$postgresql = pg4wp_rewrite($sql);
$this->assertSame($postgresql, $expected);
}
}
protected function setUp(): void
{
global $wpdb;
$wpdb = new class () {
public $categories = "wp_categories";
public $comments = "wp_comments";
public $prefix = "wp_";
public $options = "wp_options";
};
}
}

View File

@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
if (!defined('ABSPATH')) {
@ -13,7 +15,7 @@ require_once __DIR__ . "/../pg4wp/db.php";
final class verifyAgainstStubsTest extends TestCase
{
const STUBS_DIRECTORY = __DIR__ . '/stubs';
public const STUBS_DIRECTORY = __DIR__ . '/stubs';
public function test_verify_against_stubs()
{
@ -27,11 +29,11 @@ final class verifyAgainstStubsTest extends TestCase
protected function setUp(): void
{
global $wpdb;
$wpdb = new class() {
$wpdb = new class () {
public $categories = "wp_categories";
public $comments = "wp_comments";
public $prefix = "wp_";
public $options = "wp_options";
};
}
}
}