Skip to content

Commit a4c86bc

Browse files
authored
Merge pull request #94 from wp-cli/add/phpstan
2 parents fd4481d + a50c132 commit a4c86bc

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

composer.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@
1515
"wp-cli/wp-cli": "^2.12"
1616
},
1717
"require-dev": {
18+
"wordpress/wordpress-importer": "^0.8",
1819
"wp-cli/entity-command": "^1.3 || ^2",
1920
"wp-cli/export-command": "^1 || ^2",
2021
"wp-cli/extension-command": "^1.2 || ^2",
2122
"wp-cli/wp-cli-tests": "^5"
2223
},
24+
"repositories": [
25+
{
26+
"type": "vcs",
27+
"url": "https://github.com/wordpress/wordpress-importer"
28+
}
29+
],
2330
"config": {
2431
"process-timeout": 7200,
2532
"sort-packages": true,

phpstan.neon.dist

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
parameters:
2+
level: 9
3+
paths:
4+
- src
5+
- import-command.php
6+
scanDirectories:
7+
- vendor/wp-cli/wp-cli/php
8+
- vendor/wordpress/wordpress-importer/src
9+
scanFiles:
10+
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
11+
treatPhpDocTypesAsCertain: false
12+
ignoreErrors:
13+
- identifier: missingType.property
14+
- identifier: missingType.parameter
15+
- identifier: missingType.return

src/Import_Command.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
class Import_Command extends WP_CLI_Command {
44

5+
private $blog_users = array();
6+
57
public $processed_posts = array();
68

79
/**
@@ -113,9 +115,6 @@ private function import_wxr( $file, $args ) {
113115
$wp_import = new WP_Import();
114116
$wp_import->processed_posts = $this->processed_posts;
115117
$import_data = $wp_import->parse( $file );
116-
if ( is_wp_error( $import_data ) ) {
117-
return $import_data;
118-
}
119118

120119
// Prepare the data to be used in process_author_mapping();
121120
$wp_import->get_authors_from_import( $import_data );
@@ -147,6 +146,10 @@ private function import_wxr( $file, $args ) {
147146
$author_data[] = $author;
148147
}
149148

149+
/**
150+
* @var array<\WP_User> $author_data
151+
*/
152+
150153
// Build the author mapping
151154
$author_mapping = $this->process_author_mapping( $args['authors'], $author_data );
152155
if ( is_wp_error( $author_mapping ) ) {
@@ -256,6 +259,7 @@ function ( $post_id ) {
256259
}
257260

258261
if ( 0 === ( $wpcli_import_counts['current_post'] % 500 ) ) {
262+
// @phpstan-ignore function.deprecated
259263
WP_CLI\Utils\wp_clear_object_cache();
260264
WP_CLI::log( '-- Cleared object cache.' );
261265
}
@@ -323,9 +327,9 @@ private function is_importer_available() {
323327
/**
324328
* Processes how the authors should be mapped
325329
*
326-
* @param string $authors_arg The `--author` argument originally passed to command
327-
* @param array $author_data An array of WP_User-esque author objects
328-
* @return array|WP_Error $author_mapping Author mapping array if successful, WP_Error if something bad happened
330+
* @param string $authors_arg The `--author` argument originally passed to command
331+
* @param array<\WP_User> $author_data An array of WP_User-esque author objects
332+
* @return array<\WP_User>|WP_Error Author mapping array if successful, WP_Error if something bad happened
329333
*/
330334
private function process_author_mapping( $authors_arg, $author_data ) {
331335

@@ -360,6 +364,9 @@ private function read_author_mapping_file( $file ) {
360364
$author_mapping = array();
361365

362366
foreach ( new \WP_CLI\Iterators\CSV( $file ) as $i => $author ) {
367+
/**
368+
* @var array<string, \WP_User> $author
369+
*/
363370
if ( ! array_key_exists( 'old_user_login', $author ) || ! array_key_exists( 'new_user_login', $author ) ) {
364371
return new WP_Error( 'invalid-author-mapping', "Author mapping file isn't properly formatted." );
365372
}
@@ -386,7 +393,15 @@ private function create_author_mapping_file( $file, $author_data ) {
386393
);
387394
}
388395
$file_resource = fopen( $file, 'w' );
389-
\WP_CLI\utils\write_csv( $file_resource, $author_mapping, array( 'old_user_login', 'new_user_login' ) );
396+
397+
if ( ! $file_resource ) {
398+
return new WP_Error( 'author-mapping-error', "Couldn't create author mapping file." );
399+
}
400+
401+
// TODO: Fix $rows type upstream in write_csv()
402+
// @phpstan-ignore argument.type
403+
\WP_CLI\Utils\write_csv( $file_resource, $author_mapping, array( 'old_user_login', 'new_user_login' ) );
404+
390405
return new WP_Error( 'author-mapping-error', sprintf( 'Please update author mapping file before continuing: %s', $file ) );
391406
} else {
392407
return new WP_Error( 'author-mapping-error', "Couldn't create author mapping file." );
@@ -395,6 +410,8 @@ private function create_author_mapping_file( $file, $author_data ) {
395410

396411
/**
397412
* Creates users if they don't exist, and build an author mapping file.
413+
*
414+
* @param array<\WP_User> $author_data
398415
*/
399416
private function create_authors_for_mapping( $author_data ) {
400417

@@ -433,6 +450,9 @@ private function create_authors_for_mapping( $author_data ) {
433450
return $user_id;
434451
}
435452

453+
/**
454+
* @var \WP_User $user
455+
*/
436456
$user = get_user_by( 'id', $user_id );
437457
$author_mapping[] = array(
438458
'old_user_login' => $author->user_login,
@@ -444,6 +464,8 @@ private function create_authors_for_mapping( $author_data ) {
444464

445465
/**
446466
* Suggests a blog user based on the levenshtein distance.
467+
*
468+
* @return string|\WP_User
447469
*/
448470
private function suggest_user( $author_user_login, $author_user_email = '' ) {
449471

0 commit comments

Comments
 (0)