Skip to content

Commit f8e11d8

Browse files
committed
PHPStan fixes
1 parent e6de7e2 commit f8e11d8

7 files changed

Lines changed: 66 additions & 34 deletions

‎functions.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* @param array{filters?: array<mixed>, format?: class-string<WP_Export_WXR_Formatter>, writer?: class-string<WP_Export_Returner>, writer_args?: mixed} $args
4+
* @param array{filters?: array<mixed>, format?: class-string<WP_Export_WXR_Formatter>, writer?: class-string<WP_Export_Base_Writer>, writer_args?: mixed} $args
55
*/
66
function wp_export( $args = array() ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Renaming breaks Phar compat.
77
$defaults = array(
@@ -12,7 +12,7 @@ function wp_export( $args = array() ) { // phpcs:ignore WordPress.NamingConventi
1212
);
1313

1414
/**
15-
* @var array{filters: array<mixed>, format: class-string<WP_Export_WXR_Formatter>, writer: class-string<WP_Export_Returner>, writer_args: mixed} $args
15+
* @var array{filters: array<mixed>, format: class-string<WP_Export_WXR_Formatter>, writer: class-string<WP_Export_Base_Writer>, writer_args: mixed} $args
1616
*/
1717
$args = wp_parse_args( $args, $defaults );
1818
$export_query = new WP_Export_Query( $args['filters'] );

‎src/Export_Command.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,15 @@ static function ( $file_path ) {
190190
wp_export(
191191
[
192192
'filters' => $this->export_args,
193-
'writer' => WP_Export_File_Writer::class, // @phpstan-ignore argument.type
193+
'writer' => WP_Export_File_Writer::class,
194194
'writer_args' => 'php://output',
195195
]
196196
);
197197
} else {
198198
wp_export(
199199
[
200200
'filters' => $this->export_args,
201-
'writer' => WP_Export_Split_Files_Writer::class, // @phpstan-ignore argument.type
201+
'writer' => WP_Export_Split_Files_Writer::class,
202202
'writer_args' => [
203203
'max_file_size' => $this->max_file_size,
204204
'destination_directory' => $this->wxr_path,

‎src/WP_Export_Oxymel.php‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@ public function optional( $tag_name, $contents ) {
1010

1111
public function optional_cdata( $tag_name, $contents ) {
1212
if ( $contents ) {
13-
$this->$tag_name->contains->cdata( $contents )->end; // @phpstan-ignore-line
13+
$this->$tag_name->contains->cdata( $contents )->end;
1414
}
1515
return $this;
1616
}
1717

1818
public function cdata( $text ) {
19-
if ( is_string( $text ) && ! seems_utf8( $text ) ) {
20-
$text = mb_convert_encoding( $text, 'UTF-8' );
19+
if ( is_string( $text ) ) {
20+
if ( function_exists( 'wp_is_valid_utf8' ) && ! wp_is_valid_utf8( $text ) ) {
21+
$text = mb_convert_encoding( $text, 'UTF-8' );
22+
// @phpstan-ignore function.deprecated
23+
} elseif ( ! seems_utf8( $text ) ) { // phpcs:ignore WordPress.WP.DeprecatedFunctions.seems_utf8Found
24+
$text = mb_convert_encoding( $text, 'UTF-8' );
25+
}
2126
}
2227
return parent::cdata( $text );
2328
}

‎src/WP_Export_Query.php‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function custom_taxonomies_terms() {
122122
}
123123
$custom_taxonomies = get_taxonomies( [ '_builtin' => false ] );
124124
// phpcs:ignore WordPress.WP.DeprecatedParameters.Get_termsParam2Found -- Deprecated, but we need to support older versions of WordPress.
125-
$custom_terms = (array) get_terms( $custom_taxonomies, [ 'get' => 'all' ] ); // @phpstan-ignore-line
125+
$custom_terms = (array) get_terms( $custom_taxonomies, [ 'get' => 'all' ] );
126126
$custom_terms = $this->process_orphaned_terms( $custom_terms );
127127
$custom_terms = self::topologically_sort_terms( $custom_terms );
128128
return $custom_terms;
@@ -137,8 +137,13 @@ public function nav_menu_terms() {
137137
}
138138

139139
public function exportify_post( $post ) {
140-
$GLOBALS['wp_query']->in_the_loop = true;
141-
$previous_global_post = Utils\get_flag_value( $GLOBALS, 'post' );
140+
/**
141+
* @var \WP_Query $wp_query
142+
*/
143+
global $wp_query;
144+
145+
$wp_query->in_the_loop = true;
146+
$previous_global_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
142147
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Temporary override.
143148
$GLOBALS['post'] = $post;
144149
setup_postdata( $post );
@@ -207,6 +212,9 @@ private function post_type_where() {
207212
if ( isset( $post_types_filters['name'] ) && is_array( $post_types_filters['name'] ) ) {
208213
$post_types = [];
209214
foreach ( $post_types_filters['name'] as $post_type ) {
215+
/**
216+
* @var string $post_type
217+
*/
210218
if ( post_type_exists( $post_type ) ) {
211219
$post_types[] = $post_type;
212220
}

‎src/WP_Export_WXR_Formatter.php‎

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function after_posts() {
6464
public function header() {
6565
$oxymel = new Oxymel();
6666
$wp_generator_tag = $this->export->wp_generator_tag();
67-
$comment = <<<COMMENT
67+
$comment = <<<'COMMENT'
6868
6969
This is a WordPress eXtended RSS file generated by WordPress as an export of your site.
7070
It contains information about your site's posts, pages, comments, categories, and other content.
@@ -84,8 +84,9 @@ public function header() {
8484
contained in this file into your site.
8585

8686
COMMENT;
87+
// @phpstan-ignore property.private
8788
return $oxymel
88-
->xml // @phpstan-ignore-line
89+
->xml
8990
->comment( $comment )
9091
->raw( $wp_generator_tag )
9192
->open_rss(
@@ -105,7 +106,8 @@ public function header() {
105106
public function site_metadata() {
106107
$oxymel = new Oxymel();
107108
$metadata = $this->export->site_metadata();
108-
return $oxymel // @phpstan-ignore-line
109+
// @phpstan-ignore method.notFound
110+
return $oxymel
109111
->title( $metadata['name'] )
110112
->link( $metadata['url'] )
111113
->description( $metadata['description'] )
@@ -129,7 +131,7 @@ public function authors() {
129131
->tag( 'wp:author_display_name' )->contains->cdata( $author->display_name )->end
130132
->tag( 'wp:author_first_name' )->contains->cdata( $author->user_firstname )->end
131133
->tag( 'wp:author_last_name' )->contains->cdata( $author->user_lastname )->end
132-
->end; // @phpstan-ignore-line
134+
->end;
133135
}
134136
return $oxymel->to_string();
135137
}
@@ -145,7 +147,7 @@ public function categories() {
145147
->tag( 'wp:category_parent', $category->parent_slug )
146148
->optional_cdata( 'wp:cat_name', $category->name )
147149
->optional_cdata( 'wp:category_description', $category->description )
148-
->end; // @phpstan-ignore-line
150+
->end;
149151
}
150152
return $oxymel->to_string();
151153
}
@@ -159,7 +161,7 @@ public function tags() {
159161
->tag( 'wp:tag_slug', $tag->slug )
160162
->optional_cdata( 'wp:tag_name', $tag->name )
161163
->optional_cdata( 'wp:tag_description', $tag->description )
162-
->end; // @phpstan-ignore-line
164+
->end;
163165
}
164166
return $oxymel->to_string();
165167
}
@@ -180,13 +182,18 @@ public function rss2_head_action() {
180182
}
181183

182184
public function post( $post ) {
183-
$oxymel = new WP_Export_Oxymel();
184-
$GLOBALS['wp_query']->in_the_loop = true;
185+
/**
186+
* @var \WP_Query $wp_query
187+
*/
188+
global $wp_query;
189+
$oxymel = new WP_Export_Oxymel();
190+
$wp_query->in_the_loop = true;
185191
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Intentional.
186192
$GLOBALS['post'] = $post;
187193
setup_postdata( $post );
188194

189-
$oxymel->item->contains // @phpstan-ignore-line
195+
// @phpstan-ignore property.notFound
196+
$oxymel->item->contains
190197
->tag( 'title' )->contains->cdata( apply_filters( 'the_title_export', $post->post_title ) )->end // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WordPress native hook.
191198
->link( esc_url( apply_filters( 'the_permalink_rss', get_permalink() ) ) ) // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WordPress native hook.
192199
->pubDate( mysql2date( 'D, d M Y H:i:s +0000', (string) get_post_time( 'Y-m-d H:i:s', true ), false ) )
@@ -211,23 +218,24 @@ public function post( $post ) {
211218
->tag( 'wp:is_sticky', $post->is_sticky )
212219
->optional( 'wp:attachment_url', wp_get_attachment_url( $post->ID ) );
213220
foreach ( $post->terms as $term ) {
214-
$oxymel // @phpstan-ignore-line
221+
// @phpstan-ignore method.notFound
222+
$oxymel
215223
->category(
216224
[
217225
'domain' => $term->taxonomy,
218226
'nicename' => $term->slug,
219227
]
220-
)->contains->cdata( $term->name )->end; // @phpstan-ignore-line
228+
)->contains->cdata( $term->name )->end;
221229
}
222230
foreach ( $post->meta as $meta ) {
223-
$oxymel // @phpstan-ignore-line
231+
$oxymel
224232
->tag( 'wp:postmeta' )->contains
225233
->tag( 'wp:meta_key', $meta->meta_key )
226234
->tag( 'wp:meta_value' )->contains->cdata( $meta->meta_value )->end
227-
->end; // @phpstan-ignore-line
235+
->end;
228236
}
229237
foreach ( $post->comments as $comment ) {
230-
$oxymel // @phpstan-ignore-line
238+
$oxymel
231239
->tag( 'wp:comment' )->contains
232240
->tag( 'wp:comment_id', $comment->comment_ID )
233241
->tag( 'wp:comment_author' )->contains->cdata( $comment->comment_author )->end
@@ -242,16 +250,18 @@ public function post( $post ) {
242250
->tag( 'wp:comment_parent', $comment->comment_parent )
243251
->tag( 'wp:comment_user_id', $comment->user_id )
244252
->oxymel( $this->comment_meta( $comment ) )
245-
->end; // @phpstan-ignore-line
253+
->end;
246254
}
247-
$oxymel // @phpstan-ignore-line
248-
->end; // @phpstan-ignore-line
255+
256+
// @phpstan-ignore property.notFound, expr.resultUnused
257+
$oxymel->end;
249258
return $oxymel->to_string();
250259
}
251260

252261
public function footer() {
253262
$oxymel = new Oxymel();
254-
return $oxymel->close_channel->close_rss->to_string(); // @phpstan-ignore-line
263+
// @phpstan-ignore property.notFound
264+
return $oxymel->close_channel->close_rss->to_string();
255265
}
256266

257267
protected function terms( $terms ) {
@@ -266,10 +276,10 @@ protected function terms( $terms ) {
266276
$oxymel
267277
->tag( 'wp:term_parent', $term->parent_slug );
268278
}
269-
$oxymel // @phpstan-ignore-line
279+
$oxymel
270280
->optional_cdata( 'wp:term_name', $term->name )
271281
->optional_cdata( 'wp:term_description', $term->description )
272-
->end; // @phpstan-ignore-line
282+
->end;
273283
}
274284
return $oxymel->to_string();
275285
}
@@ -282,10 +292,10 @@ protected function comment_meta( $comment ) {
282292
}
283293
$oxymel = new WP_Export_Oxymel();
284294
foreach ( $metas as $meta ) {
285-
$oxymel->tag( 'wp:commentmeta' )->contains // @phpstan-ignore-line
295+
$oxymel->tag( 'wp:commentmeta' )->contains
286296
->tag( 'wp:meta_key', $meta->meta_key )
287297
->tag( 'wp:meta_value' )->contains->cdata( $meta->meta_value )->end
288-
->end; // @phpstan-ignore-line
298+
->end;
289299
}
290300
return $oxymel;
291301
}

‎src/WP_Export_XML_Over_HTTP.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ protected function get_export() {
4848
}
4949

5050
protected function send_headers() {
51+
/**
52+
* @var string $charset
53+
*/
54+
$charset = get_option( 'blog_charset' );
5155
header( 'Content-Description: File Transfer' );
5256
header( 'Content-Disposition: attachment; filename=' . $this->file_name );
53-
header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
57+
header( 'Content-Type: text/xml; charset=' . $charset, true );
5458
}
5559
}

‎src/WP_Post_IDs_Iterator.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ class WP_Post_IDs_Iterator implements Iterator {
4040
private $global_index;
4141

4242
public function __construct( $post_ids, $limit = null ) {
43-
$this->db = $GLOBALS['wpdb'];
43+
/**
44+
* @var \wpdb $wpdb
45+
*/
46+
global $wpdb;
47+
48+
$this->db = $wpdb;
4449
$this->post_ids = $post_ids;
4550
$this->ids_left = $post_ids;
4651
if ( ! is_null( $limit ) ) {

0 commit comments

Comments
 (0)