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 |
||
19 | class RoboFileFixture extends \Robo\Tasks implements LoggerAwareInterface, CustomEventAwareInterface |
||
20 | { |
||
21 | use LoggerAwareTrait; |
||
22 | use CustomEventAwareTrait; |
||
23 | |||
24 | /** |
||
25 | * Demonstrate Robo variable argument passing. |
||
26 | * |
||
27 | * @param array $a |
||
28 | * A list of commandline parameters. |
||
29 | */ |
||
30 | public function testArrayArgs(array $a) |
||
31 | { |
||
32 | $this->say("The parameters passed are:\n" . var_export($a, true)); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Demonstrate use of SymfonyStyle |
||
37 | */ |
||
38 | public function testSymfonyStyle() |
||
39 | { |
||
40 | $this->io()->title('My Title'); |
||
|
|||
41 | $this->io()->section('Section 1'); |
||
42 | $this->io()->text('Some text in section one.'); |
||
43 | $this->io()->comment('This is just an example of different styles.'); |
||
44 | $this->io()->section('Section 2'); |
||
45 | $this->io()->text('Some text in section two.'); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Demonstrate use of SymfonyStyle with a style injector |
||
50 | */ |
||
51 | public function testStyleInjector(SymfonyStyle $io) |
||
60 | |||
61 | /** |
||
62 | * @hook command-event test:command-event |
||
63 | */ |
||
64 | public function hookCommandEvent() |
||
68 | |||
69 | public function testCommandEvent() |
||
73 | |||
74 | /** |
||
75 | * @hook post-command test:command-event |
||
76 | */ |
||
77 | public function hookPostCommand() |
||
81 | |||
82 | /** |
||
83 | * This command uses a custom event 'custom-event' to collect data. Note that |
||
84 | * the event handlers will not be found unless the hook manager is |
||
85 | * injected into this command handler object via `setHookManager()` |
||
86 | * (defined in CustomEventAwareTrait). The Robo DI container does this |
||
87 | * for us through inflection. |
||
88 | * |
||
89 | * @command test:custom-event |
||
90 | */ |
||
91 | public function testCustomEvent() |
||
101 | |||
102 | /** |
||
103 | * @hook on-event custom-event |
||
104 | */ |
||
105 | public function hookOne() |
||
109 | |||
110 | /** |
||
111 | * @hook on-event custom-event |
||
112 | */ |
||
113 | public function hookTwo() |
||
117 | |||
118 | /** |
||
119 | * Test handling of options |
||
120 | * |
||
121 | * @field-labels |
||
122 | * a: A |
||
123 | * b: B |
||
124 | */ |
||
125 | public function testSimpleList($options = ['a' => '1', 'b' => '2', 'format' => 'yaml']) |
||
130 | |||
131 | /** |
||
132 | * Demonstrate Robo error output and command failure. |
||
133 | */ |
||
134 | public function testError() |
||
135 | { |
||
136 | $this->io()->text(var_export(\Robo\Robo::config()->export(), true)); |
||
137 | return $this->taskExec('ls xyzzy' . date('U'))->dir('/tmp')->run(); |
||
138 | } |
||
139 | |||
140 | public function testExec() |
||
144 | |||
145 | /** |
||
146 | * Demonstrate what happens when a command or a task |
||
147 | * throws an exception. Note that typically, Robo commands |
||
148 | * should return Result objects rather than throw exceptions. |
||
149 | */ |
||
150 | public function testException($options = ['task' => false]) |
||
151 | { |
||
152 | if (!$options['task']) { |
||
153 | throw new \RuntimeException('Command failed with an exception.'); |
||
154 | } |
||
155 | throw new \RuntimeException('Task failed with an exception.'); |
||
156 | } |
||
157 | |||
158 | public function testStopOnFail() |
||
159 | { |
||
160 | $this->stopOnFail(); |
||
161 | $this->collectionBuilder() |
||
162 | ->taskExec('ls xyzzy' . date('U')) |
||
163 | ->dir('/tmp') |
||
164 | ->run(); |
||
165 | |||
166 | // stopOnFail() should cause the failed task to throw an exception, |
||
167 | // so we should not get here, and instead exit the program with a |
||
168 | // non-zero status. |
||
169 | return 0; |
||
170 | } |
||
171 | |||
172 | public function testVerbosity() |
||
173 | { |
||
174 | $this->output()->writeln('This command will print more information at higher verbosity levels.'); |
||
175 | $this->output()->writeln('Try running with -v, -vv or -vvv'); |
||
176 | $this->output()->writeln('The current verbosity level is ' . $this->output()->getVerbosity()); |
||
177 | $this->output()->writeln('This is a verbose message (-v).', OutputInterface::VERBOSITY_VERBOSE); |
||
178 | $this->output()->writeln('This is a very verbose message (-vv).', OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
179 | $this->output()->writeln('This is a debug message (-vvv).', OutputInterface::VERBOSITY_DEBUG); |
||
180 | $this->logger->warning('This is a warning log message.'); |
||
181 | $this->logger->notice('This is a notice log message.'); |
||
182 | $this->logger->debug('This is a debug log message.'); |
||
183 | } |
||
184 | |||
185 | public function testVerbosityThreshold() |
||
202 | |||
203 | public function testDeploy() |
||
204 | { |
||
205 | $gitTask = $this->taskGitStack() |
||
206 | ->pull(); |
||
207 | |||
208 | $this->taskSshExec('mysite.com') |
||
209 | ->remoteDir('/var/www/somesite') |
||
210 | ->exec($gitTask) |
||
211 | ->run(); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Demonstrate use of Symfony $input object in Robo in place of |
||
216 | * the usual "parameter arguments". |
||
217 | * |
||
218 | * @param InputInterface $input |
||
219 | * @arg array $a A list of commandline parameters. |
||
220 | * @option foo |
||
221 | * @default a [] |
||
222 | * @default foo [] |
||
223 | */ |
||
224 | View Code Duplication | public function testSymfony(InputInterface $input) |
|
233 | } |
||
234 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.