Skip to content

Commit 46639c3

Browse files
authored
Merge pull request #125 from wp-cli/add/phpstan
PHPStan level 9
2 parents 84a335c + f08b921 commit 46639c3

10 files changed

Lines changed: 218 additions & 34 deletions

functions.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
<?php
22

3-
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Renaming breaks Phar compat.
4-
function wp_export( $args = array() ) {
5-
$defaults = array(
3+
/**
4+
* @param array{filters?: array<mixed>, format?: class-string<WP_Export_WXR_Formatter>, writer?: class-string<WP_Export_Base_Writer>, writer_args?: mixed} $args
5+
*/
6+
function wp_export( $args = array() ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Renaming breaks Phar compat.
7+
$defaults = array(
68
'filters' => array(),
79
'format' => 'WP_Export_WXR_Formatter',
810
'writer' => 'WP_Export_Returner',
911
'writer_args' => null,
1012
);
13+
14+
/**
15+
* @var array{filters: array<mixed>, format: class-string<WP_Export_WXR_Formatter>, writer: class-string<WP_Export_Base_Writer>, writer_args: mixed} $args
16+
*/
1117
$args = wp_parse_args( $args, $defaults );
1218
$export_query = new WP_Export_Query( $args['filters'] );
1319
$formatter = new $args['format']( $export_query );

phpstan.neon.dist

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
parameters:
2+
level: 9
3+
paths:
4+
- src
5+
- export-command.php
6+
- functions.php
7+
scanDirectories:
8+
- vendor/wp-cli/wp-cli/php
9+
scanFiles:
10+
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
11+
treatPhpDocTypesAsCertain: false
12+
ignoreErrors:
13+
- identifier: missingType.iterableValue
14+
- identifier: missingType.property
15+
- identifier: missingType.parameter
16+
- identifier: missingType.return

src/Export_Command.php

Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function __invoke( $_, $assoc_args ) {
181181
'wp_export_new_file',
182182
static function ( $file_path ) {
183183
WP_CLI::log( sprintf( 'Writing to file %s', $file_path ) );
184-
Utils\wp_clear_object_cache();
184+
Utils\wp_clear_object_cache(); // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.wp_clear_object_cacheDeprecatedRemoved @phpstan-ignore-line
185185
}
186186
);
187187

@@ -190,15 +190,15 @@ static function ( $file_path ) {
190190
wp_export(
191191
[
192192
'filters' => $this->export_args,
193-
'writer' => 'WP_Export_File_Writer',
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',
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,
@@ -234,6 +234,7 @@ private function validate_args( $args ) {
234234

235235
foreach ( $args as $key => $value ) {
236236
if ( is_callable( [ $this, 'check_' . $key ] ) ) {
237+
/** @phpstan-ignore argument.type */
237238
$result = call_user_func( [ $this, 'check_' . $key ], $value );
238239
if ( false === $result ) {
239240
$has_errors = true;
@@ -251,9 +252,14 @@ private function validate_args( $args ) {
251252
}
252253
}
253254

255+
/**
256+
* @param string $path
257+
*
258+
* @phpstan-ignore method.unused
259+
*/
254260
private function check_dir( $path ) {
255261
if ( empty( $path ) ) {
256-
$path = getcwd();
262+
$path = (string) getcwd();
257263
} elseif ( ! is_dir( $path ) ) {
258264
WP_CLI::error( sprintf( "The directory '%s' does not exist.", $path ) );
259265
} elseif ( ! is_writable( $path ) ) {
@@ -265,34 +271,49 @@ private function check_dir( $path ) {
265271
return true;
266272
}
267273

274+
/**
275+
* @param string $date
276+
*
277+
* @phpstan-ignore method.unused
278+
*/
268279
private function check_start_date( $date ) {
269280
if ( null === $date ) {
270281
return true;
271282
}
272283

273284
$time = strtotime( $date );
274-
if ( ! empty( $date ) && ! $time ) {
285+
if ( ! empty( $date ) && false === $time ) {
275286
WP_CLI::warning( sprintf( 'The start_date %s is invalid.', $date ) );
276287
return false;
277288
}
278-
$this->export_args['start_date'] = date( 'Y-m-d', $time ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
289+
$this->export_args['start_date'] = date( 'Y-m-d', (int) $time ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
279290
return true;
280291
}
281292

293+
/**
294+
* @param string $date
295+
*
296+
* @phpstan-ignore method.unused
297+
*/
282298
private function check_end_date( $date ) {
283299
if ( null === $date ) {
284300
return true;
285301
}
286302

287303
$time = strtotime( $date );
288-
if ( ! empty( $date ) && ! $time ) {
304+
if ( ! empty( $date ) && false === $time ) {
289305
WP_CLI::warning( sprintf( 'The end_date %s is invalid.', $date ) );
290306
return false;
291307
}
292-
$this->export_args['end_date'] = date( 'Y-m-d', $time ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
308+
$this->export_args['end_date'] = date( 'Y-m-d', (int) $time ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
293309
return true;
294310
}
295311

312+
/**
313+
* @param string $post_type
314+
*
315+
* @phpstan-ignore method.unused
316+
*/
296317
private function check_post_type( $post_type ) {
297318
if ( null === $post_type || 'any' === $post_type ) {
298319
return true;
@@ -317,6 +338,11 @@ private function check_post_type( $post_type ) {
317338
return true;
318339
}
319340

341+
/**
342+
* @param string $post_type
343+
*
344+
* @phpstan-ignore method.unused
345+
*/
320346
private function check_post_type__not_in( $post_type ) {
321347
if ( null === $post_type ) {
322348
return true;
@@ -341,6 +367,11 @@ private function check_post_type__not_in( $post_type ) {
341367
return true;
342368
}
343369

370+
/**
371+
* @param string $post__in
372+
*
373+
* @phpstan-ignore method.unused
374+
*/
344375
private function check_post__in( $post__in ) {
345376
if ( null === $post__in ) {
346377
return true;
@@ -357,6 +388,11 @@ private function check_post__in( $post__in ) {
357388
return true;
358389
}
359390

391+
/**
392+
* @param string $start_id
393+
*
394+
* @phpstan-ignore method.unused
395+
*/
360396
private function check_start_id( $start_id ) {
361397
if ( null === $start_id ) {
362398
return true;
@@ -374,14 +410,19 @@ private function check_start_id( $start_id ) {
374410
return true;
375411
}
376412

413+
/**
414+
* @param string $author
415+
*
416+
* @phpstan-ignore method.unused
417+
*/
377418
private function check_author( $author ) {
378419
if ( null === $author ) {
379420
return true;
380421
}
381422

382423
// phpcs:ignore WordPress.WP.DeprecatedFunctions.get_users_of_blogFound -- Fallback.
383-
$authors = function_exists( 'get_users' ) ? get_users() : get_users_of_blog();
384-
if ( empty( $authors ) || is_wp_error( $authors ) ) {
424+
$authors = function_exists( 'get_users' ) ? get_users() : get_users_of_blog(); // @phpstan-ignore-line
425+
if ( empty( $authors ) ) {
385426
WP_CLI::warning( 'Could not find any authors in this blog.' );
386427
return false;
387428
}
@@ -407,6 +448,9 @@ private function check_author( $author ) {
407448
return true;
408449
}
409450

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

465+
/**
466+
* @param string $category
467+
*
468+
* @phpstan-ignore method.unused
469+
*/
421470
private function check_category( $category ) {
422471
if ( null === $category ) {
423472
return true;
@@ -428,14 +477,19 @@ private function check_category( $category ) {
428477
}
429478

430479
$term = category_exists( $category );
431-
if ( empty( $term ) || is_wp_error( $term ) ) {
480+
if ( empty( $term ) ) {
432481
WP_CLI::warning( sprintf( 'Could not find a category matching %s.', $category ) );
433482
return false;
434483
}
435484
$this->export_args['category'] = $category;
436485
return true;
437486
}
438487

488+
/**
489+
* @param string $status
490+
*
491+
* @phpstan-ignore method.unused
492+
*/
439493
private function check_post_status( $status ) {
440494
if ( null === $status ) {
441495
return true;
@@ -456,6 +510,11 @@ private function check_post_status( $status ) {
456510
return true;
457511
}
458512

513+
/**
514+
* @param string|null $skip
515+
*
516+
* @phpstan-ignore method.unused
517+
*/
459518
private function check_skip_comments( $skip ) {
460519
if ( null === $skip ) {
461520
return true;
@@ -469,6 +528,11 @@ private function check_skip_comments( $skip ) {
469528
return true;
470529
}
471530

531+
/**
532+
* @param string $size
533+
*
534+
* @phpstan-ignore method.unused
535+
*/
472536
private function check_max_file_size( $size ) {
473537
if ( ! is_numeric( $size ) ) {
474538
WP_CLI::warning( 'max_file_size should be numeric.' );
@@ -480,6 +544,11 @@ private function check_max_file_size( $size ) {
480544
return true;
481545
}
482546

547+
/**
548+
* @param string $once
549+
*
550+
* @phpstan-ignore method.unused
551+
*/
483552
private function check_include_once( $once ) {
484553
if ( null === $once ) {
485554
return true;
@@ -498,6 +567,11 @@ private function check_include_once( $once ) {
498567
return true;
499568
}
500569

570+
/**
571+
* @param string $allow_orphan_terms
572+
*
573+
* @phpstan-ignore method.unused
574+
*/
501575
private function check_allow_orphan_terms( $allow_orphan_terms ) {
502576
if ( null === $allow_orphan_terms ) {
503577
return true;

src/WP_Export_Oxymel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public function cdata( $text ) {
2121
if ( ! wp_is_valid_utf8( $text ) ) {
2222
$text = mb_convert_encoding( $text, 'UTF-8' );
2323
}
24+
// @phpstan-ignore function.deprecated
2425
} elseif ( ! seems_utf8( $text ) ) { // phpcs:ignore WordPress.WP.DeprecatedFunctions.seems_utf8Found
2526
$text = mb_convert_encoding( $text, 'UTF-8' );
26-
2727
}
2828
}
2929
return parent::cdata( $text );

src/WP_Export_Query.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,13 @@ public function nav_menu_terms() {
138138
}
139139

140140
public function exportify_post( $post ) {
141-
$GLOBALS['wp_query']->in_the_loop = true;
142-
$previous_global_post = Utils\get_flag_value( $GLOBALS, 'post' );
141+
/**
142+
* @var \WP_Query $wp_query
143+
*/
144+
global $wp_query;
145+
146+
$wp_query->in_the_loop = true;
147+
$previous_global_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
143148
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Temporary override.
144149
$GLOBALS['post'] = $post;
145150
setup_postdata( $post );
@@ -208,6 +213,9 @@ private function post_type_where() {
208213
if ( isset( $post_types_filters['name'] ) && is_array( $post_types_filters['name'] ) ) {
209214
$post_types = [];
210215
foreach ( $post_types_filters['name'] as $post_type ) {
216+
/**
217+
* @var string $post_type
218+
*/
211219
if ( post_type_exists( $post_type ) ) {
212220
$post_types[] = $post_type;
213221
}
@@ -327,7 +335,7 @@ private function bloginfo_rss( $section ) {
327335

328336
private function find_user_from_any_object( $user ) {
329337
if ( is_numeric( $user ) ) {
330-
return get_user_by( 'id', $user );
338+
return get_user_by( 'id', (int) $user );
331339
} elseif ( is_string( $user ) ) {
332340
return get_user_by( 'login', $user );
333341
} elseif ( isset( $user->ID ) ) {
@@ -338,10 +346,10 @@ private function find_user_from_any_object( $user ) {
338346

339347
private function find_category_from_any_object( $category ) {
340348
if ( is_numeric( $category ) ) {
341-
return get_term( $category, 'category' );
349+
return get_term( (int) $category, 'category' );
342350
} elseif ( is_string( $category ) ) {
343351
$term = term_exists( $category, 'category' );
344-
return isset( $term['term_id'] ) ? get_term( $term['term_id'], 'category' ) : false;
352+
return isset( $term['term_id'] ) ? get_term( (int) $term['term_id'], 'category' ) : false;
345353
} elseif ( isset( $category->term_id ) ) {
346354
return get_term( $category->term_id, 'category' );
347355
}

src/WP_Export_Returner.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ class WP_Export_Returner extends WP_Export_Base_Writer {
44
private $result = '';
55

66
public function export() {
7-
$this->private = '';
87
try {
98
parent::export();
109
} catch ( WP_Export_Exception $e ) {

0 commit comments

Comments
 (0)