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 | abstract class AbstractCommand extends Command |
||
| 23 | { |
||
| 24 | const FOLDER_RESOURCES = '/../../app/Resources/config/'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var InputInterface |
||
| 28 | */ |
||
| 29 | protected $oInput; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var OutputInterface |
||
| 33 | */ |
||
| 34 | protected $oOutput; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var ContainerBuilder |
||
| 38 | */ |
||
| 39 | private static $oContainer; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private static $sHomeDir = ''; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Executes the current command. |
||
| 48 | * |
||
| 49 | * This method is not abstract because you can use this class |
||
| 50 | * as a concrete class. In this case, instead of defining the |
||
| 51 | * execute() method, you set the code to execute by passing |
||
| 52 | * a Closure to the setCode() method. |
||
| 53 | * |
||
| 54 | * @param InputInterface $oInput An InputInterface instance |
||
| 55 | * @param OutputInterface $oOutput An OutputInterface instance |
||
| 56 | * |
||
| 57 | * @return integer null or 0 if everything went fine, or an error code |
||
| 58 | * |
||
| 59 | * @throws \LogicException When this abstract method is not implemented |
||
| 60 | * |
||
| 61 | * @see setCode() |
||
| 62 | */ |
||
| 63 | 10 | protected function execute(InputInterface $oInput, OutputInterface $oOutput) |
|
| 80 | |||
| 81 | /** |
||
| 82 | * @return int |
||
| 83 | */ |
||
| 84 | abstract protected function process(); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @return ContainerBuilder |
||
| 88 | */ |
||
| 89 | protected function getContainer() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | 3 | protected function isAppRunable() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | 1 | protected function getHomeDir() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | 1 | protected function getCacheDir() |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | protected function getWorkingDir() |
||
| 178 | } |
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.