Skip to content

Commit 7b4002a

Browse files
Copilotswissspidy
andcommitted
Address code review feedback
- Remove unused $_displayed property - Fix assignment in function argument (use false directly) - Add proper cleanup with try-finally blocks in tests - Ensure temp files are cleaned up even if assertions fail Co-authored-by: swissspidy <[email protected]>
1 parent 19ec48e commit 7b4002a

File tree

2 files changed

+53
-44
lines changed

2 files changed

+53
-44
lines changed

lib/cli/Table.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class Table {
2727
protected $_footers = array();
2828
protected $_width = array();
2929
protected $_rows = array();
30-
protected $_displayed = false;
3130

3231
/**
3332
* Initializes the `Table` class.
@@ -80,7 +79,6 @@ public function resetTable()
8079
$this->_width = array();
8180
$this->_rows = array();
8281
$this->_footers = array();
83-
$this->_displayed = false;
8482
return $this;
8583
}
8684

@@ -138,7 +136,6 @@ public function display() {
138136
foreach( $this->getDisplayLines() as $line ) {
139137
Streams::line( $line );
140138
}
141-
$this->_displayed = true;
142139
}
143140

144141
/**
@@ -154,7 +151,7 @@ public function displayRow(array $row) {
154151
$row = $this->checkRow($row);
155152

156153
// Recalculate widths for the renderer
157-
$this->_renderer->setWidths($this->_width, $fallback = false);
154+
$this->_renderer->setWidths($this->_width, false);
158155

159156
$rendered_row = $this->_renderer->row($row);
160157
$row_lines = explode( PHP_EOL, $rendered_row );

tests/Test_Table.php

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -308,48 +308,60 @@ public function test_resetRows() {
308308
}
309309

310310
public function test_displayRow_ascii() {
311-
$mockFile = tempnam(sys_get_temp_dir(), 'temp');
312-
$resource = fopen($mockFile, 'wb');
313-
\cli\Streams::setStream('out', $resource);
314-
315-
$table = new cli\Table();
316-
$renderer = new cli\Table\Ascii();
317-
$table->setRenderer( $renderer );
318-
$table->setHeaders( array( 'Name', 'Age' ) );
319-
320-
// Display a single row
321-
$table->displayRow( array( 'Alice', '30' ) );
322-
323-
$output = file_get_contents($mockFile);
324-
unlink($mockFile);
325-
326-
// Should contain the row data
327-
$this->assertStringContainsString( 'Alice', $output );
328-
$this->assertStringContainsString( '30', $output );
329-
330-
// Should contain borders
331-
$this->assertStringContainsString( '|', $output );
332-
$this->assertStringContainsString( '+', $output );
311+
$mockFile = tempnam( sys_get_temp_dir(), 'temp' );
312+
$resource = fopen( $mockFile, 'wb' );
313+
314+
try {
315+
\cli\Streams::setStream( 'out', $resource );
316+
317+
$table = new cli\Table();
318+
$renderer = new cli\Table\Ascii();
319+
$table->setRenderer( $renderer );
320+
$table->setHeaders( array( 'Name', 'Age' ) );
321+
322+
// Display a single row
323+
$table->displayRow( array( 'Alice', '30' ) );
324+
325+
$output = file_get_contents( $mockFile );
326+
327+
// Should contain the row data
328+
$this->assertStringContainsString( 'Alice', $output );
329+
$this->assertStringContainsString( '30', $output );
330+
331+
// Should contain borders
332+
$this->assertStringContainsString( '|', $output );
333+
$this->assertStringContainsString( '+', $output );
334+
} finally {
335+
if ( $mockFile && file_exists( $mockFile ) ) {
336+
unlink( $mockFile );
337+
}
338+
}
333339
}
334340

335341
public function test_displayRow_tabular() {
336-
$mockFile = tempnam(sys_get_temp_dir(), 'temp');
337-
$resource = fopen($mockFile, 'wb');
338-
\cli\Streams::setStream('out', $resource);
339-
340-
$table = new cli\Table();
341-
$renderer = new cli\Table\Tabular();
342-
$table->setRenderer( $renderer );
343-
$table->setHeaders( array( 'Name', 'Age' ) );
344-
345-
// Display a single row
346-
$table->displayRow( array( 'Alice', '30' ) );
347-
348-
$output = file_get_contents($mockFile);
349-
unlink($mockFile);
350-
351-
// Should contain the row data with tabs
352-
$this->assertStringContainsString( 'Alice', $output );
353-
$this->assertStringContainsString( '30', $output );
342+
$mockFile = tempnam( sys_get_temp_dir(), 'temp' );
343+
$resource = fopen( $mockFile, 'wb' );
344+
345+
try {
346+
\cli\Streams::setStream( 'out', $resource );
347+
348+
$table = new cli\Table();
349+
$renderer = new cli\Table\Tabular();
350+
$table->setRenderer( $renderer );
351+
$table->setHeaders( array( 'Name', 'Age' ) );
352+
353+
// Display a single row
354+
$table->displayRow( array( 'Alice', '30' ) );
355+
356+
$output = file_get_contents( $mockFile );
357+
358+
// Should contain the row data with tabs
359+
$this->assertStringContainsString( 'Alice', $output );
360+
$this->assertStringContainsString( '30', $output );
361+
} finally {
362+
if ( $mockFile && file_exists( $mockFile ) ) {
363+
unlink( $mockFile );
364+
}
365+
}
354366
}
355367
}

0 commit comments

Comments
 (0)