Skip to content

Commit a89fe31

Browse files
committed
PHPStan fixes
1 parent 35a91ba commit a89fe31

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

src/DB_Command.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,8 @@ public function size( $args, $assoc_args ) {
12021202
// Get the database size.
12031203
if ( $is_sqlite ) {
12041204
$db_bytes = $this->sqlite_size();
1205-
$db_name = basename( $this->get_sqlite_db_path() );
1205+
$db_path = $this->get_sqlite_db_path();
1206+
$db_name = $db_path ? basename( $db_path ) : '';
12061207
} else {
12071208
$db_bytes = $wpdb->get_var(
12081209
$wpdb->prepare(

src/DB_Command_SQLite.php

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ protected function get_sqlite_pdo() {
105105
*/
106106
protected function sqlite_create() {
107107
$db_path = $this->get_sqlite_db_path();
108-
$db_dir = dirname( $db_path );
108+
if ( ! $db_path ) {
109+
WP_CLI::error( 'Could not determine the database path.' );
110+
}
111+
$db_dir = dirname( $db_path );
109112

110113
// Create directory if it doesn't exist.
111114
if ( ! is_dir( $db_dir ) ) {
@@ -139,6 +142,10 @@ protected function sqlite_create() {
139142
protected function sqlite_drop() {
140143
$db_path = $this->get_sqlite_db_path();
141144

145+
if ( ! $db_path ) {
146+
WP_CLI::error( 'Could not determine the database path.' );
147+
}
148+
142149
if ( ! file_exists( $db_path ) ) {
143150
WP_CLI::error( 'Database does not exist.' );
144151
}
@@ -156,6 +163,10 @@ protected function sqlite_drop() {
156163
protected function sqlite_reset() {
157164
$db_path = $this->get_sqlite_db_path();
158165

166+
if ( ! $db_path ) {
167+
WP_CLI::error( 'Could not determine the database path.' );
168+
}
169+
159170
// Delete if exists.
160171
if ( file_exists( $db_path ) ) {
161172
if ( ! unlink( $db_path ) ) {
@@ -208,6 +219,12 @@ protected function sqlite_query( $query ) {
208219
} else {
209220
$stmt = $pdo->query( $query );
210221

222+
if ( ! $stmt ) {
223+
// There was an error.
224+
$error_info = $pdo->errorInfo();
225+
WP_CLI::error( 'Query failed: ' . $error_info[2] );
226+
}
227+
211228
// Fetch and display results.
212229
$results = $stmt->fetchAll( PDO::FETCH_ASSOC );
213230

@@ -278,6 +295,10 @@ protected function display_table( $headers, $rows ) {
278295
protected function sqlite_export( $file, $assoc_args ) {
279296
$db_path = $this->get_sqlite_db_path();
280297

298+
if ( ! $db_path ) {
299+
WP_CLI::error( 'Could not determine the database path.' );
300+
}
301+
281302
if ( ! file_exists( $db_path ) ) {
282303
WP_CLI::error( 'Database does not exist.' );
283304
}
@@ -293,9 +314,10 @@ protected function sqlite_export( $file, $assoc_args ) {
293314
$output = fopen( 'php://stdout', 'w' );
294315
} else {
295316
$output = fopen( $file, 'w' );
296-
if ( ! $output ) {
297-
WP_CLI::error( "Could not open file for writing: {$file}" );
298-
}
317+
}
318+
319+
if ( ! $output ) {
320+
WP_CLI::error( "Could not open file for writing: {$file}" );
299321
}
300322

301323
try {
@@ -304,14 +326,26 @@ protected function sqlite_export( $file, $assoc_args ) {
304326
fwrite( $output, '-- Database: ' . basename( $db_path ) . "\n\n" );
305327

306328
// Get all tables.
307-
$tables = $pdo->query( "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name" )->fetchAll( PDO::FETCH_COLUMN );
329+
$stmt = $pdo->query( "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name" );
330+
if ( ! $stmt ) {
331+
// There was an error.
332+
$error_info = $pdo->errorInfo();
333+
WP_CLI::error( 'Could not retrieve table list: ' . $error_info[2] );
334+
}
335+
$tables = $stmt->fetchAll( PDO::FETCH_COLUMN );
308336

309337
foreach ( $tables as $table ) {
310338
// Escape table name for identifiers.
311339
$escaped_table = '"' . str_replace( '"', '""', $table ) . '"';
312340

313341
// Get CREATE TABLE statement.
314-
$create_stmt = $pdo->query( "SELECT sql FROM sqlite_master WHERE type='table' AND name=" . $pdo->quote( $table ) )->fetchColumn();
342+
$stmt = $pdo->query( "SELECT sql FROM sqlite_master WHERE type='table' AND name=" . $pdo->quote( $table ) );
343+
if ( ! $stmt ) {
344+
// There was an error.
345+
$error_info = $pdo->errorInfo();
346+
WP_CLI::error( "Could not retrieve CREATE TABLE statement for table {$escaped_table}: " . $error_info[2] );
347+
}
348+
$create_stmt = $stmt->fetchColumn();
315349

316350
if ( isset( $assoc_args['add-drop-table'] ) ) {
317351
fwrite( $output, "DROP TABLE IF EXISTS {$escaped_table};\n" );
@@ -320,7 +354,13 @@ protected function sqlite_export( $file, $assoc_args ) {
320354
fwrite( $output, $create_stmt . ";\n\n" );
321355

322356
// Export data.
323-
$rows = $pdo->query( "SELECT * FROM {$escaped_table}" )->fetchAll( PDO::FETCH_ASSOC );
357+
$stmt = $pdo->query( "SELECT * FROM {$escaped_table}" );
358+
if ( ! $stmt ) {
359+
// There was an error.
360+
$error_info = $pdo->errorInfo();
361+
WP_CLI::error( "Could not retrieve data for table {$escaped_table}: " . $error_info[2] );
362+
}
363+
$rows = $stmt->fetchAll( PDO::FETCH_ASSOC );
324364

325365
foreach ( $rows as $row ) {
326366
$columns = array_keys( $row );
@@ -374,10 +414,14 @@ protected function sqlite_import( $file ) {
374414

375415
try {
376416
// Split SQL into individual statements.
417+
$lines = preg_split( '/;[\r\n]+/', $sql );
418+
if ( ! is_array( $lines ) ) {
419+
$lines = [];
420+
}
377421
$statements = array_filter(
378422
array_map(
379423
'trim',
380-
preg_split( '/;[\r\n]+/', $sql )
424+
$lines
381425
)
382426
);
383427

@@ -408,11 +452,16 @@ protected function sqlite_import( $file ) {
408452
protected function sqlite_size() {
409453
$db_path = $this->get_sqlite_db_path();
410454

411-
if ( ! file_exists( $db_path ) ) {
455+
if ( ! $db_path || ! file_exists( $db_path ) ) {
456+
return 0;
457+
}
458+
459+
$size = filesize( $db_path );
460+
if ( false === $size ) {
412461
return 0;
413462
}
414463

415-
return filesize( $db_path );
464+
return $size;
416465
}
417466

418467
/**

0 commit comments

Comments
 (0)