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 declare(strict_types=1); |
||
| 11 | class ScriptLoader |
||
| 12 | { |
||
| 13 | const MODIFIER_IS_TTY = 'TTY: '; |
||
| 14 | |||
| 15 | const MODIFIER_IGNORE_ERROR_PREFIX = 'I: '; |
||
| 16 | |||
| 17 | const MODIFIER_DEFERRED_EXECUTION_PREFIX = 'D: '; |
||
| 18 | |||
| 19 | const INCLUDE_STATEMENT_PREFIX = 'INCLUDE: '; |
||
| 20 | |||
| 21 | const WAIT_STATEMENT = 'WAIT:'; |
||
| 22 | |||
| 23 | const TEMPLATE_STATEMENT_PREFIX = 'TEMPLATE: '; |
||
| 24 | |||
| 25 | const CONCATENATE_PREFIX = ' '; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var CommandBuilder |
||
| 29 | */ |
||
| 30 | private $commandBuilder; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param CommandBuilder $commandBuilder |
||
| 34 | */ |
||
| 35 | public function __construct(CommandBuilder $commandBuilder) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param Script $script |
||
| 42 | * @return Command[] |
||
| 43 | */ |
||
| 44 | public function loadScript(Script $script): array |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param Script $fromScript |
||
| 116 | * @param string $includeStatement |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | private function findInclude(Script $fromScript, string $includeStatement): string |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param string $command |
||
| 134 | * @return bool |
||
| 135 | */ |
||
| 136 | private function isExecutableLine(string $command): bool |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param string $needle |
||
| 153 | * @param string $haystack |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | private function removeFromStart(string $needle, string $haystack): string |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param string $needle |
||
| 163 | * @param string $haystack |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | private function startsWith(string $needle, string $haystack): bool |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param string $file |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | protected function loadFileContents(string $file): string |
||
| 179 | } |
||
| 180 |
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.