Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
63 | class UbLangConsole extends Console |
||
64 | { |
||
65 | protected $currentPhase; |
||
66 | protected $currentPhaseStep; |
||
67 | protected $phaseGroupHasOutput = false; |
||
68 | protected $phaseNumber = 0; |
||
69 | protected $phaseMessages = array(); |
||
70 | |||
71 | /** |
||
72 | * a list of the results we have received from stories |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $storyResults = []; |
||
76 | |||
77 | /** |
||
78 | * called when storyplayer starts |
||
79 | * |
||
80 | * @param string $version |
||
81 | * @param string $url |
||
82 | * @param string $copyright |
||
83 | * @param string $license |
||
84 | * @return void |
||
85 | */ |
||
86 | public function startStoryplayer($version, $url, $copyright, $license) |
||
87 | { |
||
88 | $this->write("Storyplayer {$version}", $this->writer->highlightStyle); |
||
89 | $this->write(" - "); |
||
90 | $this->write($url, $this->writer->urlStyle); |
||
91 | $this->write(PHP_EOL); |
||
92 | $this->write($copyright . PHP_EOL); |
||
93 | $this->write($license . PHP_EOL . PHP_EOL); |
||
94 | } |
||
95 | |||
96 | public function endStoryplayer($duration) |
||
100 | |||
101 | /** |
||
102 | * called when we start a new set of phases |
||
103 | * |
||
104 | * @param string $activity |
||
105 | * what are we doing? (e.g. 'creating', 'running') |
||
106 | * @param string $name |
||
107 | * the name of the phase group |
||
108 | * @param array|null $details |
||
109 | * optional explanation of what this PhaseGroup is trying |
||
110 | * to achieve |
||
111 | * @return void |
||
112 | */ |
||
113 | public function startPhaseGroup($activity, $name, $details = null) |
||
114 | { |
||
115 | $this->write($activity . ' ', $this->writer->activityStyle); |
||
116 | $this->write($name, $this->writer->nameStyle); |
||
117 | $this->write(' ...' . PHP_EOL, $this->writer->punctuationStyle); |
||
118 | |||
119 | if (is_array($details)) { |
||
120 | $this->write(' Scenario: ' . PHP_EOL, $this->writer->stepStyle); |
||
|
|||
121 | foreach ($details as $line) { |
||
122 | $this->write(' ' . $line . PHP_EOL); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | // reset our tracker of any output |
||
127 | $this->phaseGroupHasOutput = false; |
||
128 | } |
||
129 | |||
130 | View Code Duplication | public function endPhaseGroup($result) |
|
131 | { |
||
132 | // tell the user what happened |
||
133 | $this->write(' Result: '); |
||
134 | if ($result->getPhaseGroupSkipped()) { |
||
135 | $this->writePhaseGroupSkipped($result->getResultString()); |
||
136 | } |
||
137 | else if ($result->getPhaseGroupSucceeded()) { |
||
138 | $this->writePhaseGroupSucceeded($result->getResultString()); |
||
139 | } |
||
140 | else { |
||
141 | $this->writePhaseGroupFailed($result->getResultString()); |
||
142 | } |
||
143 | |||
144 | // write out the duration too |
||
145 | $this->write(' (', $this->writer->punctuationStyle); |
||
146 | $this->writeDuration($result->getDuration()); |
||
147 | $this->write(')' . PHP_EOL, $this->writer->punctuationStyle); |
||
148 | |||
149 | // remember the result for the final report |
||
150 | // |
||
151 | // we have to clone as the result object apparently changes |
||
152 | // afterwards. no idea why (yet) |
||
153 | $this->results[] = clone $result; |
||
154 | |||
155 | // if we are not connected to a terminal, we need to write out |
||
156 | // a detailed error report |
||
157 | if (!function_exists("posix_isatty") || !posix_isatty(STDOUT)) { |
||
158 | $this->writeDetailedErrorReport($result); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * called when a story starts a new phase |
||
164 | * |
||
165 | * @return void |
||
166 | */ |
||
167 | public function startPhase($phase) |
||
168 | { |
||
169 | // shorthand |
||
170 | $phaseType = $phase->getPhaseType(); |
||
171 | $phaseSeqNo = $phase->getPhaseSequenceNo(); |
||
172 | |||
173 | $this->currentPhaseType = $phaseType; |
||
174 | |||
175 | // we're only interested in telling the user about the |
||
176 | // phases of a story |
||
177 | if ($phaseType !== Phase::STORY_PHASE) { |
||
178 | return; |
||
179 | } |
||
180 | |||
181 | // remember the phase for later use |
||
182 | $this->currentPhase = $phaseSeqNo; |
||
183 | $this->currentPhaseName = $phase->getPhaseName(); |
||
184 | $this->currentPhaseStep = 0; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * called when a story ends a phase |
||
189 | * |
||
190 | * @return void |
||
191 | */ |
||
192 | public function endPhase($phase, $phaseResult) |
||
193 | { |
||
194 | // shorthand |
||
195 | $phaseType = $phase->getPhaseType(); |
||
196 | |||
197 | // we're only interested in telling the user about the |
||
198 | // phases of a story |
||
199 | if ($phaseType !== Phase::STORY_PHASE) { |
||
200 | return; |
||
201 | } |
||
202 | |||
203 | // if there was no output for the phase, skip the report |
||
204 | if ($this->currentPhaseStep === 0) { |
||
205 | return; |
||
206 | } |
||
207 | |||
208 | if ($phaseResult->getPhaseFailed() || $phaseResult->getPhaseHasErrored()) { |
||
209 | $this->writePhaseGroupFailed($phaseResult->getPhaseResultString()); |
||
210 | } |
||
211 | else if ($phaseResult->getPhaseIsIncomplete() || $phaseResult->getPhaseIsBlacklisted()) { |
||
212 | $this->writePhaseGroupSkipped( $phaseResult->getPhaseResultString()); |
||
213 | } |
||
214 | $this->write(PHP_EOL); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * called when a story logs an action |
||
219 | * |
||
220 | * @param string $msg |
||
221 | * @return void |
||
222 | */ |
||
223 | public function logPhaseActivity($msg, $codeLine = null) |
||
224 | { |
||
225 | // has output been suppressed? |
||
226 | if ($this->isSilent()) { |
||
227 | return; |
||
228 | } |
||
229 | |||
230 | // we only want Story phases |
||
231 | if ($this->currentPhaseType !== Phase::STORY_PHASE) { |
||
232 | return; |
||
233 | } |
||
234 | |||
235 | // skip any empty messages (just in case) |
||
236 | if (strlen($msg) === 0) { |
||
237 | return; |
||
238 | } |
||
239 | |||
240 | // we only want the top-level messages |
||
241 | if ($msg{0} == ' ') { |
||
242 | return; |
||
243 | } |
||
244 | |||
245 | // special case - first message for a phase group |
||
246 | if (!$this->phaseGroupHasOutput && !$this->getIsVerbose()) { |
||
247 | $this->write(' Steps:' . PHP_EOL, $this->writer->stepStyle); |
||
248 | } |
||
249 | |||
250 | // special case - first message for a phase |
||
251 | if ($this->currentPhaseStep === 0 && $this->getIsVerbose()) { |
||
252 | $this->write(' ' . $this->currentPhaseName . ':' . PHP_EOL, $this->writer->stepStyle); |
||
253 | } |
||
254 | // special case - not the first message for a phase |
||
255 | if ($this->currentPhaseStep !== 0) { |
||
256 | $this->write(PHP_EOL); |
||
257 | } |
||
258 | |||
259 | $this->write(' ' . $this->currentPhase . chr(ord('a') + $this->currentPhaseStep) . '. ', $this->writer->stepStyle); |
||
260 | $this->write($msg . ' '); |
||
261 | |||
262 | $this->currentPhaseStep++; |
||
263 | $this->phaseGroupHasOutput = true; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * called when a story logs the (possibly partial) output from |
||
268 | * running a subprocess |
||
269 | * |
||
270 | * @param string $msg the output to log |
||
271 | * @return void |
||
272 | */ |
||
273 | public function logPhaseSubprocessOutput($msg) |
||
277 | |||
278 | /** |
||
279 | * called when a story logs an error |
||
280 | * |
||
281 | * @param string $phaseName |
||
282 | * @param string $msg |
||
283 | * @return void |
||
284 | */ |
||
285 | public function logPhaseError($phaseName, $msg) |
||
289 | |||
290 | /** |
||
291 | * called when a story is skipped |
||
292 | * |
||
293 | * @param string $phaseName |
||
294 | * @param string $msg |
||
295 | * @return void |
||
296 | */ |
||
297 | public function logPhaseSkipped($phaseName, $msg) |
||
301 | |||
302 | public function logPhaseCodeLine($codeLine) |
||
306 | |||
307 | /** |
||
308 | * called when the outer CLI shell encounters a fatal error |
||
309 | * |
||
310 | * @param string $msg |
||
311 | * the error message to show the user |
||
312 | * |
||
313 | * @return void |
||
314 | */ |
||
315 | public function logCliError($msg) |
||
319 | |||
320 | /** |
||
321 | * |
||
322 | * @param string $msg |
||
323 | * @param Exception $e |
||
324 | * @return void |
||
325 | */ |
||
326 | public function logCliErrorWithException($msg, $e) |
||
327 | { |
||
328 | $this->write("*** error: $msg" . PHP_EOL . PHP_EOL |
||
329 | . "This was caused by an unexpected exception " . get_class($e) . PHP_EOL . PHP_EOL |
||
330 | . $e->getTraceAsString() . PHP_EOL); |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * called when the outer CLI shell needs to publish a warning |
||
335 | * |
||
336 | * @param string $msg |
||
337 | * the warning message to show the user |
||
338 | * |
||
339 | * @return void |
||
340 | */ |
||
341 | public function logCliWarning($msg) |
||
345 | |||
346 | /** |
||
347 | * called when the outer CLI shell needs to tell the user something |
||
348 | * |
||
349 | * @param string $msg |
||
350 | * the message to show the user |
||
351 | * |
||
352 | * @return void |
||
353 | */ |
||
354 | public function logCliInfo($msg) |
||
358 | |||
359 | /** |
||
360 | * an alternative to using PHP's built-in var_dump() |
||
361 | * |
||
362 | * @param string $name |
||
363 | * a human-readable name to describe $var |
||
364 | * |
||
365 | * @param mixed $var |
||
366 | * the variable to dump |
||
367 | * |
||
368 | * @return void |
||
369 | */ |
||
370 | public function logVardump($name, $var) |
||
374 | } |
||
375 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.