Skip to content
12 changes: 9 additions & 3 deletions functions.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<?php

// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Renaming breaks Phar compat.
function wp_export( $args = array() ) {
$defaults = array(
/**
* @param array{filters?: array<mixed>, format?: class-string<WP_Export_WXR_Formatter>, writer?: class-string<WP_Export_Returner>, writer_args?: mixed} $args
*/
function wp_export( $args = array() ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Renaming breaks Phar compat.
$defaults = array(
'filters' => array(),
'format' => 'WP_Export_WXR_Formatter',
'writer' => 'WP_Export_Returner',
'writer_args' => null,
);

/**
* @var array{filters: array<mixed>, format: class-string<WP_Export_WXR_Formatter>, writer: class-string<WP_Export_Returner>, writer_args: mixed} $args
*/
$args = wp_parse_args( $args, $defaults );
$export_query = new WP_Export_Query( $args['filters'] );
$formatter = new $args['format']( $export_query );
Expand Down
16 changes: 16 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
parameters:
level: 9
paths:
- src
- export-command.php
- functions.php
scanDirectories:
- vendor/wp-cli/wp-cli/php
scanFiles:
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
treatPhpDocTypesAsCertain: false
ignoreErrors:
- identifier: missingType.iterableValue
- identifier: missingType.property
- identifier: missingType.parameter
- identifier: missingType.return
98 changes: 86 additions & 12 deletions src/Export_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,24 +181,24 @@
'wp_export_new_file',
static function ( $file_path ) {
WP_CLI::log( sprintf( 'Writing to file %s', $file_path ) );
Utils\wp_clear_object_cache();
Utils\wp_clear_object_cache(); // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.wp_clear_object_cacheDeprecatedRemoved @phpstan-ignore-line
}
);

try {
if ( $this->stdout ) {
wp_export(
[

Check failure on line 191 in src/Export_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Parameter #1 $args of function wp_export expects array{filters?: array, format?: class-string<WP_Export_WXR_Formatter>, writer?: class-string<WP_Export_Returner>, writer_args?: mixed}, array{filters: non-empty-array, writer: 'WP_Export_File_Writer', writer_args: 'php://output'} given.
'filters' => $this->export_args,
'writer' => 'WP_Export_File_Writer',
'writer' => WP_Export_File_Writer::class, // @phpstan-ignore argument.type

Check failure on line 193 in src/Export_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

No error with identifier argument.type is reported on line 193.
'writer_args' => 'php://output',
]
);
} else {
wp_export(
[

Check failure on line 199 in src/Export_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Parameter #1 $args of function wp_export expects array{filters?: array, format?: class-string<WP_Export_WXR_Formatter>, writer?: class-string<WP_Export_Returner>, writer_args?: mixed}, array{filters: non-empty-array, writer: 'WP_Export_Split_Files_Writer', writer_args: array{max_file_size: mixed, destination_directory: mixed, filename_template: mixed, include_once: mixed}} given.
'filters' => $this->export_args,
'writer' => 'WP_Export_Split_Files_Writer',
'writer' => WP_Export_Split_Files_Writer::class, // @phpstan-ignore argument.type

Check failure on line 201 in src/Export_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

No error with identifier argument.type is reported on line 201.
'writer_args' => [
'max_file_size' => $this->max_file_size,
'destination_directory' => $this->wxr_path,
Expand Down Expand Up @@ -234,6 +234,7 @@

foreach ( $args as $key => $value ) {
if ( is_callable( [ $this, 'check_' . $key ] ) ) {
/** @phpstan-ignore argument.type */
$result = call_user_func( [ $this, 'check_' . $key ], $value );
if ( false === $result ) {
$has_errors = true;
Expand All @@ -251,9 +252,14 @@
}
}

/**
* @param string $path
*
* @phpstan-ignore method.unused
*/
private function check_dir( $path ) {
if ( empty( $path ) ) {
$path = getcwd();
$path = (string) getcwd();
} elseif ( ! is_dir( $path ) ) {
WP_CLI::error( sprintf( "The directory '%s' does not exist.", $path ) );
} elseif ( ! is_writable( $path ) ) {
Expand All @@ -265,34 +271,49 @@
return true;
}

/**
* @param string $date
*
* @phpstan-ignore method.unused
*/
private function check_start_date( $date ) {
if ( null === $date ) {
return true;
}

$time = strtotime( $date );
if ( ! empty( $date ) && ! $time ) {
if ( ! empty( $date ) && false === $time ) {
WP_CLI::warning( sprintf( 'The start_date %s is invalid.', $date ) );
return false;
}
$this->export_args['start_date'] = date( 'Y-m-d', $time ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
$this->export_args['start_date'] = date( 'Y-m-d', (int) $time ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
return true;
}

/**
* @param string $date
*
* @phpstan-ignore method.unused
*/
private function check_end_date( $date ) {
if ( null === $date ) {
return true;
}

$time = strtotime( $date );
if ( ! empty( $date ) && ! $time ) {
if ( ! empty( $date ) && false === $time ) {
WP_CLI::warning( sprintf( 'The end_date %s is invalid.', $date ) );
return false;
}
$this->export_args['end_date'] = date( 'Y-m-d', $time ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
$this->export_args['end_date'] = date( 'Y-m-d', (int) $time ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
return true;
}

/**
* @param string $post_type
*
* @phpstan-ignore method.unused
*/
private function check_post_type( $post_type ) {
if ( null === $post_type || 'any' === $post_type ) {
return true;
Expand All @@ -317,6 +338,11 @@
return true;
}

/**
* @param string $post_type
*
* @phpstan-ignore method.unused
*/
private function check_post_type__not_in( $post_type ) {
if ( null === $post_type ) {
return true;
Expand All @@ -341,6 +367,11 @@
return true;
}

/**
* @param string $post__in
*
* @phpstan-ignore method.unused
*/
private function check_post__in( $post__in ) {
if ( null === $post__in ) {
return true;
Expand All @@ -357,6 +388,11 @@
return true;
}

/**
* @param string $start_id
*
* @phpstan-ignore method.unused
*/
private function check_start_id( $start_id ) {
if ( null === $start_id ) {
return true;
Expand All @@ -374,14 +410,19 @@
return true;
}

/**
* @param string $author
*
* @phpstan-ignore method.unused
*/
private function check_author( $author ) {
if ( null === $author ) {
return true;
}

// phpcs:ignore WordPress.WP.DeprecatedFunctions.get_users_of_blogFound -- Fallback.
$authors = function_exists( 'get_users' ) ? get_users() : get_users_of_blog();
if ( empty( $authors ) || is_wp_error( $authors ) ) {
$authors = function_exists( 'get_users' ) ? get_users() : get_users_of_blog(); // @phpstan-ignore-line
if ( empty( $authors ) ) {
WP_CLI::warning( 'Could not find any authors in this blog.' );
return false;
}
Expand All @@ -407,6 +448,9 @@
return true;
}

/**
* @phpstan-ignore method.unused
*/
private function check_max_num_posts( $num ) {
if ( null !== $num && ( ! is_numeric( $num ) || $num <= 0 ) ) {
WP_CLI::warning( 'max_num_posts should be a positive integer.' );
Expand All @@ -418,6 +462,11 @@
return true;
}

/**
* @param string $category
*
* @phpstan-ignore method.unused
*/
private function check_category( $category ) {
if ( null === $category ) {
return true;
Expand All @@ -428,21 +477,26 @@
}

$term = category_exists( $category );
if ( empty( $term ) || is_wp_error( $term ) ) {
if ( empty( $term ) ) {
WP_CLI::warning( sprintf( 'Could not find a category matching %s.', $category ) );
return false;
}
$this->export_args['category'] = $category;
return true;
}

/**
* @param string $status
*
* @phpstan-ignore method.unused
*/
private function check_post_status( $status ) {
if ( null === $status ) {
return true;
}

$stati = get_post_stati();
if ( empty( $stati ) || is_wp_error( $stati ) ) {
if ( empty( $stati ) ) {
WP_CLI::warning( 'Could not find any post stati.' );
return false;
}
Expand All @@ -455,6 +509,11 @@
return true;
}

/**
* @param string|null $skip
*
* @phpstan-ignore method.unused
*/
private function check_skip_comments( $skip ) {
if ( null === $skip ) {
return true;
Expand All @@ -468,6 +527,11 @@
return true;
}

/**
* @param string $size
*
* @phpstan-ignore method.unused
*/
private function check_max_file_size( $size ) {
if ( ! is_numeric( $size ) ) {
WP_CLI::warning( 'max_file_size should be numeric.' );
Expand All @@ -479,6 +543,11 @@
return true;
}

/**
* @param string $once
*
* @phpstan-ignore method.unused
*/
private function check_include_once( $once ) {
if ( null === $once ) {
return true;
Expand All @@ -497,6 +566,11 @@
return true;
}

/**
* @param string $allow_orphan_terms
*
* @phpstan-ignore method.unused
*/
private function check_allow_orphan_terms( $allow_orphan_terms ) {
if ( null === $allow_orphan_terms ) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/WP_Export_Oxymel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function optional( $tag_name, $contents ) {

public function optional_cdata( $tag_name, $contents ) {
if ( $contents ) {
$this->$tag_name->contains->cdata( $contents )->end;
$this->$tag_name->contains->cdata( $contents )->end; // @phpstan-ignore-line
}
return $this;
}
Expand Down
8 changes: 4 additions & 4 deletions src/WP_Export_Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function custom_taxonomies_terms() {
}
$custom_taxonomies = get_taxonomies( [ '_builtin' => false ] );
// phpcs:ignore WordPress.WP.DeprecatedParameters.Get_termsParam2Found -- Deprecated, but we need to support older versions of WordPress.
$custom_terms = (array) get_terms( $custom_taxonomies, [ 'get' => 'all' ] );
$custom_terms = (array) get_terms( $custom_taxonomies, [ 'get' => 'all' ] ); // @phpstan-ignore-line
$custom_terms = $this->process_orphaned_terms( $custom_terms );
$custom_terms = self::topologically_sort_terms( $custom_terms );
return $custom_terms;
Expand Down Expand Up @@ -323,7 +323,7 @@ private function bloginfo_rss( $section ) {

private function find_user_from_any_object( $user ) {
if ( is_numeric( $user ) ) {
return get_user_by( 'id', $user );
return get_user_by( 'id', (int) $user );
} elseif ( is_string( $user ) ) {
return get_user_by( 'login', $user );
} elseif ( isset( $user->ID ) ) {
Expand All @@ -334,10 +334,10 @@ private function find_user_from_any_object( $user ) {

private function find_category_from_any_object( $category ) {
if ( is_numeric( $category ) ) {
return get_term( $category, 'category' );
return get_term( (int) $category, 'category' );
} elseif ( is_string( $category ) ) {
$term = term_exists( $category, 'category' );
return isset( $term['term_id'] ) ? get_term( $term['term_id'], 'category' ) : false;
return isset( $term['term_id'] ) ? get_term( (int) $term['term_id'], 'category' ) : false;
} elseif ( isset( $category->term_id ) ) {
return get_term( $category->term_id, 'category' );
}
Expand Down
1 change: 0 additions & 1 deletion src/WP_Export_Returner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class WP_Export_Returner extends WP_Export_Base_Writer {
private $result = '';

public function export() {
$this->private = '';
try {
parent::export();
} catch ( WP_Export_Exception $e ) {
Expand Down
Loading
Loading