Skip to content

Commit 69bd29f

Browse files
Copilotswissspidy
andcommitted
Optimize alignment lookup and validation
- Extract getColumnAlignment() method for better readability - Use array_flip() for O(1) lookups instead of in_array() O(n) searches - Address code review feedback Co-authored-by: swissspidy <[email protected]>
1 parent df01866 commit 69bd29f

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

lib/cli/Table.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,17 @@ public function setFooters(array $footers) {
217217
* @param array $alignments An array of alignment constants keyed by column name.
218218
*/
219219
public function setAlignments(array $alignments) {
220-
$valid_alignments = array( Column::ALIGN_LEFT, Column::ALIGN_RIGHT, Column::ALIGN_CENTER );
220+
$valid_alignments = array_flip( array( Column::ALIGN_LEFT, Column::ALIGN_RIGHT, Column::ALIGN_CENTER ) );
221221
foreach ( $alignments as $column => $alignment ) {
222-
if ( ! in_array( $alignment, $valid_alignments, true ) ) {
222+
if ( ! isset( $valid_alignments[ $alignment ] ) ) {
223223
throw new \InvalidArgumentException( "Invalid alignment value '$alignment' for column '$column'." );
224224
}
225225
// Only validate column names if headers are already set
226-
if ( ! empty( $this->_headers ) && ! in_array( $column, $this->_headers, true ) ) {
227-
throw new \InvalidArgumentException( "Column '$column' does not exist in table headers." );
226+
if ( ! empty( $this->_headers ) ) {
227+
$headers_map = array_flip( $this->_headers );
228+
if ( ! isset( $headers_map[ $column ] ) ) {
229+
throw new \InvalidArgumentException( "Column '$column' does not exist in table headers." );
230+
}
228231
}
229232
}
230233
$this->_alignments = $alignments;

lib/cli/table/Ascii.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,22 @@ public function row( array $row ) {
197197
return $ret;
198198
}
199199

200+
/**
201+
* Get the alignment for a column.
202+
*
203+
* @param int $column Column index.
204+
* @return int Alignment constant (STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH).
205+
*/
206+
private function getColumnAlignment( $column ) {
207+
$column_name = isset( $this->_headers[ $column ] ) ? $this->_headers[ $column ] : '';
208+
if ( $column_name !== '' && array_key_exists( $column_name, $this->_alignments ) ) {
209+
return $this->_alignments[ $column_name ];
210+
}
211+
return Column::ALIGN_LEFT;
212+
}
213+
200214
private function padColumn($content, $column) {
201-
$column_name = isset( $this->_headers[$column] ) ? $this->_headers[$column] : '';
202-
$alignment = ( $column_name !== '' && array_key_exists( $column_name, $this->_alignments ) ) ? $this->_alignments[$column_name] : Column::ALIGN_LEFT;
215+
$alignment = $this->getColumnAlignment( $column );
203216
$content = str_replace( "\t", ' ', (string) $content );
204217
return $this->_characters['padding'] . Colors::pad( $content, $this->_widths[ $column ], $this->isPreColorized( $column ), false, $alignment ) . $this->_characters['padding'];
205218
}

0 commit comments

Comments
 (0)