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 |
||
62 | protected function columnAutowidth($rows, $widths) |
||
63 | { |
||
64 | $auto_widths = $widths; |
||
65 | |||
66 | // First we determine the distribution of row lengths in each column. |
||
67 | // This is an array of descending character length keys (i.e. starting at |
||
68 | // the rightmost character column), with the value indicating the number |
||
69 | // of rows where that character column is present. |
||
70 | $col_dist = array(); |
||
71 | foreach ($rows as $rowkey => $row) { |
||
72 | foreach ($row as $col_id => $cell) { |
||
73 | if (empty($widths[$col_id])) { |
||
74 | $length = strlen($cell); |
||
75 | if ($length == 0) { |
||
76 | $col_dist[$col_id][0] = 0; |
||
77 | } |
||
78 | while ($length > 0) { |
||
79 | if (!isset($col_dist[$col_id][$length])) { |
||
80 | $col_dist[$col_id][$length] = 0; |
||
81 | } |
||
82 | $col_dist[$col_id][$length]++; |
||
83 | $length--; |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | foreach ($col_dist as $col_id => $count) { |
||
89 | // Sort the distribution in decending key order. |
||
90 | krsort($col_dist[$col_id]); |
||
91 | // Initially we set all columns to their "ideal" longest width |
||
92 | // - i.e. the width of their longest column. |
||
93 | $auto_widths[$col_id] = max(array_keys($col_dist[$col_id])); |
||
94 | } |
||
95 | |||
96 | // We determine what width we have available to use, and what width the |
||
97 | // above "ideal" columns take up. |
||
98 | $available_width = $this->width - (count($auto_widths) * 2); |
||
99 | $auto_width_current = array_sum($auto_widths); |
||
100 | |||
101 | // If we need to reduce a column so that we can fit the space we use this |
||
102 | // loop to figure out which column will cause the "least wrapping", |
||
103 | // (relative to the other columns) and reduce the width of that column. |
||
104 | while ($auto_width_current > $available_width) { |
||
105 | $count = 0; |
||
106 | $width = 0; |
||
107 | foreach ($col_dist as $col_id => $counts) { |
||
108 | // If we are just starting out, select the first column. |
||
109 | if ($count == 0 || |
||
110 | // OR: if this column would cause less wrapping than the currently |
||
111 | // selected column, then select it. |
||
112 | (current($counts) < $count) || |
||
113 | // OR: if this column would cause the same amount of wrapping, but is |
||
114 | // longer, then we choose to wrap the longer column (proportionally |
||
115 | // less wrapping, and helps avoid triple line wraps). |
||
116 | (current($counts) == $count && key($counts) > $width)) { |
||
117 | // Select the column number, and record the count and current width |
||
118 | // for later comparisons. |
||
119 | $column = $col_id; |
||
120 | $count = current($counts); |
||
121 | $width = key($counts); |
||
122 | } |
||
123 | } |
||
124 | if ($width <= 1) { |
||
125 | // If we have reached a width of 1 then give up, so wordwrap can still progress. |
||
126 | break; |
||
127 | } |
||
128 | // Reduce the width of the selected column. |
||
129 | $auto_widths[$column]--; |
||
|
|||
130 | // Reduce our overall table width counter. |
||
131 | $auto_width_current--; |
||
132 | // Remove the corresponding data from the disctribution, so next time |
||
133 | // around we use the data for the row to the left. |
||
134 | unset($col_dist[$column][$width]); |
||
135 | } |
||
136 | return $auto_widths; |
||
137 | } |
||
138 | } |
||
139 |
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: