Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions src/Import_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

class Import_Command extends WP_CLI_Command {

private $blog_users = array();
private $blog_users = [];

public $processed_posts = array();
public $processed_posts = [];

/**
* Imports content from a given WXR file.
Expand Down Expand Up @@ -43,7 +43,7 @@ class Import_Command extends WP_CLI_Command {
public function __invoke( $args, $assoc_args ) {
$defaults = array(
'authors' => null,
'skip' => array(),
'skip' => [],
'rewrite_urls' => null,
);
$assoc_args = wp_parse_args( $assoc_args, $defaults );
Expand All @@ -61,7 +61,7 @@ public function __invoke( $args, $assoc_args ) {

WP_CLI::log( 'Starting the import process...' );

$new_args = array();
$new_args = [];
foreach ( $args as $arg ) {
if ( is_dir( $arg ) ) {
$dir = WP_CLI\Utils\trailingslashit( $arg );
Expand Down Expand Up @@ -128,7 +128,10 @@ private function import_wxr( $file, $args ) {
// memory.
unset( $import_data );

$author_data = array();
/**
* @var array<object{user_email: string, user_login: string}> $author_data

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The PHPDoc type for $author_data is not entirely correct. The property user_email is optional, as it's only set if isset( $wxr_author['author_email'] ) in the loop below. The current type object{user_email: string, user_login: string} suggests user_email is always present, which can mislead static analysis tools and developers. Please mark it as optional.

 * @var array<object{user_login: string, user_email?: string}> $author_data

*/
$author_data = [];
foreach ( $wp_import->authors as $wxr_author ) {
$author = new \stdClass();
// Always in the WXR
Expand All @@ -152,7 +155,7 @@ private function import_wxr( $file, $args ) {
}

/**
* @var array<\WP_User> $author_data
* @var array<object{user_email: string, user_login: string}> $author_data
*/
Comment on lines 157 to 159

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This PHPDoc block for $author_data is redundant, as the variable is already documented at lines 131-133. Duplicate documentation can lead to inconsistencies and should be avoided. Please remove this block.

Comment on lines 157 to 159
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PHPDoc comment on lines 157-159 is redundant and adds no value. The variable $author_data already has the same type annotation on lines 131-133 just 23 lines above, and its type hasn't changed in between. This duplicate comment should be removed.

Copilot uses AI. Check for mistakes.

// Build the author mapping
Expand All @@ -166,8 +169,8 @@ private function import_wxr( $file, $args ) {
unset( $author_mapping, $author_data );

// $user_select needs to be an array of user IDs
$user_select = array();
$invalid_user_select = array();
$user_select = [];
$invalid_user_select = [];
foreach ( $author_out as $author_login ) {
$user = get_user_by( 'login', $author_login );
if ( $user ) {
Expand Down Expand Up @@ -351,7 +354,7 @@ 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<\WP_User> $author_data An array of WP_User-esque author objects
* @param array<\WP_User|object{user_email: string, user_login: string}> $author_data An array of WP_User-esque author objects

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The type hint for the $author_data parameter is incorrect. The user_email property on the object shape is optional, but it's not marked as such. This can cause issues with static analysis. Please mark user_email as optional.

 * @param array<\WP_User|object{user_login: string, user_email?: string}> $author_data An array of WP_User-esque author objects

Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type annotation is incomplete and inconsistent with lines 131-133. The objects can have optional properties display_name, first_name, and last_name in addition to user_login and user_email. The type should be array<\WP_User|object{user_login: string, user_email?: string, display_name?: string, first_name?: string, last_name?: string}> to accurately reflect all possible properties.

Suggested change
* @param array<\WP_User|object{user_email: string, user_login: string}> $author_data An array of WP_User-esque author objects
* @param array<\WP_User|object{user_login: string, user_email?: string, display_name?: string, first_name?: string, last_name?: string}> $author_data An array of WP_User-esque author objects

Copilot uses AI. Check for mistakes.
* @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 ) {
Expand All @@ -373,7 +376,7 @@ private function process_author_mapping( $authors_arg, $author_data ) {

// Skip any sort of author mapping
case 'skip':
return array();
return [];

default:
return new WP_Error( 'invalid-argument', "'authors' argument is invalid." );
Expand All @@ -384,7 +387,7 @@ private function process_author_mapping( $authors_arg, $author_data ) {
* Reads an author mapping file.
*/
private function read_author_mapping_file( $file ) {
$author_mapping = array();
$author_mapping = [];

foreach ( new \WP_CLI\Iterators\CSV( $file ) as $i => $author ) {
/**
Expand All @@ -408,7 +411,7 @@ private function read_author_mapping_file( $file ) {
private function create_author_mapping_file( $file, $author_data ) {

if ( touch( $file ) ) {
$author_mapping = array();
$author_mapping = [];
foreach ( $author_data as $author ) {
$author_mapping[] = array(
'old_user_login' => $author->user_login,
Comment on lines 416 to 417
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent array syntax. Lines 416, 450, 460, 467, and 483 in the file all use array( for multi-line array initialization, while line 414 in this same method uses []. For consistency with the pattern established in this PR (converting array() to []), these should also be converted.

Copilot uses AI. Check for mistakes.
Expand All @@ -434,11 +437,11 @@ 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
* @param array<\WP_User|object{user_email: string, user_login: string}> $author_data

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The type hint for the $author_data parameter is incorrect. The user_email property on the object shape is optional, but it's not marked as such. This is evident from the code within this function which checks isset( $author->user_email ). Please mark user_email as optional to reflect this.

 * @param array<\WP_User|object{user_login: string, user_email?: string}> $author_data

Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type annotation is incomplete and inconsistent with lines 131-133. The objects can have optional properties display_name, first_name, and last_name in addition to user_login and user_email. The type should be array<\WP_User|object{user_login: string, user_email?: string, display_name?: string, first_name?: string, last_name?: string}> to accurately reflect all possible properties.

Suggested change
* @param array<\WP_User|object{user_email: string, user_login: string}> $author_data
* @param array<\WP_User|object{user_login: string, user_email?: string, display_name?: string, first_name?: string, last_name?: string}> $author_data

Copilot uses AI. Check for mistakes.
*/
private function create_authors_for_mapping( $author_data ) {

$author_mapping = array();
$author_mapping = [];
foreach ( $author_data as $author ) {

if ( isset( $author->user_email ) ) {
Expand Down Expand Up @@ -497,7 +500,7 @@ private function suggest_user( $author_user_login, $author_user_email = '' ) {
}

$shortest = -1;
$shortestavg = array();
$shortestavg = [];

$threshold = floor( ( strlen( $author_user_login ) / 100 ) * 10 ); // 10 % of the strlen are valid
$closest = '';
Expand All @@ -507,7 +510,7 @@ private function suggest_user( $author_user_login, $author_user_email = '' ) {
return $user->user_login;
}

$levs = array();
$levs = [];
$levs[] = levenshtein( $author_user_login, $user->display_name );
$levs[] = levenshtein( $author_user_login, $user->user_login );
$levs[] = levenshtein( $author_user_login, $user->user_email );
Expand Down