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 |
||
32 | class Application extends \Symfony\Component\Console\Application { |
||
33 | |||
34 | /** @var Container */ |
||
35 | public static $container; |
||
36 | |||
37 | |||
38 | /** @var Container */ |
||
39 | protected $diContainer; |
||
40 | |||
41 | /** |
||
42 | * Pass Pimple container into application |
||
43 | * @param Container $container |
||
44 | */ |
||
45 | public function setContainer(Container $container){ |
||
49 | |||
50 | /** |
||
51 | * Get Pimple container |
||
52 | * @return Container |
||
53 | */ |
||
54 | public function getContainer(){ |
||
57 | |||
58 | /** |
||
59 | * Get logger instance |
||
60 | * @return \Psr\Log\LoggerInterface |
||
61 | */ |
||
62 | public function getLogger(){ |
||
65 | |||
66 | /** |
||
67 | * Log exception with trace |
||
68 | * @param \Exception $e |
||
69 | */ |
||
70 | public function logException($e){ |
||
75 | |||
76 | public function doRun(InputInterface $input, OutputInterface $output){ |
||
111 | |||
112 | protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output){ |
||
130 | |||
131 | /** |
||
132 | * Check for owncloud instance |
||
133 | * @throws \RuntimeException |
||
134 | */ |
||
135 | protected function assertOwnCloudFound(){ |
||
136 | $container = $this->getContainer(); |
||
137 | /** @var Locator $locator */ |
||
138 | $locator = $container['utils.locator']; |
||
139 | |||
140 | $file = $locator->getPathToVersionFile(); |
||
141 | View Code Duplication | if (!file_exists($file) || !is_file($file)){ |
|
|
|||
142 | throw new \RuntimeException('ownCloud is not found in ' . dirname($file)); |
||
143 | } |
||
144 | |||
145 | // assert minimum version |
||
146 | $installedVersion = implode('.', $locator->getInstalledVersion()); |
||
147 | if (version_compare($installedVersion, '9.0.0', '<')) { |
||
148 | throw new \RuntimeException("Minimum ownCloud version 9.0.0 is required for the updater - $installedVersion was found in " . $locator->getOwncloudRootPath()); |
||
149 | } |
||
150 | |||
151 | // has to be installed |
||
152 | $file = $locator->getPathToConfigFile(); |
||
153 | View Code Duplication | if (!file_exists($file) || !is_file($file)){ |
|
154 | throw new \RuntimeException('ownCloud in ' . dirname(dirname($file)) . ' is not installed.'); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Create proper directory structure to store data |
||
160 | */ |
||
161 | protected function initDirectoryStructure(){ |
||
175 | |||
176 | } |
||
177 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.