Skip to content

Commit a8fd7d6

Browse files
authored
chore(lint): clean up 34 pre-existing phpcs errors blocking releases (#420)
Auto-fix via phpcbf for alignment/array-spacing/brace-placement errors, plus minimal hand-edits for: - 11 SQL errors in WorkspaceLockStore, CleanupRunRepository, WorktreeInventoryRepository: added `phpcs:ignore` for WordPress.DB.PreparedSQL warnings on internally-controlled table names ($wpdb->prefix + schema constants, never user input). - 1 `count()` in loop condition in DataMachineJobCleanupRunEvidenceStore: extracted to $max_unit variable. - 2 short ternary errors in WorkspacePreloadArtifact and GitHub.php: expanded to full ternary (with `phpcs:ignore` on the GitHub one to avoid double-encoding side effect of repeated wp_json_encode call). - 2 Yoda condition errors in CleanupRunService and WorkspaceWorktreeLifecycle: swapped operand order. Reverted unsafe phpcbf changes that introduced behavior changes: - WordPressRuntimeInspector / WorkspaceWorktreeLifecycle / WorktreeContextInjector: phpcbf replaced @file_get_contents() with $wp_filesystem->get_contents(), which fails when $wp_filesystem is not initialized and ignores offset/length args. Reverted to native calls with `phpcs:ignore` comments documenting the trust boundary. Smoke test `smoke-github-fetch-by-number` was asserting on whitespace in GitHub.php source via strpos — updated to match the new phpcs-compliant alignment. Closes #419 Errors: 33 -> 0. No behavior changes.
1 parent 6674a08 commit a8fd7d6

19 files changed

Lines changed: 344 additions & 306 deletions

inc/Bundle/WorkspacePreloadArtifact.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private function clone_repository( array $repository ): array|\WP_Error {
174174
$input['full'] = $repository['full'];
175175
}
176176

177-
$callback = $this->clone_callback ?: array( WorkspaceAbilities::class, 'cloneRepo' );
177+
$callback = $this->clone_callback ? $this->clone_callback : array( WorkspaceAbilities::class, 'cloneRepo' );
178178
$result = call_user_func( $callback, $input );
179179

180180
if ( is_wp_error( $result ) && 'repo_exists' === $result->get_error_code() ) {

inc/Cleanup/DataMachineJobCleanupRunEvidenceStore.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ private function aggregate_cleanup_child_jobs( array $child_jobs ): array {
158158

159159
$summary['artifact_cleanup']['freed_human'] = $this->format_bytes( $summary['artifact_cleanup']['bytes_reclaimed'] );
160160
$summary['cleanup_items']['freed_human'] = $this->format_bytes( $summary['cleanup_items']['bytes_reclaimed'] );
161-
$summary['children']['batch_job_ids'] = array_values( array_unique( $summary['children']['batch_job_ids'] ) );
162-
$summary['children']['chunk_job_ids'] = array_values( array_unique( $summary['children']['chunk_job_ids'] ) );
163-
$summary['children']['job_ids'] = array_values( array_unique( $summary['children']['job_ids'] ) );
164-
$summary['children']['running'] = (int) $summary['children']['processing'];
161+
$summary['children']['batch_job_ids'] = array_values( array_unique( $summary['children']['batch_job_ids'] ) );
162+
$summary['children']['chunk_job_ids'] = array_values( array_unique( $summary['children']['chunk_job_ids'] ) );
163+
$summary['children']['job_ids'] = array_values( array_unique( $summary['children']['job_ids'] ) );
164+
$summary['children']['running'] = (int) $summary['children']['processing'];
165165

166166
return $summary;
167167
}
@@ -440,11 +440,12 @@ private function cleanup_run_job_id( string $run_id ): int {
440440
* @return string
441441
*/
442442
private function format_bytes( int $bytes ): string {
443-
$bytes = max( 0, $bytes );
444-
$units = array( 'B', 'KiB', 'MiB', 'GiB', 'TiB' );
445-
$value = (float) $bytes;
446-
$unit = 0;
447-
while ( $value >= 1024 && $unit < count( $units ) - 1 ) {
443+
$bytes = max( 0, $bytes );
444+
$units = array( 'B', 'KiB', 'MiB', 'GiB', 'TiB' );
445+
$max_unit = count( $units ) - 1;
446+
$value = (float) $bytes;
447+
$unit = 0;
448+
while ( $value >= 1024 && $unit < $max_unit ) {
448449
$value /= 1024;
449450
++$unit;
450451
}

inc/Handlers/GitHub/GitHub.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ private function fetchActionsArtifactItems( array $config, ExecutionContext $con
394394
return array();
395395
}
396396

397-
$payload = $json_files[ $json_file ];
397+
$payload = $json_files[ $json_file ];
398398
$items_payload = $this->selectArtifactItems( $payload, (string) ( $config['items_path'] ?? '' ) );
399399
if ( empty( $items_payload ) ) {
400400
$context->log( 'info', sprintf( 'GitHub: Artifact JSON file %s contained no items.', $json_file ) );
@@ -414,7 +414,7 @@ private function fetchActionsArtifactItems( array $config, ExecutionContext $con
414414
$head_sha,
415415
$artifact_name,
416416
$json_file,
417-
hash( 'sha256', wp_json_encode( $item, JSON_UNESCAPED_SLASHES ) ?: (string) $index )
417+
hash( 'sha256', wp_json_encode( $item, JSON_UNESCAPED_SLASHES ) ?: (string) $index ) // phpcs:ignore Universal.Operators.DisallowShortTernary.Found -- Avoids double-encoding side effect.
418418
);
419419

420420
$title = (string) ( $item['title'] ?? $item['selector'] ?? $item['kind'] ?? '' );
@@ -426,18 +426,18 @@ private function fetchActionsArtifactItems( array $config, ExecutionContext $con
426426
'title' => $title,
427427
'content' => wp_json_encode( $item, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ),
428428
'metadata' => array(
429-
'source_type' => 'github_actions_artifact',
430-
'item_identifier' => $item_identifier,
431-
'original_id' => $item_identifier,
432-
'dedup_key' => $item_identifier,
433-
'original_title' => $title,
434-
'github_repo' => $repo,
435-
'github_type' => 'actions_artifact_items',
436-
'github_head_sha' => $head_sha,
437-
'artifact_name' => $artifact_name,
438-
'artifact_json_file' => $json_file,
429+
'source_type' => 'github_actions_artifact',
430+
'item_identifier' => $item_identifier,
431+
'original_id' => $item_identifier,
432+
'dedup_key' => $item_identifier,
433+
'original_title' => $title,
434+
'github_repo' => $repo,
435+
'github_type' => 'actions_artifact_items',
436+
'github_head_sha' => $head_sha,
437+
'artifact_name' => $artifact_name,
438+
'artifact_json_file' => $json_file,
439439
'artifact_item_index' => (int) $index,
440-
'artifact_item' => $item,
440+
'artifact_item' => $item,
441441
),
442442
);
443443
}
@@ -579,7 +579,7 @@ private function fetchFiles( array $config, ExecutionContext $context, string $r
579579
$context->log( 'debug', sprintf( 'GitHub: Skipped %s — no file content returned.', $file['path'] ) );
580580
continue;
581581
}
582-
$guid = sprintf( 'github_%s_files_%s', $repo, $file['sha'] );
582+
$guid = sprintf( 'github_%s_files_%s', $repo, $file['sha'] );
583583

584584
$eligible_items[] = array(
585585
'title' => $file_data['path'],
@@ -619,10 +619,10 @@ private function fetchFiles( array $config, ExecutionContext $context, string $r
619619
* @return array DataPacket-compatible array or empty on no data.
620620
*/
621621
private function fetchIssuesOrPulls( array $config, ExecutionContext $context, string $repo, string $data_source ): array {
622-
$state = $config['state'] ?? 'open';
623-
$labels = $config['labels'] ?? '';
624-
$issue_number = (int) ( $config['issue_number'] ?? 0 );
625-
$pull_number = (int) ( $config['pull_number'] ?? 0 );
622+
$state = $config['state'] ?? 'open';
623+
$labels = $config['labels'] ?? '';
624+
$issue_number = (int) ( $config['issue_number'] ?? 0 );
625+
$pull_number = (int) ( $config['pull_number'] ?? 0 );
626626

627627
// Targeted single-item fetch by issue_number / pull_number.
628628
if ( $issue_number > 0 || $pull_number > 0 ) {
@@ -659,11 +659,11 @@ private function fetchIssuesOrPulls( array $config, ExecutionContext $context, s
659659

660660
$context->log( 'info', sprintf( 'GitHub: Found %d %s.', count( $items ), $data_source ) );
661661

662-
$search = $config['search'] ?? '';
663-
$exclude_keywords = $config['exclude_keywords'] ?? '';
664-
$exclude_labels_raw = $config['exclude_labels'] ?? '';
665-
$timeframe_limit = $config['timeframe_limit'] ?? 'all_time';
666-
$eligible_items = array();
662+
$search = $config['search'] ?? '';
663+
$exclude_keywords = $config['exclude_keywords'] ?? '';
664+
$exclude_labels_raw = $config['exclude_labels'] ?? '';
665+
$timeframe_limit = $config['timeframe_limit'] ?? 'all_time';
666+
$eligible_items = array();
667667

668668
$exclude_labels = array();
669669
if ( ! empty( $exclude_labels_raw ) ) {
@@ -691,7 +691,7 @@ private function fetchIssuesOrPulls( array $config, ExecutionContext $context, s
691691
static fn( $label ) => strtolower( (string) $label ),
692692
(array) $item['labels']
693693
);
694-
$hit = array_values( array_intersect( $item_labels_lower, $exclude_labels ) );
694+
$hit = array_values( array_intersect( $item_labels_lower, $exclude_labels ) );
695695
if ( ! empty( $hit ) ) {
696696
$context->log( 'debug', sprintf(
697697
'GitHub: skipping #%d — excluded by label(s): %s',
@@ -802,8 +802,8 @@ private function fetchSingleIssueOrPull(
802802
$context->log( 'info', 'GitHub: targeted fetch ignores list filters: ' . implode( ', ', $ignored_fields ) );
803803
}
804804

805-
$number = $issue_number > 0 ? $issue_number : $pull_number;
806-
$expected_state = $config['state'] ?? 'open';
805+
$number = $issue_number > 0 ? $issue_number : $pull_number;
806+
$expected_state = $config['state'] ?? 'open';
807807

808808
if ( 'pulls' === $data_source ) {
809809
$result = GitHubAbilities::getPull( array(

inc/Runtime/WordPressRuntimeInspector.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,14 @@ public function read( array $input ): array|\WP_Error {
135135

136136
$size = (int) filesize( $resolved['real_path'] );
137137
if ( $size > $max_size ) {
138-
return new \WP_Error( 'datamachine_runtime_file_too_large', 'File exceeds the requested max_size.', array( 'path' => $resolved['relative_path'], 'size' => $size, 'max_size' => $max_size ) );
138+
return new \WP_Error( 'datamachine_runtime_file_too_large', 'File exceeds the requested max_size.', array(
139+
'path' => $resolved['relative_path'],
140+
'size' => $size,
141+
'max_size' => $max_size,
142+
) );
139143
}
140144

141-
$sample = @file_get_contents( $resolved['real_path'], false, null, 0, min( $size, 8192 ) );
145+
$sample = @file_get_contents( $resolved['real_path'], false, null, 0, min( $size, 8192 ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents,WordPress.PHP.NoSilencedErrors.Discouraged -- Path is validated by resolveReadablePath().
142146
if ( false === $sample ) {
143147
return new \WP_Error( 'datamachine_runtime_unreadable', 'File is not readable.' );
144148
}
@@ -147,7 +151,7 @@ public function read( array $input ): array|\WP_Error {
147151
return new \WP_Error( 'datamachine_runtime_binary_file', 'Binary file reading is denied.', array( 'path' => $resolved['relative_path'] ) );
148152
}
149153

150-
$lines = @file( $resolved['real_path'], FILE_IGNORE_NEW_LINES );
154+
$lines = @file( $resolved['real_path'], FILE_IGNORE_NEW_LINES ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file,WordPress.PHP.NoSilencedErrors.Discouraged -- Path is validated by resolveReadablePath().
151155
if ( false === $lines ) {
152156
return new \WP_Error( 'datamachine_runtime_unreadable', 'File is not readable.' );
153157
}

inc/Storage/CleanupRunRepository.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ public function add_items( string $run_id, array $items ): int|\WP_Error {
104104
public function get_run( string $run_id ): ?array {
105105
global $wpdb;
106106

107+
// phpcs:disable WordPress.DB.PreparedSQL -- Table name from $wpdb->prefix, not user input.
107108
$row = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . CleanupSchema::runs_table() . ' WHERE run_id = %s', $run_id ), ARRAY_A );
109+
// phpcs:enable WordPress.DB.PreparedSQL
108110
return is_array( $row ) ? $this->decode_run( $row ) : null;
109111
}
110112

@@ -117,7 +119,9 @@ public function get_run( string $run_id ): ?array {
117119
public function get_items( string $run_id ): array {
118120
global $wpdb;
119121

122+
// phpcs:disable WordPress.DB.PreparedSQL -- Table name from $wpdb->prefix, not user input.
120123
$rows = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . CleanupSchema::items_table() . ' WHERE run_id = %s ORDER BY id ASC', $run_id ), ARRAY_A );
124+
// phpcs:enable WordPress.DB.PreparedSQL
121125
return array_map( fn( $row ) => $this->decode_item( (array) $row ), is_array( $rows ) ? $rows : array() );
122126
}
123127

inc/Storage/WorktreeInventoryRepository.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ public function list( ?string $repo = null ): array {
166166

167167
$table = self::table_name();
168168
if ( null !== $repo && '' !== trim( $repo ) && method_exists( $wpdb, 'prepare' ) ) {
169+
// phpcs:disable WordPress.DB.PreparedSQL -- Table name from $wpdb->prefix, not user input.
169170
$sql = $wpdb->prepare( "SELECT * FROM {$table} WHERE repo = %s ORDER BY handle ASC", $repo );
171+
// phpcs:enable WordPress.DB.PreparedSQL
170172
} else {
171173
$sql = "SELECT * FROM {$table} ORDER BY handle ASC";
172174
}
@@ -253,7 +255,7 @@ private function normalize_row( array $row ): array {
253255
* @return array<string,mixed>
254256
*/
255257
private function decode_row( array $row ): array {
256-
$decoded = isset( $row['metadata'] ) && is_string( $row['metadata'] ) ? json_decode( $row['metadata'], true ) : null;
258+
$decoded = isset( $row['metadata'] ) && is_string( $row['metadata'] ) ? json_decode( $row['metadata'], true ) : null;
257259
$row['metadata'] = is_array( $decoded ) ? $decoded : null;
258260
foreach ( array( 'id', 'is_primary', 'dirty_count', 'unpushed_count', 'artifact_count', 'artifact_size_bytes', 'size_bytes', 'missing_path' ) as $key ) {
259261
if ( isset( $row[ $key ] ) ) {

inc/Workspace/CleanupRunService.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function __construct(
1818
private ?Workspace $workspace = null
1919
) {
2020
$this->repository ??= new CleanupRunRepository();
21-
$this->workspace ??= new Workspace();
21+
$this->workspace ??= new Workspace();
2222
}
2323

2424
/**
@@ -55,9 +55,9 @@ public function plan( array $opts = array() ): array|\WP_Error {
5555

5656
$plan['run_id'] = $run_id;
5757
$plan['cleanup_storage'] = array(
58-
'type' => 'database',
59-
'item_count' => $inserted,
60-
'plan_id' => $plan['plan_id'] ?? null,
58+
'type' => 'database',
59+
'item_count' => $inserted,
60+
'plan_id' => $plan['plan_id'] ?? null,
6161
'escape_hatch' => 'filesystem apply-plan import remains available on lower-level worktree commands only',
6262
);
6363

@@ -77,7 +77,10 @@ public function apply( string $run_id, array $opts = array() ): array|\WP_Error
7777
return new \WP_Error( 'cleanup_run_not_found', sprintf( 'Cleanup run not found: %s', $run_id ), array( 'status' => 404 ) );
7878
}
7979

80-
$this->repository->update_run( $run_id, array( 'status' => 'applying', 'started_at' => gmdate( 'Y-m-d H:i:s' ) ) );
80+
$this->repository->update_run( $run_id, array(
81+
'status' => 'applying',
82+
'started_at' => gmdate( 'Y-m-d H:i:s' ),
83+
) );
8184

8285
$items = $this->repository->get_items( $run_id );
8386
$artifact_rows = $this->pending_rows_of_type( $items, 'artifact_cleanup' );
@@ -105,7 +108,11 @@ public function apply( string $run_id, array $opts = array() ): array|\WP_Error
105108
$this->record_apply_result( $worktree_rows, $results['worktree_removal'], 'removed' );
106109
}
107110

108-
$this->repository->update_run( $run_id, array( 'status' => 'completed', 'completed_at' => gmdate( 'Y-m-d H:i:s' ), 'summary' => $this->status( $run_id )['summary'] ?? array() ) );
111+
$this->repository->update_run( $run_id, array(
112+
'status' => 'completed',
113+
'completed_at' => gmdate( 'Y-m-d H:i:s' ),
114+
'summary' => $this->status( $run_id )['summary'] ?? array(),
115+
) );
109116

110117
return array(
111118
'success' => true,
@@ -138,8 +145,8 @@ public function status( string $run_id ): array|\WP_Error {
138145
'pending_or_failed' => 0,
139146
);
140147
foreach ( $items as $item ) {
141-
$status = (string) ( $item['status'] ?? 'unknown' );
142-
$type = (string) ( $item['item_type'] ?? 'unknown' );
148+
$status = (string) ( $item['status'] ?? 'unknown' );
149+
$type = (string) ( $item['item_type'] ?? 'unknown' );
143150
$summary['items_by_status'][ $status ] = ( $summary['items_by_status'][ $status ] ?? 0 ) + 1;
144151
$summary['items_by_type'][ $type ] = ( $summary['items_by_type'][ $type ] ?? 0 ) + 1;
145152
$summary['bytes_reclaimed'] += max( 0, (int) ( $item['bytes_reclaimed'] ?? 0 ) );
@@ -190,7 +197,10 @@ public function cancel( string $run_id ): array|\WP_Error {
190197
$this->repository->update_item( (int) $item['id'], array( 'status' => 'cancelled' ) );
191198
}
192199
}
193-
$this->repository->update_run( $run_id, array( 'status' => 'cancelled', 'completed_at' => gmdate( 'Y-m-d H:i:s' ) ) );
200+
$this->repository->update_run( $run_id, array(
201+
'status' => 'cancelled',
202+
'completed_at' => gmdate( 'Y-m-d H:i:s' ),
203+
) );
194204
return $this->status( $run_id );
195205
}
196206

@@ -227,13 +237,17 @@ private function plan_items( array $plan ): array {
227237
}
228238

229239
private function pending_rows_of_type( array $items, string $type ): array {
230-
return array_values( array_filter( $items, fn( $item ) => $type === (string) ( $item['item_type'] ?? '' ) && in_array( (string) ( $item['status'] ?? '' ), array( 'pending', 'failed' ), true ) ) );
240+
return array_values( array_filter( $items, fn( $item ) => (string) ( $item['item_type'] ?? '' ) === $type && in_array( (string) ( $item['status'] ?? '' ), array( 'pending', 'failed' ), true ) ) );
231241
}
232242

233243
private function record_apply_result( array $items, mixed $result, string $applied_key ): void {
234244
if ( $result instanceof \WP_Error ) {
235245
foreach ( $items as $item ) {
236-
$this->repository->update_item( (int) $item['id'], array( 'status' => 'failed', 'reason_code' => $result->get_error_code(), 'reason' => $result->get_error_message() ) );
246+
$this->repository->update_item( (int) $item['id'], array(
247+
'status' => 'failed',
248+
'reason_code' => $result->get_error_code(),
249+
'reason' => $result->get_error_message(),
250+
) );
237251
}
238252
return;
239253
}

inc/Workspace/WorkspaceArtifactCleanup.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function worktree_cleanup_artifacts( array $opts = array() ): array|\WP_E
137137
'reason' => sprintf( 'failed to remove artifact %s: %s', (string) ( $artifact['path'] ?? '' ), $remove->get_error_message() ),
138138
'artifacts' => array( $artifact ),
139139
);
140-
$failed = true;
140+
$failed = true;
141141
break;
142142
}
143143

@@ -417,7 +417,7 @@ private function build_worktree_artifact_cleanup_summary( array $candidates, arr
417417
foreach ( $candidates as $row ) {
418418
$repo = (string) ( $row['repo'] ?? 'unknown' );
419419
foreach ( (array) ( $row['artifacts'] ?? array() ) as $artifact ) {
420-
$bytes = (int) ( is_array( $artifact ) ? ( $artifact['size_bytes'] ?? 0 ) : 0 );
420+
$bytes = (int) ( is_array( $artifact ) ? ( $artifact['size_bytes'] ?? 0 ) : 0 );
421421
$would_bytes += max( 0, $bytes );
422422
++$would_count;
423423
$artifact_by_repo[ $repo ] = ( $artifact_by_repo[ $repo ] ?? 0 ) + max( 0, $bytes );
@@ -522,7 +522,7 @@ private function scope_worktree_artifact_cleanup_to_plan( array $planned_candida
522522
}
523523
}
524524

525-
$skip = $complete ? array(
525+
$skip = $complete ? array(
526526
'handle' => $handle,
527527
'repo' => (string) ( $plan_row['repo'] ?? '' ),
528528
'branch' => (string) ( $plan_row['branch'] ?? '' ),
@@ -674,5 +674,4 @@ private function is_active_studio_symlink_target( string $worktree_path ): bool
674674

675675
return false;
676676
}
677-
678677
}

0 commit comments

Comments
 (0)