Skip to content

Commit

Permalink
Merge pull request #67 from WordPress/fix/cs
Browse files Browse the repository at this point in the history
Up the packages & fix CS issues
  • Loading branch information
aristath authored Dec 28, 2023
2 parents 8500a9c + 856349d commit 84c5d69
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 58 deletions.
2 changes: 1 addition & 1 deletion activate.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
function sqlite_plugin_activation_redirect( $plugin ) {
if ( plugin_basename( SQLITE_MAIN_FILE ) === $plugin ) {
if( wp_safe_redirect( admin_url( 'options-general.php?page=sqlite-integration' ) ) ) {
if ( wp_safe_redirect( admin_url( 'options-general.php?page=sqlite-integration' ) ) ) {
exit;
}
}
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
"squizlabs/php_codesniffer": "3.7.0",
"wp-coding-standards/wpcs": "~2.3.0",
"phpcompatibility/phpcompatibility-wp": "~2.1.3",
"squizlabs/php_codesniffer": "^3.7",
"wp-coding-standards/wpcs": "^3.0",
"phpcompatibility/phpcompatibility-wp": "^2.1",
"yoast/phpunit-polyfills": "^1.0.1"
},
"config": {
Expand Down
2 changes: 1 addition & 1 deletion deactivate.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function sqlite_plugin_remove_db_file() {
// Run an action on `shutdown`, to deactivate the option in the MySQL database.
add_action(
'shutdown',
function() {
function () {
global $table_prefix;

// Get credentials for the MySQL database.
Expand Down
2 changes: 1 addition & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

<rule ref="WordPress-Core"/>
<rule ref="WordPress-Docs"/>
<rule ref="WordPress.CodeAnalysis.EmptyStatement"/>

<!-- Directories and third party library exclusions. -->
<exclude-pattern>/vendor/*</exclude-pattern>
<exclude-pattern>/tests/*</exclude-pattern>

<!-- Allow the WP DB Class and related tests for usage of direct database access functions. -->
<rule ref="WordPress.DB.RestrictedClasses.mysql__PDO">
Expand Down
1 change: 0 additions & 1 deletion tests/WP_SQLite_Metadata_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,5 +318,4 @@ public function testBogusQuery() {
$actual
);
}

}
1 change: 0 additions & 1 deletion tests/WP_SQLite_PDO_User_Defined_Functions_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ function dataProviderForTestFieldFunction() {
array( 2, array( 'User 0000019', 'User 0000018', 'User 0000019', 'User 0000020' ) ),
);
}

}
1 change: 0 additions & 1 deletion tests/WP_SQLite_Query_RewriterTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,4 @@ public function skip_over() {
$this->assertEquals( ' ', $buffer[1]->value );
$this->assertEquals( 'UPDATE', $buffer[2]->value );
}

}
7 changes: 3 additions & 4 deletions tests/WP_SQLite_Query_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function setUp(): void {
}

/* Mock up some metadata rows. When meta_key starts with _, the custom field isn't visible to the editor. */
for ( $i = 1; $i <= 40; $i ++ ) {
for ( $i = 1; $i <= 40; $i++ ) {
$k1 = 'visible_meta_key_' . str_pad( $i, 2, '0', STR_PAD_LEFT );
$k2 = '_invisible_meta_key_%_percent' . str_pad( $i, 2, '0', STR_PAD_LEFT );
$this->assertQuery(
Expand Down Expand Up @@ -413,7 +413,7 @@ public function testDeleteExpiredNonSiteTransients() {
$count_unexpired = 0;
foreach ( $actual as $row ) {
if ( str_starts_with( $row->option_name, '_transient' ) ) {
$count_unexpired ++;
++$count_unexpired;
$this->assertGreaterThan( $now, $row->option_timeout );
}
}
Expand Down Expand Up @@ -478,7 +478,7 @@ public function testRecoverSerialized() {
$unserialized = unserialize( $retrieved_string );
$this->assertEquals( $obj, $unserialized );

$obj ['two'] ++;
++$obj ['two'];
$obj ['pi'] *= 2;
$option_value = serialize( $obj );
$option_value_escaped = $this->engine->get_pdo()->quote( $option_value );
Expand Down Expand Up @@ -533,5 +533,4 @@ private function assertQuery( $sql ) {

return $retval;
}

}
6 changes: 0 additions & 6 deletions tests/WP_SQLite_Translator_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,6 @@ public function testAlterTableModifyColumnComplexChange() {
$result = $this->engine->query( "INSERT INTO _tmp_table (ID, firstname, lastname, datetime) VALUES (6, 'Sophie', 'Bar', '2010-01-01 12:53:13');" );
$this->assertEquals( '', $this->engine->get_error_message() );
$this->assertEquals( 1, $result );

}

public function testCaseInsensitiveUniqueIndex() {
Expand Down Expand Up @@ -727,7 +726,6 @@ public function testCaseInsensitiveUniqueIndex() {

$result1 = $this->engine->query( 'SELECT COUNT(*) num FROM _tmp_table;' );
$this->assertEquals( 2, $result1[0]->num );

}

public function testOnDuplicateUpdate() {
Expand Down Expand Up @@ -966,7 +964,6 @@ public function testNestedTransactionWorkComplexModify() {
),
)
);

}

public function testCount() {
Expand Down Expand Up @@ -1462,7 +1459,6 @@ public function testStringToFloatComparison() {
$this->assertQuery( "SELECT (0+'00.42' = 0.4200) as cmp;" );
$results = $this->engine->get_query_results();
$this->assertEquals( '1', $results[0]->cmp );

}

public function testZeroPlusStringToFloatComparison() {
Expand All @@ -1474,7 +1470,6 @@ public function testZeroPlusStringToFloatComparison() {
$this->assertQuery( "SELECT 0+'1234abcd' = 1234 as cmp;" );
$results = $this->engine->get_query_results();
$this->assertEquals( '1', $results[0]->cmp );

}

public function testCalcFoundRows() {
Expand Down Expand Up @@ -1789,5 +1784,4 @@ public function testTranslatesUtf8SELECT() {

$this->assertQuery( 'DELETE FROM _options' );
}

}
1 change: 0 additions & 1 deletion tests/wp-sqlite-schema.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* WordPress schema for unit tests for the SQLite database integration project.
*
Expand Down
4 changes: 1 addition & 3 deletions wp-includes/sqlite/class-wp-sqlite-crosscheck-db.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php // phpcs:ignoreFile

class WP_SQLite_Crosscheck_DB_ extends WP_SQLite_DB {

Expand Down Expand Up @@ -117,13 +117,11 @@ private function crosscheck( $query, $sqlite_retval ) {
break;
}
}

}

private function report_factor( $factor, $mysql, $sqlite ) {
echo "$factor: \n";
echo ' MySQL: ' . var_export( $mysql, true ) . "\n";
echo ' SQLite: ' . var_export( $sqlite, true ) . "\n\n";
}

}
4 changes: 2 additions & 2 deletions wp-includes/sqlite/class-wp-sqlite-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function select( $db, $dbh = null ) {
*
* @return string escaped
*/
function _real_escape( $str ) {
public function _real_escape( $str ) {
return addslashes( $str );
}

Expand Down Expand Up @@ -278,7 +278,7 @@ public function query( $query ) {
}

$this->result = $this->dbh->query( $query );
$this->num_queries++;
++$this->num_queries;

if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
$this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function month( $field ) {
* From https://www.php.net/manual/en/datetime.format.php:
*
* n - Numeric representation of a month, without leading zeros.
* 1 through 12
* 1 through 12
*/
return intval( gmdate( 'n', strtotime( $field ) ) );
}
Expand Down Expand Up @@ -446,14 +446,14 @@ public function isnull( $field ) {
*
* As 'IF' is a reserved word for PHP, function name must be changed.
*
* @param mixed $expression the statement to be evaluated as true or false.
* @param mixed $true statement or value returned if $expression is true.
* @param mixed $false statement or value returned if $expression is false.
* @param mixed $expression The statement to be evaluated as true or false.
* @param mixed $truthy Statement or value returned if $expression is true.
* @param mixed $falsy Statement or value returned if $expression is false.
*
* @return mixed
*/
public function _if( $expression, $true, $false ) {
return ( true === $expression ) ? $true : $false;
public function _if( $expression, $truthy, $falsy ) {
return ( true === $expression ) ? $truthy : $falsy;
}

/**
Expand Down
57 changes: 33 additions & 24 deletions wp-includes/sqlite/class-wp-sqlite-translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ class WP_SQLite_Translator {
/**
* Variable to keep track of nested transactions level.
*
* @var number
* @var int
*/
private $transaction_level = 0;

Expand Down Expand Up @@ -335,6 +335,13 @@ class WP_SQLite_Translator {
*/
private $sqlite_system_tables = array();

/**
* The last error message from SQLite.
*
* @var string
*/
private $last_sqlite_error;

/**
* Constructor.
*
Expand Down Expand Up @@ -408,7 +415,8 @@ public function __construct( $pdo = null ) {

// WordPress happens to use no foreign keys.
$statement = $this->pdo->query( 'PRAGMA foreign_keys' );
if ( $statement->fetchColumn( 0 ) == '0' ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
// phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
if ( $statement->fetchColumn( 0 ) == '0' ) {
$this->pdo->query( 'PRAGMA foreign_keys = ON' );
}
$this->pdo->query( 'PRAGMA encoding="UTF-8";' );
Expand All @@ -422,7 +430,7 @@ public function __construct( $pdo = null ) {
*
* This definition is changed since version 1.7.
*/
function __destruct() {
public function __destruct() {
if ( defined( 'SQLITE_MEM_DEBUG' ) && SQLITE_MEM_DEBUG ) {
$max = ini_get( 'memory_limit' );
if ( is_null( $max ) ) {
Expand Down Expand Up @@ -756,9 +764,9 @@ public function get_return_value() {
* @throws Exception If the query is not supported.
*/
private function execute_mysql_query( $query ) {
$tokens = ( new WP_SQLite_Lexer( $query ) )->tokens;
$this->rewriter = new WP_SQLite_Query_Rewriter( $tokens );
$this->query_type = $this->rewriter->peek()->value;
$tokens = ( new WP_SQLite_Lexer( $query ) )->tokens;
$this->rewriter = new WP_SQLite_Query_Rewriter( $tokens );
$this->query_type = $this->rewriter->peek()->value;

switch ( $this->query_type ) {
case 'ALTER':
Expand Down Expand Up @@ -1392,7 +1400,8 @@ private function execute_select() {
$this->remember_last_reserved_keyword( $token );

if ( ! $table_name ) {
$this->table_name = $table_name = $this->peek_table_name( $token );
$this->table_name = $this->peek_table_name( $token );
$table_name = $this->peek_table_name( $token );
}

if ( $this->skip_sql_calc_found_rows( $token ) ) {
Expand Down Expand Up @@ -1484,7 +1493,7 @@ private function execute_truncate() {
private function execute_describe() {
$this->rewriter->skip();
$this->table_name = $this->rewriter->consume()->value;
$stmt = $this->execute_sqlite_query(
$stmt = $this->execute_sqlite_query(
"SELECT
`name` as `Field`,
(
Expand Down Expand Up @@ -1541,10 +1550,10 @@ private function execute_update() {
break;
}

// Record the table name
// Record the table name.
if (
!$this->table_name &&
!$token->matches(
! $this->table_name &&
! $token->matches(
WP_SQLite_Token::TYPE_KEYWORD,
WP_SQLite_Token::FLAG_KEYWORD_RESERVED
)
Expand Down Expand Up @@ -1664,7 +1673,7 @@ private function execute_insert_or_replace() {
if ( is_numeric( $this->last_insert_id ) ) {
$this->last_insert_id = (int) $this->last_insert_id;
}
$this->last_insert_id = apply_filters('sqlite_last_insert_id', $this->last_insert_id, $this->table_name);
$this->last_insert_id = apply_filters( 'sqlite_last_insert_id', $this->last_insert_id, $this->table_name );
}

/**
Expand Down Expand Up @@ -1753,7 +1762,7 @@ private function preprocess_like_expr( &$token ) {
/* Remove the quotes around the name. */
$unescaped_value = mb_substr( $token->token, 1, -1, 'UTF-8' );
if ( str_contains( $unescaped_value, '\_' ) || str_contains( $unescaped_value, '\%' ) ) {
$this->like_escape_count ++;
++$this->like_escape_count;
return str_replace(
array( '\_', '\%' ),
array( self::LIKE_ESCAPE_CHAR . '_', self::LIKE_ESCAPE_CHAR . '%' ),
Expand Down Expand Up @@ -2379,14 +2388,14 @@ private function translate_like_escape( $token ) {
} else {
/* open parenthesis during LIKE parameter, count it. */
if ( $token->matches( WP_SQLite_Token::TYPE_OPERATOR, null, array( '(' ) ) ) {
$this->like_expression_nesting ++;
++$this->like_expression_nesting;

return false;
}

/* close parenthesis matching open parenthesis during LIKE parameter, count it. */
if ( $this->like_expression_nesting > 1 && $token->matches( WP_SQLite_Token::TYPE_OPERATOR, null, array( ')' ) ) ) {
$this->like_expression_nesting --;
--$this->like_expression_nesting;

return false;
}
Expand Down Expand Up @@ -2664,7 +2673,7 @@ private function execute_alter() {
$op_type = strtoupper( $this->rewriter->consume()->token );
$op_subject = strtoupper( $this->rewriter->consume()->token );
$mysql_index_type = $this->normalize_mysql_index_type( $op_subject );
$is_index_op = ! ! $mysql_index_type;
$is_index_op = (bool) $mysql_index_type;

if ( 'ADD' === $op_type && 'COLUMN' === $op_subject ) {
$column_name = $this->rewriter->consume()->value;
Expand Down Expand Up @@ -3436,16 +3445,16 @@ private function handle_error( Exception $err ) {
* When $wpdb::suppress_errors is set to true or $wpdb::show_errors is set to false,
* the error messages are ignored.
*
* @param string $line Where the error occurred.
* @param string $function Indicate the function name where the error occurred.
* @param string $message The message.
* @param string $line Where the error occurred.
* @param string $function_name Indicate the function name where the error occurred.
* @param string $message The message.
*
* @return boolean|void
*/
private function set_error( $line, $function, $message ) {
private function set_error( $line, $function_name, $message ) {
$this->errors[] = array(
'line' => $line,
'function' => $function,
'function' => $function_name,
);
$this->error_messages[] = $message;
$this->is_error = true;
Expand Down Expand Up @@ -3640,7 +3649,7 @@ public function begin_transaction() {
*
* @since 0.1.0
*/
do_action( 'sqlite_transaction_query_executed', 'START TRANSACTION', !!$this->last_exec_returned, $this->transaction_level - 1 );
do_action( 'sqlite_transaction_query_executed', 'START TRANSACTION', (bool) $this->last_exec_returned, $this->transaction_level - 1 );
}
}
return $success;
Expand All @@ -3663,7 +3672,7 @@ public function commit() {
$this->execute_sqlite_query( 'RELEASE SAVEPOINT LEVEL' . $this->transaction_level );
}

do_action( 'sqlite_transaction_query_executed', 'COMMIT', !!$this->last_exec_returned, $this->transaction_level );
do_action( 'sqlite_transaction_query_executed', 'COMMIT', (bool) $this->last_exec_returned, $this->transaction_level );
return $this->last_exec_returned;
}

Expand All @@ -3683,7 +3692,7 @@ public function rollback() {
} else {
$this->execute_sqlite_query( 'ROLLBACK TO SAVEPOINT LEVEL' . $this->transaction_level );
}
do_action( 'sqlite_transaction_query_executed', 'ROLLBACK', !!$this->last_exec_returned, $this->transaction_level );
do_action( 'sqlite_transaction_query_executed', 'ROLLBACK', (bool) $this->last_exec_returned, $this->transaction_level );
return $this->last_exec_returned;
}
}
Loading

0 comments on commit 84c5d69

Please sign in to comment.