diff --git a/functions.php b/functions.php index 8716c197..a7fe5c8c 100644 --- a/functions.php +++ b/functions.php @@ -1,13 +1,19 @@ , format?: class-string, writer?: class-string, 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, format: class-string, writer: class-string, writer_args: mixed} $args + */ $args = wp_parse_args( $args, $defaults ); $export_query = new WP_Export_Query( $args['filters'] ); $formatter = new $args['format']( $export_query ); diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 00000000..25183173 --- /dev/null +++ b/phpstan.neon.dist @@ -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 diff --git a/src/Export_Command.php b/src/Export_Command.php index 273f9d33..96ffbbb5 100644 --- a/src/Export_Command.php +++ b/src/Export_Command.php @@ -181,7 +181,7 @@ public function __invoke( $_, $assoc_args ) { '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 } ); @@ -190,7 +190,7 @@ static function ( $file_path ) { wp_export( [ 'filters' => $this->export_args, - 'writer' => 'WP_Export_File_Writer', + 'writer' => WP_Export_File_Writer::class, // @phpstan-ignore argument.type 'writer_args' => 'php://output', ] ); @@ -198,7 +198,7 @@ static function ( $file_path ) { wp_export( [ 'filters' => $this->export_args, - 'writer' => 'WP_Export_Split_Files_Writer', + 'writer' => WP_Export_Split_Files_Writer::class, // @phpstan-ignore argument.type 'writer_args' => [ 'max_file_size' => $this->max_file_size, 'destination_directory' => $this->wxr_path, @@ -234,6 +234,7 @@ private function validate_args( $args ) { 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; @@ -251,9 +252,14 @@ private function validate_args( $args ) { } } + /** + * @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 ) ) { @@ -265,34 +271,49 @@ private function check_dir( $path ) { 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; @@ -317,6 +338,11 @@ private function check_post_type( $post_type ) { 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; @@ -341,6 +367,11 @@ private function check_post_type__not_in( $post_type ) { return true; } + /** + * @param string $post__in + * + * @phpstan-ignore method.unused + */ private function check_post__in( $post__in ) { if ( null === $post__in ) { return true; @@ -357,6 +388,11 @@ private function check_post__in( $post__in ) { return true; } + /** + * @param string $start_id + * + * @phpstan-ignore method.unused + */ private function check_start_id( $start_id ) { if ( null === $start_id ) { return true; @@ -374,14 +410,19 @@ private function check_start_id( $start_id ) { 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; } @@ -407,6 +448,9 @@ private function check_author( $author ) { 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.' ); @@ -418,6 +462,11 @@ private function check_max_num_posts( $num ) { return true; } + /** + * @param string $category + * + * @phpstan-ignore method.unused + */ private function check_category( $category ) { if ( null === $category ) { return true; @@ -428,7 +477,7 @@ private function check_category( $category ) { } $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; } @@ -436,13 +485,18 @@ private function check_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; } @@ -455,6 +509,11 @@ private function check_post_status( $status ) { return true; } + /** + * @param string|null $skip + * + * @phpstan-ignore method.unused + */ private function check_skip_comments( $skip ) { if ( null === $skip ) { return true; @@ -468,6 +527,11 @@ private function check_skip_comments( $skip ) { 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.' ); @@ -479,6 +543,11 @@ private function check_max_file_size( $size ) { return true; } + /** + * @param string $once + * + * @phpstan-ignore method.unused + */ private function check_include_once( $once ) { if ( null === $once ) { return true; @@ -497,6 +566,11 @@ private function check_include_once( $once ) { 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; diff --git a/src/WP_Export_Oxymel.php b/src/WP_Export_Oxymel.php index 0d3c75e3..39480a07 100644 --- a/src/WP_Export_Oxymel.php +++ b/src/WP_Export_Oxymel.php @@ -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; } diff --git a/src/WP_Export_Query.php b/src/WP_Export_Query.php index 4ddc9656..02e46aac 100644 --- a/src/WP_Export_Query.php +++ b/src/WP_Export_Query.php @@ -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; @@ -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 ) ) { @@ -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' ); } diff --git a/src/WP_Export_Returner.php b/src/WP_Export_Returner.php index 6327e135..234e1b5b 100644 --- a/src/WP_Export_Returner.php +++ b/src/WP_Export_Returner.php @@ -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 ) { diff --git a/src/WP_Export_WXR_Formatter.php b/src/WP_Export_WXR_Formatter.php index 3910cc8e..2f497396 100644 --- a/src/WP_Export_WXR_Formatter.php +++ b/src/WP_Export_WXR_Formatter.php @@ -12,11 +12,24 @@ * Responsible for formatting the data in WP_Export_Query to WXR */ class WP_Export_WXR_Formatter { + /** + * @var WP_Export_Query + */ + private $export; + + /** + * @var string + */ + private $wxr_version; + public function __construct( $export ) { $this->export = $export; $this->wxr_version = WXR_VERSION; } + /** + * @param array $requested_sections + */ public function before_posts( $requested_sections = [] ) { $available_sections = [ 'header', @@ -72,7 +85,7 @@ public function header() { COMMENT; return $oxymel - ->xml + ->xml // @phpstan-ignore-line ->comment( $comment ) ->raw( $wp_generator_tag ) ->open_rss( @@ -92,7 +105,7 @@ public function header() { public function site_metadata() { $oxymel = new Oxymel(); $metadata = $this->export->site_metadata(); - return $oxymel + return $oxymel // @phpstan-ignore-line ->title( $metadata['name'] ) ->link( $metadata['url'] ) ->description( $metadata['description'] ) @@ -116,7 +129,7 @@ public function authors() { ->tag( 'wp:author_display_name' )->contains->cdata( $author->display_name )->end ->tag( 'wp:author_first_name' )->contains->cdata( $author->user_firstname )->end ->tag( 'wp:author_last_name' )->contains->cdata( $author->user_lastname )->end - ->end; + ->end; // @phpstan-ignore-line } return $oxymel->to_string(); } @@ -132,7 +145,7 @@ public function categories() { ->tag( 'wp:category_parent', $category->parent_slug ) ->optional_cdata( 'wp:cat_name', $category->name ) ->optional_cdata( 'wp:category_description', $category->description ) - ->end; + ->end; // @phpstan-ignore-line } return $oxymel->to_string(); } @@ -146,7 +159,7 @@ public function tags() { ->tag( 'wp:tag_slug', $tag->slug ) ->optional_cdata( 'wp:tag_name', $tag->name ) ->optional_cdata( 'wp:tag_description', $tag->description ) - ->end; + ->end; // @phpstan-ignore-line } return $oxymel->to_string(); } @@ -173,10 +186,10 @@ public function post( $post ) { $GLOBALS['post'] = $post; setup_postdata( $post ); - $oxymel->item->contains + $oxymel->item->contains // @phpstan-ignore-line ->tag( 'title' )->contains->cdata( apply_filters( 'the_title_export', $post->post_title ) )->end // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WordPress native hook. ->link( esc_url( apply_filters( 'the_permalink_rss', get_permalink() ) ) ) // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WordPress native hook. - ->pubDate( mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ) ) + ->pubDate( mysql2date( 'D, d M Y H:i:s +0000', (string) get_post_time( 'Y-m-d H:i:s', true ), false ) ) ->tag( 'dc:creator', get_the_author_meta( 'login' ) ) ->guid( esc_url( get_the_guid() ), [ 'isPermaLink' => 'false' ] ) ->description( '' ) @@ -198,23 +211,23 @@ public function post( $post ) { ->tag( 'wp:is_sticky', $post->is_sticky ) ->optional( 'wp:attachment_url', wp_get_attachment_url( $post->ID ) ); foreach ( $post->terms as $term ) { - $oxymel + $oxymel // @phpstan-ignore-line ->category( [ 'domain' => $term->taxonomy, 'nicename' => $term->slug, ] - )->contains->cdata( $term->name )->end; + )->contains->cdata( $term->name )->end; // @phpstan-ignore-line } foreach ( $post->meta as $meta ) { - $oxymel + $oxymel // @phpstan-ignore-line ->tag( 'wp:postmeta' )->contains ->tag( 'wp:meta_key', $meta->meta_key ) ->tag( 'wp:meta_value' )->contains->cdata( $meta->meta_value )->end - ->end; + ->end; // @phpstan-ignore-line } foreach ( $post->comments as $comment ) { - $oxymel + $oxymel // @phpstan-ignore-line ->tag( 'wp:comment' )->contains ->tag( 'wp:comment_id', $comment->comment_ID ) ->tag( 'wp:comment_author' )->contains->cdata( $comment->comment_author )->end @@ -229,16 +242,16 @@ public function post( $post ) { ->tag( 'wp:comment_parent', $comment->comment_parent ) ->tag( 'wp:comment_user_id', $comment->user_id ) ->oxymel( $this->comment_meta( $comment ) ) - ->end; + ->end; // @phpstan-ignore-line } - $oxymel - ->end; + $oxymel // @phpstan-ignore-line + ->end; // @phpstan-ignore-line return $oxymel->to_string(); } public function footer() { $oxymel = new Oxymel(); - return $oxymel->close_channel->close_rss->to_string(); + return $oxymel->close_channel->close_rss->to_string(); // @phpstan-ignore-line } protected function terms( $terms ) { @@ -253,10 +266,10 @@ protected function terms( $terms ) { $oxymel ->tag( 'wp:term_parent', $term->parent_slug ); } - $oxymel + $oxymel // @phpstan-ignore-line ->optional_cdata( 'wp:term_name', $term->name ) ->optional_cdata( 'wp:term_description', $term->description ) - ->end; + ->end; // @phpstan-ignore-line } return $oxymel->to_string(); } @@ -269,10 +282,10 @@ protected function comment_meta( $comment ) { } $oxymel = new WP_Export_Oxymel(); foreach ( $metas as $meta ) { - $oxymel->tag( 'wp:commentmeta' )->contains + $oxymel->tag( 'wp:commentmeta' )->contains // @phpstan-ignore-line ->tag( 'wp:meta_key', $meta->meta_key ) ->tag( 'wp:meta_value' )->contains->cdata( $meta->meta_value )->end - ->end; + ->end; // @phpstan-ignore-line } return $oxymel; } diff --git a/src/WP_Export_XML_Over_HTTP.php b/src/WP_Export_XML_Over_HTTP.php index f4dee588..a0332b2f 100644 --- a/src/WP_Export_XML_Over_HTTP.php +++ b/src/WP_Export_XML_Over_HTTP.php @@ -1,9 +1,20 @@ file_name = $file_name; diff --git a/src/WP_Map_Iterator.php b/src/WP_Map_Iterator.php index 9d2d0b3e..b68a618e 100644 --- a/src/WP_Map_Iterator.php +++ b/src/WP_Map_Iterator.php @@ -1,6 +1,18 @@ > + */ class WP_Map_Iterator extends IteratorIterator { + /** + * @var callable + */ + private $callback; + + /** + * @param \Iterator $iterator + * @param callable $callback + */ public function __construct( $iterator, $callback ) { $this->callback = $callback; parent::__construct( $iterator ); diff --git a/src/WP_Post_IDs_Iterator.php b/src/WP_Post_IDs_Iterator.php index b05c6f12..4560ccc3 100644 --- a/src/WP_Post_IDs_Iterator.php +++ b/src/WP_Post_IDs_Iterator.php @@ -1,11 +1,44 @@ + */ class WP_Post_IDs_Iterator implements Iterator { + /** + * @var \wpdb + */ + private $db; + + /** + * @var int + */ private $limit = 100; + + /** + * @var int[] + */ private $post_ids; + + /** + * @var int[] + */ private $ids_left; + + /** + * @var object[] + */ private $results = array(); + /** + * @var int + */ + private $index_in_results; + + /** + * @var int + */ + private $global_index; + public function __construct( $post_ids, $limit = null ) { $this->db = $GLOBALS['wpdb']; $this->post_ids = $post_ids; @@ -15,19 +48,23 @@ public function __construct( $post_ids, $limit = null ) { } } + #[\ReturnTypeWillChange] public function current() { return $this->results[ $this->index_in_results ]; } + #[\ReturnTypeWillChange] public function key() { return $this->global_index; } + #[\ReturnTypeWillChange] public function next() { ++$this->index_in_results; ++$this->global_index; } + #[\ReturnTypeWillChange] public function rewind() { $this->results = array(); $this->global_index = 0; @@ -35,6 +72,7 @@ public function rewind() { $this->ids_left = $this->post_ids; } + #[\ReturnTypeWillChange] public function valid() { if ( isset( $this->results[ $this->index_in_results ] ) ) { return true; @@ -53,7 +91,8 @@ public function valid() { private function load_next_posts_from_db() { $next_batch_post_ids = array_splice( $this->ids_left, 0, $this->limit ); $in_post_ids_sql = _wp_export_build_IN_condition( 'ID', $next_batch_post_ids ); - $this->results = $this->db->get_results( "SELECT * FROM {$this->db->posts} WHERE {$in_post_ids_sql}" ); + $results = $this->db->get_results( "SELECT * FROM {$this->db->posts} WHERE {$in_post_ids_sql}" ); + $this->results = is_array( $results ) ? $results : array(); if ( ! $this->results ) { if ( $this->db->last_error ) { throw new WP_Iterator_Exception( "Database error: {$this->db->last_error}" );