Skip to content

Commit 00a9caf

Browse files
committed
Satisfy PHPCS, PHPStan and the spell checker
Three separate code quality failures on the new build scripts: * PHPStan runs at level 9, where every offset read off `json_decode()` output is `mixed`. Narrow the decoded `installed.json` explicitly and build the packages list in a local variable instead of writing back through nested offsets. * Align the scoper config's array arrows on the longest key. * `marc-mabe` is a vendor name, not a misspelling of "maybe"; mark those two lines with the `spellchecker:disable-line` annotation the repo's `.typos.toml` already recognises. Re-ran the prefixing end to end against a scratch tree after the PHPStan refactor: 30 packages rewritten, autoloader regenerated, verification still passes. Refs wp-cli/wp-cli#5920 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RHtjyXkZh8X16sBgmidSQi
1 parent d296118 commit 00a9caf

2 files changed

Lines changed: 44 additions & 23 deletions

File tree

‎utils/scope-dependencies.php‎

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
const SCOPED_VENDOR_DIRS = [
2929
'composer',
3030
'justinrainbow',
31-
'marc-mabe',
31+
'marc-mabe', // spellchecker:disable-line
3232
'psr',
3333
'react',
3434
'seld',
@@ -160,22 +160,25 @@ function fail( string $message ): void {
160160
fail( sprintf( "Could not find '%s'.", $installed_json ) );
161161
}
162162

163-
$installed = json_decode( (string) file_get_contents( $installed_json ), true );
163+
$decoded = json_decode( (string) file_get_contents( $installed_json ), true );
164164

165-
if ( ! is_array( $installed ) || ! isset( $installed['packages'] ) ) {
165+
if ( ! is_array( $decoded ) || ! isset( $decoded['packages'] ) || ! is_array( $decoded['packages'] ) ) {
166166
fail( sprintf( "Could not decode '%s'.", $installed_json ) );
167167
}
168168

169-
$patched = 0;
170-
171-
foreach ( $installed['packages'] as $index => $package ) {
172-
$name = $package['name'] ?? '';
169+
/**
170+
* @var array<string, mixed> $decoded
171+
* @var array<int|string, mixed> $packages
172+
*/
173+
$packages = $decoded['packages'];
174+
$patched = 0;
173175

174-
if ( '' === $name ) {
176+
foreach ( $packages as $index => $package ) {
177+
if ( ! is_array( $package ) || ! isset( $package['name'] ) || ! is_string( $package['name'] ) ) {
175178
continue;
176179
}
177180

178-
$vendor_name = explode( '/', $name )[0];
181+
$vendor_name = explode( '/', $package['name'] )[0];
179182

180183
if ( ! in_array( $vendor_name, SCOPED_VENDOR_DIRS, true ) ) {
181184
continue;
@@ -185,31 +188,49 @@ function fail( string $message ): void {
185188
continue;
186189
}
187190

188-
$roots = [];
191+
$autoload = $package['autoload'];
192+
$roots = [];
189193

190194
foreach ( [ 'psr-4', 'psr-0' ] as $standard ) {
191-
foreach ( (array) ( $package['autoload'][ $standard ] ?? [] ) as $paths ) {
195+
$rules = $autoload[ $standard ] ?? [];
196+
197+
if ( ! is_array( $rules ) ) {
198+
continue;
199+
}
200+
201+
foreach ( $rules as $paths ) {
192202
foreach ( (array) $paths as $path ) {
193-
$roots[] = '' === $path ? '.' : $path;
203+
if ( is_string( $path ) ) {
204+
$roots[] = '' === $path ? '.' : $path;
205+
}
194206
}
195207
}
196208
}
197209

198-
foreach ( (array) ( $package['autoload']['classmap'] ?? [] ) as $path ) {
199-
$roots[] = $path;
210+
$classmap_rules = $autoload['classmap'] ?? [];
211+
212+
if ( is_array( $classmap_rules ) ) {
213+
foreach ( $classmap_rules as $path ) {
214+
if ( is_string( $path ) ) {
215+
$roots[] = $path;
216+
}
217+
}
200218
}
201219

202220
if ( ! $roots ) {
203221
continue;
204222
}
205223

206-
$installed['packages'][ $index ]['autoload'] = [ 'classmap' => array_values( array_unique( $roots ) ) ];
224+
$package['autoload'] = [ 'classmap' => array_values( array_unique( $roots ) ) ];
225+
$packages[ $index ] = $package;
207226
++$patched;
208227
}
209228

229+
$decoded['packages'] = $packages;
230+
210231
report( sprintf( 'Rewrote autoload rules for %d prefixed package(s).', $patched ) );
211232

212-
$encoded = json_encode( $installed, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
233+
$encoded = json_encode( $decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
213234

214235
if ( false === $encoded || false === file_put_contents( $installed_json, $encoded ) ) {
215236
fail( sprintf( "Failed to write '%s'.", $installed_json ) );

‎utils/scoper/scoper.inc.php‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static function ( $relative ) use ( $vendor_dir ) {
5959
[
6060
'composer',
6161
'justinrainbow',
62-
'marc-mabe',
62+
'marc-mabe', // spellchecker:disable-line
6363
'psr',
6464
'react',
6565
'seld',
@@ -70,8 +70,8 @@ static function ( $relative ) use ( $vendor_dir ) {
7070
);
7171

7272
return [
73-
'prefix' => 'WP_CLI\\Vendor',
74-
'finders' => [
73+
'prefix' => 'WP_CLI\\Vendor',
74+
'finders' => [
7575
/*
7676
* Deliberately without exclusions. The prefixed output is merged back
7777
* over `vendor/` rather than replacing it, because php-scoper only
@@ -99,11 +99,11 @@ static function ( $relative ) use ( $vendor_dir ) {
9999
'Composer',
100100
],
101101

102-
'exclude-classes' => [],
103-
'exclude-functions' => [],
104-
'exclude-constants' => [],
102+
'exclude-classes' => [],
103+
'exclude-functions' => [],
104+
'exclude-constants' => [],
105105

106-
'patchers' => [
106+
'patchers' => [
107107
/*
108108
* Excluding a namespace stops php-scoper prefixing its declarations,
109109
* but not string literals that name classes inside it. Composer passes

0 commit comments

Comments
 (0)