Conditions | 15 |
Paths | 96 |
Total Lines | 73 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 | list($column, $width) = $this->selectColumnToReduce($col_dist, $auto_widths, $max_word_lens); |
||
153 | |||
154 | if (!$column || $width <= 1) { |
||
155 | // If we have reached a width of 1 then give up, so wordwrap can still progress. |
||
156 | break; |
||
157 | } |
||
158 | // Reduce the width of the selected column. |
||
159 | $auto_widths[$column]--; |
||
160 | // Reduce our overall table width counter. |
||
161 | $auto_width_current--; |
||
162 | // Remove the corresponding data from the disctribution, so next time |
||
163 | // around we use the data for the row to the left. |
||
164 | unset($col_dist[$column][$width]); |
||
165 | } |
||
166 | return $auto_widths; |
||
167 | } |
||
168 | |||
228 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.