diff --git a/composer.json b/composer.json index 9d2b566a..766a0c10 100644 --- a/composer.json +++ b/composer.json @@ -15,11 +15,18 @@ "wp-cli/wp-cli": "^2.12" }, "require-dev": { + "wordpress/wordpress-importer": "^0.8", "wp-cli/entity-command": "^1.3 || ^2", "wp-cli/export-command": "^1 || ^2", "wp-cli/extension-command": "^1.2 || ^2", "wp-cli/wp-cli-tests": "^5" }, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/wordpress/wordpress-importer" + } + ], "config": { "process-timeout": 7200, "sort-packages": true, diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 00000000..48291156 --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,15 @@ +parameters: + level: 9 + paths: + - src + - import-command.php + scanDirectories: + - vendor/wp-cli/wp-cli/php + - vendor/wordpress/wordpress-importer/src + scanFiles: + - vendor/php-stubs/wordpress-stubs/wordpress-stubs.php + treatPhpDocTypesAsCertain: false + ignoreErrors: + - identifier: missingType.property + - identifier: missingType.parameter + - identifier: missingType.return diff --git a/src/Import_Command.php b/src/Import_Command.php index 83b9fee2..74378de3 100644 --- a/src/Import_Command.php +++ b/src/Import_Command.php @@ -2,6 +2,8 @@ class Import_Command extends WP_CLI_Command { + private $blog_users = array(); + public $processed_posts = array(); /** @@ -113,9 +115,6 @@ private function import_wxr( $file, $args ) { $wp_import = new WP_Import(); $wp_import->processed_posts = $this->processed_posts; $import_data = $wp_import->parse( $file ); - if ( is_wp_error( $import_data ) ) { - return $import_data; - } // Prepare the data to be used in process_author_mapping(); $wp_import->get_authors_from_import( $import_data ); @@ -147,6 +146,10 @@ private function import_wxr( $file, $args ) { $author_data[] = $author; } + /** + * @var array<\WP_User> $author_data + */ + // Build the author mapping $author_mapping = $this->process_author_mapping( $args['authors'], $author_data ); if ( is_wp_error( $author_mapping ) ) { @@ -256,6 +259,7 @@ function ( $post_id ) { } if ( 0 === ( $wpcli_import_counts['current_post'] % 500 ) ) { + // @phpstan-ignore function.deprecated WP_CLI\Utils\wp_clear_object_cache(); WP_CLI::log( '-- Cleared object cache.' ); } @@ -323,9 +327,9 @@ private function is_importer_available() { /** * Processes how the authors should be mapped * - * @param string $authors_arg The `--author` argument originally passed to command - * @param array $author_data An array of WP_User-esque author objects - * @return array|WP_Error $author_mapping Author mapping array if successful, WP_Error if something bad happened + * @param string $authors_arg The `--author` argument originally passed to command + * @param array<\WP_User> $author_data An array of WP_User-esque author objects + * @return array<\WP_User>|WP_Error Author mapping array if successful, WP_Error if something bad happened */ private function process_author_mapping( $authors_arg, $author_data ) { @@ -360,6 +364,9 @@ private function read_author_mapping_file( $file ) { $author_mapping = array(); foreach ( new \WP_CLI\Iterators\CSV( $file ) as $i => $author ) { + /** + * @var array $author + */ if ( ! array_key_exists( 'old_user_login', $author ) || ! array_key_exists( 'new_user_login', $author ) ) { return new WP_Error( 'invalid-author-mapping', "Author mapping file isn't properly formatted." ); } @@ -386,7 +393,15 @@ private function create_author_mapping_file( $file, $author_data ) { ); } $file_resource = fopen( $file, 'w' ); - \WP_CLI\utils\write_csv( $file_resource, $author_mapping, array( 'old_user_login', 'new_user_login' ) ); + + if ( ! $file_resource ) { + return new WP_Error( 'author-mapping-error', "Couldn't create author mapping file." ); + } + + // TODO: Fix $rows type upstream in write_csv() + // @phpstan-ignore argument.type + \WP_CLI\Utils\write_csv( $file_resource, $author_mapping, array( 'old_user_login', 'new_user_login' ) ); + return new WP_Error( 'author-mapping-error', sprintf( 'Please update author mapping file before continuing: %s', $file ) ); } else { 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 ) { /** * Creates users if they don't exist, and build an author mapping file. + * + * @param array<\WP_User> $author_data */ private function create_authors_for_mapping( $author_data ) { @@ -433,6 +450,9 @@ private function create_authors_for_mapping( $author_data ) { return $user_id; } + /** + * @var \WP_User $user + */ $user = get_user_by( 'id', $user_id ); $author_mapping[] = array( 'old_user_login' => $author->user_login, @@ -444,6 +464,8 @@ private function create_authors_for_mapping( $author_data ) { /** * Suggests a blog user based on the levenshtein distance. + * + * @return string|\WP_User */ private function suggest_user( $author_user_login, $author_user_email = '' ) {