Skip to content

Commit 20fb05d

Browse files
authored
Merge pull request #113 from wp-cli/add/phpstan
2 parents 0361706 + 5fc8f38 commit 20fb05d

File tree

4 files changed

+53
-23
lines changed

4 files changed

+53
-23
lines changed

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"inmarelibero/gitignore-checker": "^1.0.4"
1818
},
1919
"require-dev": {
20-
"wp-cli/wp-cli-tests": "^4",
20+
"wp-cli/wp-cli-tests": "^5",
2121
"wp-cli/scaffold-command": "^2",
2222
"wp-cli/extension-command": "^2"
2323
},
@@ -46,20 +46,24 @@
4646
"behat-rerun": "rerun-behat-tests",
4747
"lint": "run-linter-tests",
4848
"phpcs": "run-phpcs-tests",
49+
"phpstan": "run-phpstan-tests",
4950
"phpcbf": "run-phpcbf-cleanup",
5051
"phpunit": "run-php-unit-tests",
5152
"prepare-tests": "install-package-tests",
5253
"test": [
5354
"@lint",
5455
"@phpcs",
56+
"@phpstan",
5557
"@phpunit",
5658
"@behat"
5759
]
5860
},
5961
"config": {
6062
"allow-plugins": {
6163
"dealerdirect/phpcodesniffer-composer-installer": true,
62-
"johnpbloch/wordpress-core-installer": true
63-
}
64+
"johnpbloch/wordpress-core-installer": true,
65+
"phpstan/extension-installer": true
66+
},
67+
"lock": false
6468
}
6569
}

phpcs.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
<!-- For help understanding the `testVersion` configuration setting:
4040
https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
41-
<config name="testVersion" value="5.6-"/>
41+
<config name="testVersion" value="7.2-"/>
4242

4343
<!-- Verify that everything in the global namespace is either namespaced or prefixed.
4444
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace -->

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+
- dist-archive-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/Dist_Archive_Command.php

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function __invoke( $args, $assoc_args ) {
7777
$this->checker = new GitIgnoreChecker( $source_dir_path, '.distignore' );
7878
$dist_ignore_filepath = $source_dir_path . '/.distignore';
7979
if ( file_exists( $dist_ignore_filepath ) ) {
80-
$file_ignore_rules = explode( PHP_EOL, file_get_contents( $dist_ignore_filepath ) );
80+
$file_ignore_rules = explode( PHP_EOL, (string) file_get_contents( $dist_ignore_filepath ) );
8181
} else {
8282
WP_CLI::warning( 'No .distignore file found. All files in directory included in archive.' );
8383
$file_ignore_rules = [];
@@ -124,6 +124,8 @@ public function __invoke( $args, $assoc_args ) {
124124

125125
chdir( dirname( $source_path ) );
126126

127+
$cmd = "zip -r '{$archive_absolute_filepath}' {$archive_output_dir_name}";
128+
127129
// If the files are being zipped in place, we need the exclusion rules.
128130
// whereas if they were copied for any reasons above, the rules have already been applied.
129131
if ( $source_path !== $source_dir_path || empty( $file_ignore_rules ) ) {
@@ -175,10 +177,14 @@ function ( $ignored_file ) use ( $source_path ) {
175177
$escape_whitelist = 'targz' === $assoc_args['format'] ? array( '^', '*' ) : array();
176178
WP_CLI::debug( "Running: {$cmd}", 'dist-archive' );
177179
$escaped_shell_command = $this->escapeshellcmd( $cmd, $escape_whitelist );
178-
$ret = WP_CLI::launch( $escaped_shell_command, false, true );
180+
181+
/**
182+
* @var WP_CLI\ProcessRun $ret
183+
*/
184+
$ret = WP_CLI::launch( $escaped_shell_command, false, true );
179185
if ( 0 === $ret->return_code ) {
180186
$filename = pathinfo( $archive_absolute_filepath, PATHINFO_BASENAME );
181-
$file_size = $this->get_size_format( filesize( $archive_absolute_filepath ), 2 );
187+
$file_size = $this->get_size_format( (int) filesize( $archive_absolute_filepath ), 2 );
182188

183189
WP_CLI::success( "Created {$filename} (Size: {$file_size})" );
184190
} else {
@@ -201,7 +207,7 @@ function ( $ignored_file ) use ( $source_path ) {
201207
private function get_file_paths_and_names( $args, $assoc_args ) {
202208

203209
$source_dir_path = realpath( $args[0] );
204-
if ( ! is_dir( $source_dir_path ) ) {
210+
if ( ! $source_dir_path || ! is_dir( $source_dir_path ) ) {
205211
WP_CLI::error( 'Provided input path is not a directory.' );
206212
}
207213

@@ -239,7 +245,7 @@ private function get_file_paths_and_names( $args, $assoc_args ) {
239245

240246
$destination_dir_path = realpath( $destination_dir_path );
241247

242-
if ( ! is_dir( $destination_dir_path ) ) {
248+
if ( ! $destination_dir_path || ! is_dir( $destination_dir_path ) ) {
243249
WP_CLI::error( "Target directory does not exist: {$destination_dir_path}" );
244250
}
245251

@@ -285,33 +291,42 @@ private function get_version( $source_dir_path ) {
285291
* @link https://developer.wordpress.org/reference/functions/get_file_data/
286292
*/
287293
if ( file_exists( $source_dir_path . '/style.css' ) ) {
288-
$contents = file_get_contents( $source_dir_path . '/style.css', false, null, 0, 5000 );
294+
$contents = (string) file_get_contents( $source_dir_path . '/style.css', false, null, 0, 5000 );
289295
$contents = str_replace( "\r", "\n", $contents );
290296
$pattern = '/^' . preg_quote( 'Version', ',' ) . ':(.*)$/mi';
291297
if ( preg_match( $pattern, $contents, $match ) && $match[1] ) {
292-
$version = trim( preg_replace( '/\s*(?:\*\/|\?>).*/', '', $match[1] ) );
298+
$version = trim( (string) preg_replace( '/\s*(?:\*\/|\?>).*/', '', $match[1] ) );
293299
}
294300
}
295301

296302
if ( empty( $version ) ) {
297-
foreach ( glob( $source_dir_path . '/*.php' ) as $php_file ) {
298-
$contents = file_get_contents( $php_file, false, null, 0, 5000 );
299-
$version = $this->get_version_in_code( $contents );
300-
if ( ! empty( $version ) ) {
301-
$version = trim( $version );
303+
foreach ( (array) glob( $source_dir_path . '/*.php' ) as $php_file ) {
304+
if ( ! $php_file ) {
305+
continue;
306+
}
307+
$contents = (string) file_get_contents( $php_file, false, null, 0, 5000 );
308+
$ver = $this->get_version_in_code( $contents );
309+
if ( ! empty( $ver ) ) {
310+
$version = trim( $ver );
302311
break;
303312
}
304313
}
305314
}
306315

307316
if ( empty( $version ) && file_exists( $source_dir_path . '/composer.json' ) ) {
308-
$composer_obj = json_decode( file_get_contents( $source_dir_path . '/composer.json' ) );
309-
if ( ! empty( $composer_obj->version ) ) {
317+
/**
318+
* @var null|object{version?: string} $composer_obj
319+
*/
320+
$composer_obj = json_decode( (string) file_get_contents( $source_dir_path . '/composer.json' ) );
321+
if ( $composer_obj && ! empty( $composer_obj->version ) ) {
310322
$version = trim( $composer_obj->version );
311323
}
312324
}
313325

314326
if ( ! empty( $version ) && false !== stripos( $version, '-alpha' ) && is_dir( $source_dir_path . '/.git' ) ) {
327+
/**
328+
* @var WP_CLI\ProcessRun $response
329+
*/
315330
$response = WP_CLI::launch( "cd {$source_dir_path}; git log --pretty=format:'%h' -n 1", false, true );
316331
$maybe_hash = trim( $response->stdout );
317332
if ( $maybe_hash && 7 === strlen( $maybe_hash ) ) {
@@ -387,7 +402,7 @@ private function get_version_in_docblock( $docblock ) {
387402
* @see https://github.com/phpactor/docblock/blob/master/lib/Parser.php
388403
*
389404
* @param string $docblock Docblock to parse.
390-
* @return array Associative array of parsed data.
405+
* @return array<string, string> Associative array of parsed data.
391406
*/
392407
private function parse_doc_block( $docblock ) {
393408
$tag_documentor = '{@([a-zA-Z0-9-_\\\]+)\s*?(.*)?}';
@@ -402,7 +417,7 @@ private function parse_doc_block( $docblock ) {
402417
}
403418
}
404419

405-
$tag_name = strtolower( $matches[1] );
420+
$tag_name = trim( isset( $matches[1] ) ? strtolower( $matches[1] ) : '' );
406421
$metadata = trim( isset( $matches[2] ) ? $matches[2] : '' );
407422

408423
$tags[ $tag_name ] = $metadata;
@@ -456,7 +471,6 @@ protected function is_path_contains_symlink( $source_dir_path ) {
456471
);
457472

458473
/**
459-
* @var RecursiveIteratorIterator $iterator
460474
* @var SplFileInfo $item
461475
*/
462476
foreach ( $iterator as $item ) {
@@ -487,7 +501,6 @@ private function get_file_list( $source_dir_path, $excluded = false ) {
487501
);
488502

489503
/**
490-
* @var RecursiveIteratorIterator $iterator
491504
* @var SplFileInfo $item
492505
*/
493506
foreach ( $iterator as $item ) {
@@ -499,7 +512,7 @@ private function get_file_list( $source_dir_path, $excluded = false ) {
499512
$included_files[] = $relative_filepath;
500513
}
501514
} catch ( \Inmarelibero\GitIgnoreChecker\Exception\InvalidArgumentException $exception ) {
502-
if ( $item->isLink() && ! file_exists( readlink( $item->getPathname() ) ) ) {
515+
if ( $item->isLink() && ! file_exists( (string) readlink( $item->getPathname() ) ) ) {
503516
WP_CLI::error( "Broken symlink at {$relative_filepath}. Target missing at {$item->getLinkTarget()}." );
504517
} else {
505518
WP_CLI::error( $exception->getMessage() );

0 commit comments

Comments
 (0)