Skip to content

Commit bf49aef

Browse files
Fix cleanup drain lint
1 parent 69c730f commit bf49aef

2 files changed

Lines changed: 59 additions & 30 deletions

File tree

inc/Cli/Commands/WorkspaceCommand.php

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3640,7 +3640,7 @@ public function worktree( array $args, array $assoc_args ): void {
36403640

36413641
$input = array();
36423642
$input_builder = (string) ( $operation_config['input_builder'] ?? '' );
3643-
if ( '' !== $input_builder && method_exists($this, $input_builder) ) {
3643+
if ( '' !== $input_builder ) {
36443644
$input = $this->{$input_builder}($operation, $assoc_args);
36453645
}
36463646

@@ -6059,19 +6059,47 @@ private function render_worktree_cleanup_eligible_drain_result( array $result, a
60596059
return;
60606060
}
60616061

6062-
$summary = (array) ( $result['summary'] ?? array() );
6062+
$summary = (array) ( $result['summary'] ?? array() );
6063+
$final_free_space = (array) ( $summary['final_free_space'] ?? array() );
60636064
WP_CLI::log('Cleanup-eligible drain summary:');
60646065
$this->format_items(
60656066
array(
6066-
array( 'metric' => 'mode', 'value' => ! empty($result['applied']) ? 'apply' : 'preview' ),
6067-
array( 'metric' => 'passes', 'value' => (int) ( $summary['passes'] ?? 0 ) ),
6068-
array( 'metric' => 'processed', 'value' => (int) ( $summary['processed'] ?? 0 ) ),
6069-
array( 'metric' => 'would_remove', 'value' => (int) ( $summary['would_remove'] ?? 0 ) ),
6070-
array( 'metric' => 'removed', 'value' => (int) ( $summary['removed'] ?? 0 ) ),
6071-
array( 'metric' => 'skipped', 'value' => (int) ( $summary['skipped'] ?? 0 ) ),
6072-
array( 'metric' => 'bytes_reclaimed', 'value' => $this->format_bytes($summary['bytes_reclaimed'] ?? 0) ),
6073-
array( 'metric' => 'stop_reason', 'value' => (string) ( $summary['stop_reason'] ?? '' ) ),
6074-
array( 'metric' => 'final_free_space', 'value' => (string) ( (array) ( $summary['final_free_space'] ?? array() )['free_human'] ?? 'unknown' ) ),
6067+
array(
6068+
'metric' => 'mode',
6069+
'value' => ! empty($result['applied']) ? 'apply' : 'preview',
6070+
),
6071+
array(
6072+
'metric' => 'passes',
6073+
'value' => (int) ( $summary['passes'] ?? 0 ),
6074+
),
6075+
array(
6076+
'metric' => 'processed',
6077+
'value' => (int) ( $summary['processed'] ?? 0 ),
6078+
),
6079+
array(
6080+
'metric' => 'would_remove',
6081+
'value' => (int) ( $summary['would_remove'] ?? 0 ),
6082+
),
6083+
array(
6084+
'metric' => 'removed',
6085+
'value' => (int) ( $summary['removed'] ?? 0 ),
6086+
),
6087+
array(
6088+
'metric' => 'skipped',
6089+
'value' => (int) ( $summary['skipped'] ?? 0 ),
6090+
),
6091+
array(
6092+
'metric' => 'bytes_reclaimed',
6093+
'value' => $this->format_bytes( (int) ( $summary['bytes_reclaimed'] ?? 0 ) ),
6094+
),
6095+
array(
6096+
'metric' => 'stop_reason',
6097+
'value' => (string) ( $summary['stop_reason'] ?? '' ),
6098+
),
6099+
array(
6100+
'metric' => 'final_free_space',
6101+
'value' => (string) ( $final_free_space['free_human'] ?? 'unknown' ),
6102+
),
60756103
),
60766104
array( 'metric', 'value' ),
60776105
array( 'format' => 'table' ),

inc/Workspace/WorkspaceCleanupEligibleDrainOrchestrator.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function run( array $input ): array|\WP_Error {
6565
}
6666

6767
$ability = ( $this->ability_resolver )('datamachine-code/workspace-worktree-bounded-cleanup-eligible-apply');
68-
if ( ! $ability || ! is_callable(array( $ability, 'execute' )) ) {
68+
if ( ! is_object($ability) || ! is_callable(array( $ability, 'execute' )) ) {
6969
return new \WP_Error('cleanup_eligible_drain_ability_missing', 'Bounded cleanup-eligible apply ability is not available.', array( 'status' => 500 ));
7070
}
7171

@@ -130,18 +130,18 @@ public function run( array $input ): array|\WP_Error {
130130
$continuation = (array) ( $pass_result['continuation'] ?? array() );
131131
$evidence = (array) ( $pass_result['evidence'] ?? array() );
132132
$pass_summary = array(
133-
'pass' => $pass,
134-
'dry_run' => ! empty($pass_result['dry_run']),
135-
'processed' => (int) ( $summary['processed'] ?? 0 ),
136-
'would_remove' => ! empty($pass_result['dry_run']) ? count( (array) ( $pass_result['candidates'] ?? array() ) ) : 0,
137-
'removed' => (int) ( $summary['removed'] ?? 0 ),
138-
'skipped' => (int) ( $summary['skipped'] ?? 0 ),
139-
'bytes_reclaimed' => (int) ( $summary['bytes_reclaimed'] ?? 0 ),
140-
'remaining_total' => (int) ( $continuation['remaining_total'] ?? 0 ),
141-
'elapsed_ms' => (int) ( $evidence['elapsed_ms'] ?? 0 ),
142-
'removed_handles' => array_values(array_filter(array_map(fn( $row ) => is_array($row) ? (string) ( $row['handle'] ?? '' ) : '', (array) ( $pass_result['removed'] ?? array() )))),
143-
'candidate_handles' => array_values(array_filter(array_map(fn( $row ) => is_array($row) ? (string) ( $row['handle'] ?? '' ) : '', (array) ( $pass_result['candidates'] ?? array() )))),
144-
'skipped_by_reason' => $this->summarize_skipped((array) ( $pass_result['skipped'] ?? array() )),
133+
'pass' => $pass,
134+
'dry_run' => ! empty($pass_result['dry_run']),
135+
'processed' => (int) ( $summary['processed'] ?? 0 ),
136+
'would_remove' => ! empty($pass_result['dry_run']) ? count( (array) ( $pass_result['candidates'] ?? array() ) ) : 0,
137+
'removed' => (int) ( $summary['removed'] ?? 0 ),
138+
'skipped' => (int) ( $summary['skipped'] ?? 0 ),
139+
'bytes_reclaimed' => (int) ( $summary['bytes_reclaimed'] ?? 0 ),
140+
'remaining_total' => (int) ( $continuation['remaining_total'] ?? 0 ),
141+
'elapsed_ms' => (int) ( $evidence['elapsed_ms'] ?? 0 ),
142+
'removed_handles' => array_values(array_filter(array_map(fn( $row ) => is_array($row) ? (string) ( $row['handle'] ?? '' ) : '', (array) ( $pass_result['removed'] ?? array() )))),
143+
'candidate_handles' => array_values(array_filter(array_map(fn( $row ) => is_array($row) ? (string) ( $row['handle'] ?? '' ) : '', (array) ( $pass_result['candidates'] ?? array() )))),
144+
'skipped_by_reason' => $this->summarize_skipped( (array) ( $pass_result['skipped'] ?? array() ) ),
145145
);
146146

147147
$result['pass_results'][] = $pass_summary;
@@ -213,7 +213,7 @@ private function budget_expired( ?float $deadline ): bool {
213213
private function summarize_skipped( array $rows ): array {
214214
$summary = array();
215215
foreach ( $rows as $row ) {
216-
$reason = is_array($row) ? (string) ( $row['reason_code'] ?? 'unknown' ) : 'unknown';
216+
$reason = (string) ( $row['reason_code'] ?? 'unknown' );
217217
$summary[ $reason ] = (int) ( $summary[ $reason ] ?? 0 ) + 1;
218218
}
219219
return $summary;
@@ -235,16 +235,17 @@ private function build_disk_report( string $workspace_path ): array {
235235
return array(
236236
'path' => $workspace_path,
237237
'free_bytes' => false === $free ? null : (int) $free,
238-
'free_human' => false === $free ? 'unknown' : $this->format_bytes((int) $free),
238+
'free_human' => false === $free ? 'unknown' : $this->format_bytes( (int) $free ),
239239
'total_bytes' => false === $total ? null : (int) $total,
240240
);
241241
}
242242

243243
private function format_bytes( int $bytes ): string {
244-
$units = array( 'B', 'KiB', 'MiB', 'GiB', 'TiB' );
245-
$value = (float) max(0, $bytes);
246-
$unit = 0;
247-
while ( $value >= 1024 && $unit < count($units) - 1 ) {
244+
$units = array( 'B', 'KiB', 'MiB', 'GiB', 'TiB' );
245+
$value = (float) max(0, $bytes);
246+
$unit = 0;
247+
$unit_count = count($units);
248+
while ( $value >= 1024 && $unit < $unit_count - 1 ) {
248249
$value /= 1024;
249250
++$unit;
250251
}

0 commit comments

Comments
 (0)