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 |
||
| 25 | class TaskRunner implements TransporterAwareInterface |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var Task[] |
||
| 29 | */ |
||
| 30 | protected $tasks = array(); |
||
| 31 | protected $needsNewline = false; |
||
| 32 | protected $transporter; |
||
| 33 | protected $dispatcher; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Constructor. |
||
| 37 | * |
||
| 38 | * @todo I think it's better NOT to have the taskrunner depend on the IOinterface |
||
| 39 | * |
||
| 40 | * @param IOInterface $io |
||
| 41 | * @param EventDispatcherInterface $dispatcher |
||
| 42 | */ |
||
| 43 | 3 | public function __construct(IOInterface $io, EventDispatcherInterface $dispatcher = null) |
|
| 48 | |||
| 49 | public function addTask(Task $task) |
||
| 53 | |||
| 54 | public function insertTask(Task $task) |
||
| 58 | |||
| 59 | public function hasTasks() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param Task[] $tasks |
||
| 66 | * @return $this |
||
| 67 | */ |
||
| 68 | 3 | public function setTasks(array $tasks) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @return Task[] |
||
| 77 | */ |
||
| 78 | 2 | public function getTasks() |
|
| 82 | |||
| 83 | public function setTransporter($transporter) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $target |
||
| 90 | * @param \Webcreate\Conveyor\Repository\Version $version |
||
| 91 | */ |
||
| 92 | 2 | public function execute($target, Version $version) |
|
| 93 | { |
||
| 94 | 2 | $total = count($this->tasks); |
|
| 95 | |||
| 96 | 2 | foreach ($this->tasks as $i => $task) { |
|
| 97 | 2 | while (true) { |
|
| 98 | 2 | $this->dispatch(TaskRunnerEvents::TASKRUNNER_PRE_EXECUTE_TASK, |
|
| 99 | 2 | new GenericEvent($task, array('index' => $i, 'total' => $total)) |
|
| 100 | ); |
||
| 101 | |||
| 102 | 2 | $result = null; |
|
| 103 | |||
| 104 | try { |
||
| 105 | 2 | if ($task instanceof TransporterAwareInterface) { |
|
| 106 | $task->setTransporter($this->transporter); |
||
| 107 | } |
||
| 108 | |||
| 109 | 2 | $result = $task->execute($target, $version); |
|
| 110 | |||
| 111 | 2 | break; |
|
| 112 | } catch (\Exception $e) { |
||
| 113 | $this->io->renderException($e); |
||
| 114 | |||
| 115 | // @todo instead of relying on the IOInterface here for asking an action, |
||
| 116 | // better to trigger an event and have the listener ask an action. |
||
| 117 | if (false === $this->tryAgain()) { |
||
| 118 | break; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | // @todo might be better to trigger a different event here, smt like TASKRUNNER_RETRY_EXECUTE_TASK |
||
| 123 | $this->dispatch(TaskRunnerEvents::TASKRUNNER_POST_EXECUTE_TASK, |
||
| 124 | new GenericEvent($task, array('index' => $i, 'total' => $total, 'result' => $result)) |
||
| 125 | ); |
||
| 126 | } |
||
| 127 | |||
| 128 | 2 | $this->dispatch(TaskRunnerEvents::TASKRUNNER_POST_EXECUTE_TASK, |
|
| 129 | 2 | new GenericEvent($task, array('index' => $i, 'total' => $total, 'result' => $result)) |
|
| 130 | ); |
||
| 131 | } |
||
| 132 | 2 | } |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @param string $target |
||
| 136 | * @param \Webcreate\Conveyor\Repository\Version $version |
||
| 137 | */ |
||
| 138 | public function simulate($target, $version) |
||
| 139 | { |
||
| 140 | $io = $this->io; |
||
| 141 | |||
| 142 | foreach ($this->tasks as $i => $task) { |
||
| 143 | if ($i > 0) { |
||
| 144 | if (true === $this->needsNewline) { |
||
| 145 | $this->io->write(''); |
||
| 146 | $this->needsNewline = false; |
||
| 147 | } |
||
| 148 | $this->io->write(''); |
||
| 149 | } |
||
| 150 | |||
| 151 | $this->io->write(sprintf('- Simulating task <info>%s</info>', get_class($task))); |
||
| 152 | $this->io->increaseIndention(2); |
||
| 153 | |||
| 154 | $self = $this; |
||
| 155 | |||
| 156 | $task->setOutput(function ($output) use ($io, $self) { |
||
| 157 | $io->overwrite(sprintf('%s', $output), false); |
||
| 158 | |||
| 159 | $self->needsNewline = true; |
||
| 160 | }); |
||
| 161 | |||
| 162 | $task->simulate($target, $version); |
||
| 163 | |||
| 164 | $this->io->decreaseIndention(2); |
||
| 165 | } |
||
| 166 | |||
| 167 | if (true === $this->needsNewline) { |
||
| 168 | $this->io->write(''); |
||
| 169 | $this->needsNewline = false; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | protected function tryAgain() |
||
| 174 | { |
||
| 175 | while (true) { |
||
| 176 | try { |
||
| 177 | if (!$this->io->isInteractive()) { |
||
| 178 | $answer = 'a'; |
||
| 179 | } else { |
||
| 180 | $answer = $this->io->select( |
||
| 181 | '<info>Select an action</info>', |
||
| 182 | array( |
||
| 183 | 'a' => 'abort', |
||
| 184 | 'r' => 'retry this task', |
||
| 185 | 's' => 'skip this task and continue with the next', |
||
| 186 | ), |
||
| 187 | 'r', |
||
| 188 | 5 |
||
| 189 | ); |
||
| 190 | } |
||
| 191 | } catch (\RuntimeException $e) { |
||
| 192 | // can happen when there is no stty available |
||
| 193 | $answer = "a"; |
||
| 194 | } |
||
| 195 | |||
| 196 | View Code Duplication | switch ($answer) { |
|
| 197 | case "a": |
||
| 198 | $this->io->setIndention(0); |
||
| 199 | $this->io->write('Aborted.'); |
||
| 200 | die(); |
||
| 201 | break; |
||
| 202 | case "r": |
||
| 203 | return true; |
||
| 204 | break; |
||
| 205 | case "s": |
||
| 206 | return false; |
||
| 207 | break; |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | return true; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Dispatch event when a dispatcher is available |
||
| 216 | * |
||
| 217 | * @param string $eventName |
||
| 218 | * @param Event $event |
||
| 219 | */ |
||
| 220 | 2 | protected function dispatch($eventName, Event $event = null) |
|
| 221 | { |
||
| 222 | 2 | if (null !== $this->dispatcher) { |
|
| 223 | 2 | $this->dispatcher->dispatch($eventName, $event); |
|
| 224 | } |
||
| 225 | 2 | } |
|
| 226 | |||
| 227 | 3 | public function getDispatcher() |
|
| 231 | } |
||
| 232 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.