| Conditions | 15 |
| Paths | 72 |
| Total Lines | 76 |
| Code Lines | 35 |
| 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 |
||
| 85 | protected function columnAutowidth($rows, $widths) |
||
| 86 | { |
||
| 87 | $auto_widths = $widths; |
||
| 88 | |||
| 89 | // First we determine the distribution of row lengths in each column. |
||
| 90 | // This is an array of descending character length keys (i.e. starting at |
||
| 91 | // the rightmost character column), with the value indicating the number |
||
| 92 | // of rows where that character column is present. |
||
| 93 | $col_dist = array(); |
||
| 94 | foreach ($rows as $rowkey => $row) { |
||
| 95 | foreach ($row as $col_id => $cell) { |
||
| 96 | if (empty($widths[$col_id])) { |
||
| 97 | $length = strlen($cell); |
||
| 98 | if ($length == 0) { |
||
| 99 | $col_dist[$col_id][0] = 0; |
||
| 100 | } |
||
| 101 | while ($length > 0) { |
||
| 102 | if (!isset($col_dist[$col_id][$length])) { |
||
| 103 | $col_dist[$col_id][$length] = 0; |
||
| 104 | } |
||
| 105 | $col_dist[$col_id][$length]++; |
||
| 106 | $length--; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | } |
||
| 110 | } |
||
| 111 | foreach ($col_dist as $col_id => $count) { |
||
| 112 | // Sort the distribution in decending key order. |
||
| 113 | krsort($col_dist[$col_id]); |
||
| 114 | // Initially we set all columns to their "ideal" longest width |
||
| 115 | // - i.e. the width of their longest column. |
||
| 116 | $auto_widths[$col_id] = max(array_keys($col_dist[$col_id])); |
||
| 117 | } |
||
| 118 | |||
| 119 | // We determine what width we have available to use, and what width the |
||
| 120 | // above "ideal" columns take up. |
||
| 121 | $available_width = $this->width - ($this->extraPaddingAtBeginningOfLine + $this->extraPaddingAtEndOfLine + (count($auto_widths) * $this->paddingInEachCell)); |
||
| 122 | $auto_width_current = array_sum($auto_widths); |
||
| 123 | |||
| 124 | // If we need to reduce a column so that we can fit the space we use this |
||
| 125 | // loop to figure out which column will cause the "least wrapping", |
||
| 126 | // (relative to the other columns) and reduce the width of that column. |
||
| 127 | while ($auto_width_current > $available_width) { |
||
| 128 | $count = 0; |
||
| 129 | $width = 0; |
||
| 130 | foreach ($col_dist as $col_id => $counts) { |
||
| 131 | // If we are just starting out, select the first column. |
||
| 132 | if ($count == 0 || |
||
| 133 | // OR: if this column would cause less wrapping than the currently |
||
| 134 | // selected column, then select it. |
||
| 135 | (current($counts) < $count) || |
||
| 136 | // OR: if this column would cause the same amount of wrapping, but is |
||
| 137 | // longer, then we choose to wrap the longer column (proportionally |
||
| 138 | // less wrapping, and helps avoid triple line wraps). |
||
| 139 | (current($counts) == $count && key($counts) > $width)) { |
||
| 140 | // Select the column number, and record the count and current width |
||
| 141 | // for later comparisons. |
||
| 142 | $column = $col_id; |
||
| 143 | $count = current($counts); |
||
| 144 | $width = key($counts); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | if ($width <= 1) { |
||
| 148 | // If we have reached a width of 1 then give up, so wordwrap can still progress. |
||
| 149 | break; |
||
| 150 | } |
||
| 151 | // Reduce the width of the selected column. |
||
| 152 | $auto_widths[$column]--; |
||
|
|
|||
| 153 | // Reduce our overall table width counter. |
||
| 154 | $auto_width_current--; |
||
| 155 | // Remove the corresponding data from the disctribution, so next time |
||
| 156 | // around we use the data for the row to the left. |
||
| 157 | unset($col_dist[$column][$width]); |
||
| 158 | } |
||
| 159 | return $auto_widths; |
||
| 160 | } |
||
| 161 | } |
||
| 162 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: