| Conditions | 15 |
| Paths | 96 |
| Total Lines | 74 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 95 | protected function columnAutowidth($rows, $widths) |
||
| 96 | { |
||
| 97 | $auto_widths = $widths; |
||
| 98 | |||
| 99 | // First we determine the distribution of row lengths in each column. |
||
| 100 | // This is an array of descending character length keys (i.e. starting at |
||
| 101 | // the rightmost character column), with the value indicating the number |
||
| 102 | // of rows where that character column is present. |
||
| 103 | $col_dist = []; |
||
| 104 | // We will also calculate the longest word in each column |
||
| 105 | $max_word_lens = []; |
||
| 106 | foreach ($rows as $rowkey => $row) { |
||
| 107 | foreach ($row as $col_id => $cell) { |
||
| 108 | $longest_word_len = static::longestWordLength($cell); |
||
| 109 | if ((!isset($max_word_lens[$col_id]) || ($max_word_lens[$col_id] < $longest_word_len))) { |
||
| 110 | $max_word_lens[$col_id] = $longest_word_len; |
||
| 111 | } |
||
| 112 | if (empty($widths[$col_id])) { |
||
| 113 | $length = strlen($cell); |
||
| 114 | if ($length == 0) { |
||
| 115 | $col_dist[$col_id][0] = 0; |
||
| 116 | } |
||
| 117 | while ($length > 0) { |
||
| 118 | if (!isset($col_dist[$col_id][$length])) { |
||
| 119 | $col_dist[$col_id][$length] = 0; |
||
| 120 | } |
||
| 121 | $col_dist[$col_id][$length]++; |
||
| 122 | $length--; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | foreach ($col_dist as $col_id => $count) { |
||
| 129 | // Sort the distribution in decending key order. |
||
| 130 | krsort($col_dist[$col_id]); |
||
| 131 | // Initially we set all columns to their "ideal" longest width |
||
| 132 | // - i.e. the width of their longest column. |
||
| 133 | $auto_widths[$col_id] = max(array_keys($col_dist[$col_id])); |
||
| 134 | } |
||
| 135 | |||
| 136 | // We determine what width we have available to use, and what width the |
||
| 137 | // above "ideal" columns take up. |
||
| 138 | $available_width = $this->width - ($this->extraPaddingAtBeginningOfLine + $this->extraPaddingAtEndOfLine + (count($auto_widths) * $this->paddingInEachCell)); |
||
| 139 | $auto_width_current = array_sum($auto_widths); |
||
| 140 | |||
| 141 | // If we cannot fit into the minimum width anyway, then just return |
||
| 142 | // the max word length of each column as the 'ideal' |
||
| 143 | $minimumIdealLength = array_sum($this->minimumWidths); |
||
| 144 | if ($minimumIdealLength && ($available_width < $minimumIdealLength)) { |
||
| 145 | return $max_word_lens; |
||
| 146 | } |
||
| 147 | |||
| 148 | // If we need to reduce a column so that we can fit the space we use this |
||
| 149 | // loop to figure out which column will cause the "least wrapping", |
||
| 150 | // (relative to the other columns) and reduce the width of that column. |
||
| 151 | while ($auto_width_current > $available_width) { |
||
| 152 | |||
| 153 | list($column, $count, $width) = $this->selectColumnToReduce($col_dist, $auto_widths, $max_word_lens); |
||
|
|
|||
| 154 | |||
| 155 | if (!$column || $width <= 1) { |
||
| 156 | // If we have reached a width of 1 then give up, so wordwrap can still progress. |
||
| 157 | break; |
||
| 158 | } |
||
| 159 | // Reduce the width of the selected column. |
||
| 160 | $auto_widths[$column]--; |
||
| 161 | // Reduce our overall table width counter. |
||
| 162 | $auto_width_current--; |
||
| 163 | // Remove the corresponding data from the disctribution, so next time |
||
| 164 | // around we use the data for the row to the left. |
||
| 165 | unset($col_dist[$column][$width]); |
||
| 166 | } |
||
| 167 | return $auto_widths; |
||
| 168 | } |
||
| 169 | |||
| 268 |
This checks looks for assignemnts to variables using the
list(...)function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$aand$care used. There was no need to assign$b.Instead, the list call could have been.