Skip to content

Commit 1446020

Browse files
authored
Merge pull request #220 from wp-cli/fix-phpstan
2 parents e0ecaa4 + f2022c6 commit 1446020

13 files changed

Lines changed: 162 additions & 161 deletions

File tree

.typos.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ extend-ignore-re = [
77

88
[files]
99
extend-exclude = [
10-
"tests/JsonManipulatorTest.php"
10+
"tests/phpunit/JsonManipulatorTest.php"
1111
]

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
],
1414
"require": {
1515
"ext-json": "*",
16-
"composer/composer": "^2.2.25",
17-
"wp-cli/wp-cli": "^2.12"
16+
"composer/composer": "^2.9.5",
17+
"wp-cli/wp-cli": "^2.13"
1818
},
1919
"require-dev": {
2020
"wp-cli/scaffold-command": "^1 || ^2",

phpcs.xml.dist

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<exclude-pattern>*/src/WP_CLI/JsonManipulator\.php$</exclude-pattern>
1919
<exclude-pattern>*/src/WP_CLI/Package/Compat/Min_Composer_1_10/NullIOMethodsTrait\.php$</exclude-pattern>
2020
<exclude-pattern>*/src/WP_CLI/Package/Compat/Min_Composer_2_3/NullIOMethodsTrait\.php$</exclude-pattern>
21-
<exclude-pattern>*/tests/JsonManipulatorTest\.php$</exclude-pattern>
21+
<exclude-pattern>*/tests/phpunit/JsonManipulatorTest\.php$</exclude-pattern>
2222

2323
<!-- Show progress. -->
2424
<arg value="p"/>
@@ -63,4 +63,5 @@
6363
<exclude-pattern>*/src/Package_Command\.php$</exclude-pattern>
6464
</rule>
6565

66+
<exclude-pattern>*/tests/phpstan/scan-files\.php$</exclude-pattern>
6667
</ruleset>

phpstan.neon.dist

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
parameters:
2-
level: 1
2+
level: 5
33
paths:
44
- src
55
- package-command.php
66
scanDirectories:
77
- vendor/wp-cli/wp-cli/php
88
scanFiles:
99
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
10+
- tests/phpstan/scan-files.php
1011
treatPhpDocTypesAsCertain: false
1112
ignoreErrors:
1213
# WP_CLI_VERSION is defined in wp-cli core at runtime

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
colors="true"
1414
verbose="true">
1515
<testsuite name="wp-cli/package-command tests">
16-
<directory suffix="Test.php">tests</directory>
16+
<directory suffix="Test.php">tests/phpunit</directory>
1717
</testsuite>
1818

1919
<filter>

src/Package_Command.php

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use Composer\Composer;
44
use Composer\Config;
55
use Composer\Config\JsonConfigSource;
6-
use Composer\DependencyResolver\Pool;
76
use Composer\Factory;
87
use Composer\IO\NullIO;
98
use Composer\Installer;
@@ -223,9 +222,7 @@ public function install( $args, $assoc_args ) {
223222
$dir_package = false;
224223
$version = '';
225224
if ( $this->is_git_repository( $package_name ) ) {
226-
if ( '' === $version ) {
227-
$version = "dev-{$this->get_github_default_branch( $package_name, $insecure )}";
228-
}
225+
$version = "dev-{$this->get_github_default_branch( $package_name, $insecure )}";
229226
$git_package = $package_name;
230227
$matches = [];
231228
if ( preg_match( '#([^:\/]+\/[^\/]+)\.git#', $package_name, $matches ) ) {
@@ -247,7 +244,7 @@ public function install( $args, $assoc_args ) {
247244
$gitlab_token = getenv( 'GITLAB_TOKEN' ); // Use GITLAB_TOKEN if available to avoid authorization failures or rate-limiting.
248245
$headers = $gitlab_token && strpos( $package_name, '://gitlab.com/' ) !== false ? [ 'PRIVATE-TOKEN' => $gitlab_token ] : [];
249246
$response = Utils\http_request( 'GET', $package_name, null, $headers, $options );
250-
if ( 20 !== (int) substr( $response->status_code, 0, 2 ) ) {
247+
if ( 20 !== (int) substr( (string) $response->status_code, 0, 2 ) ) {
251248
@unlink( $temp ); // @codingStandardsIgnoreLine
252249
WP_CLI::error( sprintf( "Couldn't download package from '%s' (HTTP code %d).", $package_name, $response->status_code ) );
253250
}
@@ -316,11 +313,9 @@ public function install( $args, $assoc_args ) {
316313
}
317314
}
318315

319-
if ( $this->is_composer_v2() ) {
320-
$package_name = function_exists( 'mb_strtolower' )
321-
? mb_strtolower( $package_name )
322-
: strtolower( $package_name );
323-
}
316+
$package_name = function_exists( 'mb_strtolower' )
317+
? mb_strtolower( $package_name )
318+
: strtolower( $package_name );
324319

325320
if ( '' === $version ) {
326321
$version = self::DEFAULT_DEV_BRANCH_CONSTRAINTS;
@@ -822,8 +817,8 @@ private function package_index() {
822817
* Displays a set of packages
823818
*
824819
* @param string $context
825-
* @param array
826-
* @param array
820+
* @param array $packages
821+
* @param array $assoc_args
827822
*/
828823
private function show_packages( $context, $packages, $assoc_args ) {
829824
$default_fields = [];
@@ -866,7 +861,7 @@ private function show_packages( $context, $packages, $assoc_args ) {
866861
$update_version = '';
867862
if ( 'list' === $context && ! $skip_update_check ) {
868863
try {
869-
$latest = $this->find_latest_package( $package, $composer, null );
864+
$latest = $this->find_latest_package( $package, $composer );
870865
if ( $latest && $latest->getFullPrettyVersion() !== $package->getFullPrettyVersion() ) {
871866
$update = 'available';
872867
$update_version = $latest->getPrettyVersion();
@@ -929,7 +924,7 @@ private function get_package_by_shortened_identifier( $package_name, $insecure =
929924
// Check if the package exists on Packagist.
930925
$url = "https://repo.packagist.org/p2/{$package_name}.json";
931926
$response = Utils\http_request( 'GET', $url, null, [], $options );
932-
if ( 20 === (int) substr( $response->status_code, 0, 2 ) ) {
927+
if ( 20 === (int) substr( (string) $response->status_code, 0, 2 ) ) {
933928
return $package_name;
934929
}
935930

@@ -938,7 +933,7 @@ private function get_package_by_shortened_identifier( $package_name, $insecure =
938933
$github_token = getenv( 'GITHUB_TOKEN' ); // Use GITHUB_TOKEN if available to avoid authorization failures or rate-limiting.
939934
$headers = $github_token ? [ 'Authorization' => 'token ' . $github_token ] : [];
940935
$response = Utils\http_request( 'GET', $url, null /*data*/, $headers, $options );
941-
if ( 20 === (int) substr( $response->status_code, 0, 2 ) ) {
936+
if ( 20 === (int) substr( (string) $response->status_code, 0, 2 ) ) {
942937
return $url;
943938
}
944939

@@ -948,7 +943,7 @@ private function get_package_by_shortened_identifier( $package_name, $insecure =
948943
$headers = $github_token ? [ 'Authorization' => 'token ' . $github_token ] : [];
949944
$headers = $gitlab_token && strpos( $package_name, '://gitlab.com/' ) !== false ? [ 'PRIVATE-TOKEN' => $gitlab_token ] : [];
950945
$response = Utils\http_request( 'GET', $url, null /*data*/, $headers, $options );
951-
if ( 20 === (int) substr( $response->status_code, 0, 2 ) ) {
946+
if ( 20 === (int) substr( (string) $response->status_code, 0, 2 ) ) {
952947
return $url;
953948
}
954949

@@ -976,9 +971,6 @@ private function get_installed_packages() {
976971
if ( in_array( $package->getPrettyName(), $installed_package_keys, true ) ) {
977972
$installed_packages[] = $package;
978973
} elseif ( false !== $idx ) { // Legacy incorrect name check.
979-
if ( ! $this->is_composer_v2() ) {
980-
WP_CLI::warning( sprintf( "Found package '%s' misnamed '%s' in '%s'.", $package->getPrettyName(), $installed_package_keys[ $idx ], $this->get_composer_json_path() ) );
981-
}
982974
$installed_packages[] = $package;
983975
}
984976
}
@@ -1130,12 +1122,11 @@ private function create_default_composer_json( $composer_path ) {
11301122
*
11311123
* @param PackageInterface $package
11321124
* @param Composer $composer
1133-
* @param string $phpVersion
1134-
* @param bool $minorOnly
1125+
* @param bool $minor_only
11351126
*
1136-
* @return PackageInterface|null
1127+
* @return PackageInterface|false
11371128
*/
1138-
private function find_latest_package( PackageInterface $package, Composer $composer, $php_version, $minor_only = false ) {
1129+
private function find_latest_package( PackageInterface $package, Composer $composer, $minor_only = false ) {
11391130
// Find the latest version allowed in this pool/repository set.
11401131
$name = $package->getPrettyName();
11411132
$version_selector = $this->get_version_selector( $composer );
@@ -1156,30 +1147,20 @@ private function find_latest_package( PackageInterface $package, Composer $compo
11561147
$target_version = '^' . $package->getVersion();
11571148
}
11581149

1159-
if ( $this->is_composer_v2() ) {
1160-
return $version_selector->findBestCandidate( $name, $target_version, $best_stability );
1161-
}
1162-
1163-
return $version_selector->findBestCandidate( $name, $target_version, $php_version, $best_stability );
1150+
return $version_selector->findBestCandidate( $name, $target_version, $best_stability );
11641151
}
11651152

11661153
/**
11671154
* @return VersionSelector
11681155
*/
11691156
private function get_version_selector( Composer $composer ) {
11701157
if ( ! $this->version_selector ) {
1171-
if ( $this->is_composer_v2() ) {
1172-
$repository_set = new Repository\RepositorySet(
1173-
$composer->getPackage()->getMinimumStability(),
1174-
$composer->getPackage()->getStabilityFlags()
1175-
);
1176-
$repository_set->addRepository( new CompositeRepository( $composer->getRepositoryManager()->getRepositories() ) );
1177-
$this->version_selector = new VersionSelector( $repository_set );
1178-
} else {
1179-
$pool = new Pool( $composer->getPackage()->getMinimumStability(), $composer->getPackage()->getStabilityFlags() );
1180-
$pool->addRepository( new CompositeRepository( $composer->getRepositoryManager()->getRepositories() ) );
1181-
$this->version_selector = new VersionSelector( $pool );
1182-
}
1158+
$repository_set = new Repository\RepositorySet(
1159+
$composer->getPackage()->getMinimumStability(),
1160+
$composer->getPackage()->getStabilityFlags()
1161+
);
1162+
$repository_set->addRepository( new CompositeRepository( $composer->getRepositoryManager()->getRepositories() ) );
1163+
$this->version_selector = new VersionSelector( $repository_set );
11831164
}
11841165

11851166
return $this->version_selector;
@@ -1213,7 +1194,7 @@ private function check_github_package_name( $package_name, $version = '', $insec
12131194
$raw_content_url = "https://raw.githubusercontent.com/{$package_name}/{$this->get_raw_git_version( $version )}/composer.json";
12141195

12151196
$response = Utils\http_request( 'GET', $raw_content_url, null /*data*/, $headers, $options );
1216-
if ( 20 !== (int) substr( $response->status_code, 0, 2 ) ) {
1197+
if ( 20 !== (int) substr( (string) $response->status_code, 0, 2 ) ) {
12171198
// Could not get composer.json. Possibly private so warn and return best guess from input (always xxx/xxx).
12181199
WP_CLI::warning(
12191200
sprintf(
@@ -1360,7 +1341,7 @@ private function get_github_latest_release_tag( $package_name, $insecure ) {
13601341
$url = "https://api.github.com/repos/{$package_name}/releases/latest";
13611342
$options = [ 'insecure' => $insecure ];
13621343
$response = Utils\http_request( 'GET', $url, null, [], $options );
1363-
if ( 20 !== (int) substr( $response->status_code, 0, 2 ) ) {
1344+
if ( 20 !== (int) substr( (string) $response->status_code, 0, 2 ) ) {
13641345
WP_CLI::warning( 'Could not guess stable version from GitHub repository, falling back to master branch' );
13651346
return 'master';
13661347
}
@@ -1500,15 +1481,6 @@ static function () use (
15001481
);
15011482
}
15021483

1503-
/**
1504-
* Check whether we are dealing with Composer version 2.0.0+.
1505-
*
1506-
* @return bool
1507-
*/
1508-
private function is_composer_v2() {
1509-
return version_compare( Composer::getVersion(), '2.0.0', '>=' );
1510-
}
1511-
15121484
/**
15131485
* Try to retrieve default branch via GitHub API.
15141486
*
@@ -1529,7 +1501,7 @@ private function get_github_default_branch( $package_name, $insecure = false ) {
15291501

15301502
$github_api_repo_url = "https://api.github.com/repos/{$package_name}";
15311503
$response = Utils\http_request( 'GET', $github_api_repo_url, null /*data*/, $headers, $options );
1532-
if ( 20 !== (int) substr( $response->status_code, 0, 2 ) ) {
1504+
if ( 20 !== (int) substr( (string) $response->status_code, 0, 2 ) ) {
15331505
WP_CLI::warning(
15341506
sprintf(
15351507
"Couldn't fetch default branch for package '%s' (HTTP code %d). Presuming default branch is 'master'.",

src/WP_CLI/Package/Compat/Min_Composer_1_10/NullIOMethodsTrait.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/WP_CLI/Package/Compat/Min_Composer_2_3/NullIOMethodsTrait.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/WP_CLI/Package/Compat/NullIOMethodsTrait.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/WP_CLI/Package/ComposerIO.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,35 @@
1111
namespace WP_CLI\Package;
1212

1313
use Composer\IO\NullIO;
14-
use WP_CLI\Package\Compat\NullIOMethodsTrait;
14+
use WP_CLI;
1515

1616
class ComposerIO extends NullIO {
17-
use NullIOMethodsTrait;
17+
/**
18+
* {@inheritDoc}
19+
*/
20+
public function isVerbose(): bool {
21+
return true;
22+
}
23+
24+
/**
25+
* {@inheritDoc}
26+
*/
27+
public function write( $messages, bool $newline = true, int $verbosity = self::NORMAL ): void {
28+
self::output_clean_message( $messages );
29+
}
30+
31+
/**
32+
* {@inheritDoc}
33+
*/
34+
public function writeError( $messages, bool $newline = true, int $verbosity = self::NORMAL ): void {
35+
self::output_clean_message( $messages );
36+
}
37+
38+
private static function output_clean_message( $messages ) {
39+
$messages = (array) preg_replace( '#<(https?)([^>]+)>#', '$1$2', $messages );
40+
foreach ( $messages as $message ) {
41+
// phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags
42+
WP_CLI::log( strip_tags( trim( $message ) ) );
43+
}
44+
}
1845
}

0 commit comments

Comments
 (0)