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 |
||
| 33 | class ComposerProcessService implements ComposerServiceInterface |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $appConfig; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var EntityManagerInterface |
||
| 42 | */ |
||
| 43 | protected $entityManager; |
||
| 44 | |||
| 45 | private $workingDir; |
||
| 46 | private $composerFile; |
||
| 47 | private $composerSetup; |
||
| 48 | private $pathPHP; |
||
| 49 | |||
| 50 | public function __construct($appConfig, $entityManager, $pathPHP) |
||
|
|
|||
| 51 | { |
||
| 52 | $this->appConfig = $appConfig; |
||
| 53 | $this->entityManager = $entityManager; |
||
| 54 | $this->pathPHP = $pathPHP; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * This function to install a plugin by composer require |
||
| 59 | * |
||
| 60 | * @param string $packageName format foo/bar or foo/bar:1.0.0 or "foo/bar 1.0.0" |
||
| 61 | * @return bool |
||
| 62 | */ |
||
| 63 | View Code Duplication | public function execRequire($packageName) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * This function to remove a plugin by composer remove |
||
| 81 | * Note: Remove with dependency, if not, please add " --no-update-with-dependencies" |
||
| 82 | * |
||
| 83 | * @param string $packageName format foo/bar or foo/bar:1.0.0 or "foo/bar 1.0.0" |
||
| 84 | * @return bool |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function execRemove($packageName) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Run command |
||
| 106 | * |
||
| 107 | * @param string $command |
||
| 108 | * @return void |
||
| 109 | */ |
||
| 110 | public function runCommand($command) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Set working dir |
||
| 120 | * @param string $workingDir |
||
| 121 | */ |
||
| 122 | public function setWorkingDir($workingDir) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Set init |
||
| 129 | */ |
||
| 130 | private function init() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Check composer file and setup it |
||
| 158 | */ |
||
| 159 | private function setupComposer() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get grep memory_limit | Megabyte |
||
| 177 | * @return int|string |
||
| 178 | */ |
||
| 179 | public function getCliMemoryLimit(){ |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Check to set new value grep "memory_limit" |
||
| 210 | * @return bool |
||
| 211 | */ |
||
| 212 | public function isSetCliMemoryLimit() |
||
| 213 | { |
||
| 214 | $oldMemory = exec($this->pathPHP.' -i | grep "memory_limit"'); |
||
| 215 | $tmpMem = '1.5GB'; |
||
| 216 | |||
| 217 | if ($oldMemory) { |
||
| 218 | $memory = explode('=>', $oldMemory); |
||
| 219 | $originGrepMemmory = trim($memory[2]); |
||
| 220 | |||
| 221 | if ($originGrepMemmory == $tmpMem) { |
||
| 222 | $tmpMem = '1.49GB'; |
||
| 223 | } |
||
| 224 | |||
| 225 | $newMemory = exec($this->pathPHP.' -d memory_limit='.$tmpMem.' -i | grep "memory_limit"'); |
||
| 226 | if ($newMemory) { |
||
| 227 | $newMemory = explode('=>', $newMemory); |
||
| 228 | $grepNewMemory = trim($newMemory[2]); |
||
| 229 | if ($grepNewMemory != $originGrepMemmory) { |
||
| 230 | |||
| 231 | return true; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | return false; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Check php command line |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | public function isPhpCommandLine() |
||
| 254 | } |
||
| 255 |