1 | <?php |
||
29 | class CellWrapper |
||
30 | { |
||
31 | /** |
||
32 | * @var string[] |
||
33 | */ |
||
34 | private $cells = array(); |
||
35 | |||
36 | /** |
||
37 | * @var int[][] |
||
38 | */ |
||
39 | private $cellLengths = array(); |
||
40 | |||
41 | /** |
||
42 | * @var string[][] |
||
43 | */ |
||
44 | private $wrappedRows = array(); |
||
45 | |||
46 | /** |
||
47 | * @var int |
||
48 | */ |
||
49 | private $nbColumns = 0; |
||
50 | |||
51 | /** |
||
52 | * @var int[] |
||
53 | */ |
||
54 | private $columnLengths; |
||
55 | |||
56 | /** |
||
57 | * @var bool |
||
58 | */ |
||
59 | private $wordWraps = false; |
||
60 | |||
61 | /** |
||
62 | * @var bool |
||
63 | */ |
||
64 | private $wordCuts = false; |
||
65 | |||
66 | /** |
||
67 | * @var int |
||
68 | */ |
||
69 | private $maxTotalWidth = 0; |
||
70 | |||
71 | /** |
||
72 | * @var int |
||
73 | */ |
||
74 | private $totalWidth = 0; |
||
75 | |||
76 | /** |
||
77 | * Adds a cell to the wrapper. |
||
78 | * |
||
79 | * @param string $cell The data cell. |
||
80 | */ |
||
81 | 21 | public function addCell($cell) |
|
85 | |||
86 | /** |
||
87 | * Adds cells to the wrapper. |
||
88 | * |
||
89 | * @param string[] $cells The data cells. |
||
90 | */ |
||
91 | public function addCells(array $cells) |
||
92 | { |
||
93 | foreach ($cells as $cell) { |
||
94 | $this->cells[] = rtrim($cell); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Sets the data cells in the wrapper. |
||
100 | * |
||
101 | * @param string[] $cells The data cells. |
||
102 | */ |
||
103 | public function setCells(array $cells) |
||
104 | { |
||
105 | $this->cells = array(); |
||
106 | |||
107 | $this->addCells($cells); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Returns the data cells in the wrapper. |
||
112 | * |
||
113 | * @return string[] The data cells. |
||
114 | */ |
||
115 | public function getCells() |
||
119 | |||
120 | /** |
||
121 | * Returns the wrapped cells organized by rows and columns. |
||
122 | * |
||
123 | * The method {@link fit()} should be called before accessing this method. |
||
124 | * Otherwise, an empty array is returned. |
||
125 | * |
||
126 | * @return string[][] An array of arrays. The first level represents rows, |
||
127 | * the second level the cells in each row. |
||
128 | */ |
||
129 | 21 | public function getWrappedRows() |
|
133 | |||
134 | /** |
||
135 | * Returns the lengths of the wrapped columns. |
||
136 | * |
||
137 | * The method {@link fit()} should be called before accessing this method. |
||
138 | * Otherwise, an empty array is returned. |
||
139 | * |
||
140 | * @return int[] The lengths of each column. |
||
141 | */ |
||
142 | 21 | public function getColumnLengths() |
|
146 | |||
147 | /** |
||
148 | * Returns the number of wrapped columns. |
||
149 | * |
||
150 | * The method {@link fit()} should be called before accessing this method. |
||
151 | * Otherwise this method returns zero. |
||
152 | * |
||
153 | * @return int The number of columns. |
||
154 | */ |
||
155 | public function getNbColumns() |
||
159 | |||
160 | /** |
||
161 | * Returns an estimated number of columns for the given maximum width. |
||
162 | * |
||
163 | * @param int $maxTotalWidth The maximum total width of the columns. |
||
164 | * |
||
165 | * @return int The estimated number of columns. |
||
166 | */ |
||
167 | 10 | public function getEstimatedNbColumns($maxTotalWidth) |
|
168 | { |
||
169 | 10 | $i = 0; |
|
170 | 10 | $rowWidth = 0; |
|
171 | |||
172 | 10 | while (isset($this->cells[$i])) { |
|
173 | 10 | $rowWidth += StringUtil::getLength($this->cells[$i]); |
|
174 | |||
175 | 10 | if ($rowWidth > $maxTotalWidth) { |
|
176 | // Return previous number of columns |
||
177 | 9 | return $i; |
|
178 | } |
||
179 | |||
180 | 10 | ++$i; |
|
181 | } |
||
182 | |||
183 | 1 | return $i; |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * Returns the maximum allowed total width of the columns. |
||
188 | * |
||
189 | * The method {@link fit()} should be called before accessing this method. |
||
190 | * Otherwise this method returns zero. |
||
191 | * |
||
192 | * @return int The maximum allowed total width. |
||
193 | */ |
||
194 | public function getMaxTotalWidth() |
||
198 | |||
199 | /** |
||
200 | * Returns the actual total column width. |
||
201 | * |
||
202 | * The method {@link fit()} should be called before accessing this method. |
||
203 | * Otherwise this method returns zero. |
||
204 | * |
||
205 | * @return int The actual total column width. |
||
206 | */ |
||
207 | public function getTotalWidth() |
||
211 | |||
212 | /** |
||
213 | * Returns whether any of the cells needed to be wrapped into multiple |
||
214 | * lines. |
||
215 | * |
||
216 | * The method {@link fit()} should be called before accessing this method. |
||
217 | * Otherwise this method returns `false`. |
||
218 | * |
||
219 | * @return bool Returns `true` if a cell was wrapped into multiple lines |
||
220 | * and `false` otherwise. |
||
221 | */ |
||
222 | public function hasWordWraps() |
||
226 | |||
227 | /** |
||
228 | * Returns whether any of the cells contains words cut in two. |
||
229 | * |
||
230 | * The method {@link fit()} should be called before accessing this method. |
||
231 | * Otherwise this method returns `false`. |
||
232 | * |
||
233 | * @return bool Returns `true` if a cell contains words cut in two and |
||
234 | * `false` otherwise. |
||
235 | */ |
||
236 | 10 | public function hasWordCuts() |
|
240 | |||
241 | /** |
||
242 | * Fits the added cells into the given maximum total width with the given |
||
243 | * number of columns. |
||
244 | * |
||
245 | * @param int $maxTotalWidth The maximum total width of the columns. |
||
246 | * @param int $nbColumns The number of columns to use. |
||
247 | * @param Formatter $formatter The formatter used to remove style tags. |
||
248 | */ |
||
249 | 21 | public function fit($maxTotalWidth, $nbColumns, Formatter $formatter) |
|
261 | |||
262 | 21 | private function resetState($maxTotalWidth, $nbColumns) |
|
273 | |||
274 | 21 | private function initRows(Formatter $formatter) |
|
306 | |||
307 | 14 | private function wrapColumns(Formatter $formatter) |
|
363 | |||
364 | 14 | private function wrapColumn($col, $columnLength, Formatter $formatter) |
|
392 | |||
393 | 14 | private function refreshColumnLength($col) |
|
401 | } |
||
402 |
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: