Skip to content

Commit f57a4b0

Browse files
authored
Merge pull request #258 from wp-cli/add/phpstan
2 parents f2e40fa + f9588d6 commit f57a4b0

File tree

6 files changed

+43
-33
lines changed

6 files changed

+43
-33
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"php": ">=7.2.24",
2929
"wp-cli/package-command": "^2",
3030
"wp-cli/scaffold-command": "^2",
31-
"wp-cli/wp-cli": "^2.12"
31+
"wp-cli/wp-cli": "^2.13"
3232
},
3333
"require-dev": {
3434
"wp-cli/wp-cli-tests": "^5"

features/scaffold-package-readme.feature

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Feature: Scaffold a README.md file for an existing package
4040
And the {PACKAGE_PATH}/local/wp-cli/default-readme/README.md file should exist
4141
And the {PACKAGE_PATH}/local/wp-cli/default-readme/README.md file should contain:
4242
"""
43-
Installing this package requires WP-CLI v2.11 or greater. Update to the latest stable release with `wp cli update`.
43+
Installing this package requires WP-CLI v2.12 or greater. Update to the latest stable release with `wp cli update`.
4444
"""
4545
And the {PACKAGE_PATH}/local/wp-cli/default-readme/README.md file should contain:
4646
"""
@@ -76,7 +76,7 @@ Feature: Scaffold a README.md file for an existing package
7676
And the {PACKAGE_PATH}/local/wp-cli/custom-branch/README.md file should exist
7777
And the {PACKAGE_PATH}/local/wp-cli/custom-branch/README.md file should contain:
7878
"""
79-
Installing this package requires WP-CLI v2.11 or greater. Update to the latest stable release with `wp cli update`.
79+
Installing this package requires WP-CLI v2.12 or greater. Update to the latest stable release with `wp cli update`.
8080
"""
8181
And the {PACKAGE_PATH}/local/wp-cli/custom-branch/README.md file should contain:
8282
"""
@@ -152,10 +152,10 @@ Feature: Scaffold a README.md file for an existing package
152152
"files": [ "command.php" ]
153153
},
154154
"require": {
155-
"wp-cli/wp-cli": "^2.11"
155+
"wp-cli/wp-cli": "^2.12"
156156
},
157157
"require-dev": {
158-
"wp-cli/wp-cli-tests": "^4.3.9"
158+
"wp-cli/wp-cli-tests": "^5.0.0"
159159
},
160160
"extra": {
161161
"readme": {
@@ -333,7 +333,7 @@ Feature: Scaffold a README.md file for an existing package
333333
},
334334
"require-dev": {
335335
"wp-cli/wp-cli": "*",
336-
"wp-cli/wp-cli-tests": "^4.3.9"
336+
"wp-cli/wp-cli-tests": "^5.0.0"
337337
},
338338
"extra": {
339339
"bundled": true

features/scaffold-package.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Feature: Scaffold WP-CLI commands
3434
And the {PACKAGE_PATH}/local/wp-cli/foo/composer.json file should contain:
3535
"""
3636
"require": {
37-
"wp-cli/wp-cli": "^2.11"
37+
"wp-cli/wp-cli": "^2.12"
3838
},
3939
"""
4040
And the {PACKAGE_PATH}/local/wp-cli/foo/hello-world-command.php file should exist

phpstan.neon.dist

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

src/ScaffoldPackageCommand.php

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use WP_CLI;
66
use WP_CLI\Utils;
77

8+
/**
9+
* @phpstan-type ComposerConfig array{name: string, description: string, extra: array{readme: array{shields: array<string>}, commands: array<string>}, require: array<string, string>, 'require-dev': array<string, string>}
10+
*/
811
class ScaffoldPackageCommand {
912

1013
/**
@@ -43,13 +46,13 @@ class ScaffoldPackageCommand {
4346
* [--require_wp_cli=<version>]
4447
* : Required WP-CLI version for the package.
4548
* ---
46-
* default: ^2.11
49+
* default: ^2.12
4750
* ---
4851
*
4952
* [--require_wp_cli_tests=<version>]
5053
* : Required WP-CLI testing framework version for the package.
5154
* ---
52-
* default: ^4.3.9
55+
* default: ^5.0.0
5356
* ---
5457
5558
* [--skip-tests]
@@ -242,7 +245,10 @@ public function package_readme( $args, $assoc_args ) {
242245

243246
self::check_if_valid_package_dir( $package_dir );
244247

245-
$composer_obj = json_decode( file_get_contents( $package_dir . '/composer.json' ), true );
248+
/**
249+
* @var null|ComposerConfig $composer_obj
250+
*/
251+
$composer_obj = json_decode( (string) file_get_contents( $package_dir . '/composer.json' ), true );
246252
if ( ! $composer_obj ) {
247253
WP_CLI::error( 'Invalid composer.json in package directory.' );
248254
}
@@ -334,8 +340,8 @@ public function package_readme( $args, $assoc_args ) {
334340
*/
335341

336342
$longdesc = isset( $parent_command['longdesc'] ) ? $parent_command['longdesc'] : '';
337-
$longdesc = preg_replace( '/## GLOBAL PARAMETERS(.+)/s', '', $longdesc );
338-
$longdesc = preg_replace( '/##\s(.+)/', '**$1**', $longdesc );
343+
$longdesc = (string) preg_replace( '/## GLOBAL PARAMETERS(.+)/s', '', $longdesc );
344+
$longdesc = (string) preg_replace( '/##\s(.+)/', '**$1**', $longdesc );
339345

340346
// definition lists
341347
$longdesc = preg_replace_callback( '/([^\n]+)\n: (.+?)(\n\n|$)/s', [ __CLASS__, 'rewrap_param_desc' ], $longdesc );
@@ -405,7 +411,9 @@ public function package_readme( $args, $assoc_args ) {
405411
$v = $composer_obj['extra']['readme'][ $section ][ $k ];
406412
if ( filter_var( $v, FILTER_VALIDATE_URL ) === $v ) {
407413
$response = Utils\http_request( 'GET', $v );
408-
$v = $response->body;
414+
415+
// @phpstan-ignore class.notFound
416+
$v = $response->body;
409417
} elseif ( preg_match( $ext_regex, $v ) ) {
410418
$v = $package_dir . '/' . $v;
411419
}
@@ -479,7 +487,10 @@ public function package_github( $args, $assoc_args ) {
479487
$force = Utils\get_flag_value( $assoc_args, 'force' );
480488
$template_path = dirname( __DIR__ ) . '/templates';
481489

482-
$composer_obj = json_decode( file_get_contents( $package_dir . '/composer.json' ), true );
490+
/**
491+
* @var ComposerConfig $composer_obj
492+
*/
493+
$composer_obj = json_decode( (string) file_get_contents( $package_dir . '/composer.json' ), true );
483494
$settings_vars = [
484495
'has_description' => false,
485496
'description' => '',
@@ -709,27 +720,10 @@ public function package_tests( $args, $assoc_args ) {
709720
$files_written = [];
710721
foreach ( $copy_source as $source => $to_copy ) {
711722
foreach ( $to_copy as $file => $dir ) {
712-
if ( 'php/WP_CLI/ProcessRun.php' === $file && ! file_exists( $source . "/{$file}" ) ) {
713-
continue;
714-
}
715723
// file_get_contents() works with Phar-archived files
716-
$contents = file_get_contents( $source . "/{$file}" );
724+
$contents = (string) file_get_contents( $source . "/{$file}" );
717725
$file_path = $dir . basename( $file );
718726

719-
if ( '.travis.yml' === $file ) {
720-
foreach ( $travis_tags as $travis_tag ) {
721-
if ( isset( $travis_tag_overwrites[ $travis_tag ] ) ) {
722-
// Note the contents fully overwrite, so should include the tag, eg `env:` etc (or nothing if want to remove totally).
723-
$contents = preg_replace( '/^' . $travis_tag . ':.*?(?:^$|\z)/ms', $travis_tag_overwrites[ $travis_tag ], $contents );
724-
} elseif ( isset( $travis_tag_appends[ $travis_tag ] ) ) {
725-
$contents = preg_replace( '/^' . $travis_tag . ':.*?(?:^$|\z)/ms', '\0' . $travis_tag_appends[ $travis_tag ], $contents );
726-
}
727-
}
728-
if ( $travis_append ) {
729-
$contents .= $travis_append;
730-
}
731-
}
732-
733727
$force = Utils\get_flag_value( $assoc_args, 'force' );
734728
$should_write_file = $this->prompt_if_files_will_be_overwritten( $file_path, $force );
735729
if ( ! $should_write_file ) {

templates/composer.mustache

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"process-timeout": 7200,
1616
"sort-packages": true,
1717
"allow-plugins": {
18-
"dealerdirect/phpcodesniffer-composer-installer": true
18+
"dealerdirect/phpcodesniffer-composer-installer": true,
19+
"phpstan/extension-installer": true
1920
}
2021
},
2122
"extra": {
@@ -42,12 +43,14 @@
4243
"behat-rerun": "rerun-behat-tests",
4344
"lint": "run-linter-tests",
4445
"phpcs": "run-phpcs-tests",
46+
"phpstan": "run-phpstan-tests",
4547
"phpcbf": "run-phpcbf-cleanup",
4648
"phpunit": "run-php-unit-tests",
4749
"prepare-tests": "install-package-tests",
4850
"test": [
4951
"@lint",
5052
"@phpcs",
53+
"@phpstan",
5154
"@phpunit",
5255
"@behat"
5356
]

0 commit comments

Comments
 (0)