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 |
||
22 | class ShellTask extends Task |
||
23 | { |
||
24 | protected $cwd; |
||
25 | protected $cli; |
||
26 | protected $io; |
||
|
|||
27 | protected $commandline; |
||
28 | |||
29 | 2 | public function __construct($cwd, Cli $cli, IOInterface $io = null) |
|
35 | |||
36 | /** |
||
37 | * @todo improve output (also @see SshTask) |
||
38 | * |
||
39 | * @param $target |
||
40 | * @param Version $version |
||
41 | * @return ExecuteResult |
||
42 | * @throws \Symfony\Component\Process\Exception\ProcessFailedException |
||
43 | */ |
||
44 | 2 | public function execute($target, Version $version = null) |
|
45 | { |
||
46 | 2 | $this->commandline = $this->options['command']; |
|
47 | 2 | foreach ($this->commandline as $command) { |
|
48 | 2 | $this->output(sprintf('Executing: <comment>%s</comment>', $command)); |
|
49 | |||
50 | 2 | $hasOutput = false; |
|
51 | |||
52 | 2 | $self = $this; |
|
53 | View Code Duplication | $outputter = function ($type, $buffer) use ($self, &$hasOutput) { |
|
54 | if (false === $self->io->isVerbose()) return; |
||
55 | |||
56 | if (1 || 'out' === $type) { |
||
57 | if (!$hasOutput) { |
||
58 | $this->io->write(''); |
||
59 | $this->io->write(''); |
||
60 | $hasOutput = true; |
||
61 | } |
||
62 | |||
63 | $lines = explode("\n", $buffer); |
||
64 | foreach ($lines as $line) { |
||
65 | if ($output = trim($line, "\r\n")) { |
||
66 | $self->io->write(sprintf('> %s', $output)); |
||
67 | } |
||
68 | } |
||
69 | } |
||
70 | 2 | }; |
|
71 | |||
72 | 2 | if ($this->cli->execute($command, $outputter, $this->cwd) <> 0) { |
|
73 | throw new ProcessFailedException($this->cli->getProcess()); |
||
74 | } |
||
75 | // elseif ($message = $this->cli->getErrorOutput()) { |
||
76 | // $messages = explode("\n", $message); |
||
77 | // |
||
78 | // $self->io->write(sprintf('<comment>%s</comment>', $messages)); |
||
79 | // } |
||
80 | 2 | } |
|
81 | |||
82 | 2 | return new ExecuteResult( |
|
83 | 2 | $this->getDerivedFiles(), |
|
84 | 2 | $this->getRemovedFiles() |
|
85 | 2 | ); |
|
86 | |||
87 | // return $this->cli->getOutput(); |
||
88 | } |
||
89 | |||
90 | public function simulate($target, Version $version) |
||
91 | { |
||
92 | $this->commandline = $this->options['command']; |
||
93 | foreach ($this->commandline as $command) { |
||
94 | $this->output(sprintf('Executing: <comment>%s</comment>', $command)); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | 2 | protected function getDerivedFiles() |
|
106 | |||
107 | 2 | protected function getRemovedFiles() |
|
115 | } |
||
116 |
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.