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 |
||
34 | class ComposerProcessService |
||
35 | { |
||
36 | /** |
||
37 | * @Inject("config") |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $appConfig; |
||
41 | |||
42 | private $composerFile; |
||
43 | private $composerSetup; |
||
44 | private static $vendorName = 'ec-cube'; |
||
45 | |||
46 | /** |
||
47 | * This function to install a plugin by composer require |
||
48 | * |
||
49 | * @param string $packageName |
||
50 | * @return bool |
||
51 | */ |
||
52 | View Code Duplication | public function execRequire($packageName) |
|
|
|||
53 | { |
||
54 | set_time_limit(0); |
||
55 | $this->setupComposer(); |
||
56 | // Build command |
||
57 | $packageName = self::$vendorName.'/'.$packageName; |
||
58 | $command = $this->getPHP().' '.$this->composerFile.' require '.$packageName; |
||
59 | $command .= ' --prefer-dist --no-progress --no-suggest --no-scripts --ignore-platform-reqs --profile --no-ansi --no-interaction -d '; |
||
60 | $command .= $this->appConfig['root_dir'].' 2>&1'; |
||
61 | log_info($command); |
||
62 | |||
63 | // Execute command |
||
64 | $output = array(); |
||
65 | exec($command, $output); |
||
66 | log_info(PHP_EOL.implode(PHP_EOL, $output).PHP_EOL); |
||
67 | |||
68 | return true; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * This function to remove a plugin by composer remove |
||
73 | * Note: Remove with dependency, if not, please add " --no-update-with-dependencies" |
||
74 | * |
||
75 | * @param string $packageName |
||
76 | * @return bool |
||
77 | */ |
||
78 | View Code Duplication | public function execRemove($packageName) |
|
79 | { |
||
80 | set_time_limit(0); |
||
81 | $this->setupComposer(); |
||
82 | // Build command |
||
83 | $packageName = self::$vendorName.'/'.$packageName; |
||
84 | $command = $this->getPHP().' '.$this->composerFile.' remove '.$packageName; |
||
85 | $command .= ' --no-progress --no-scripts --ignore-platform-reqs --profile --no-ansi --no-interaction -d '; |
||
86 | $command .= $this->appConfig['root_dir'].' 2>&1'; |
||
87 | log_info($command); |
||
88 | |||
89 | // Execute command |
||
90 | $output = array(); |
||
91 | exec($command, $output); |
||
92 | log_info(PHP_EOL.implode(PHP_EOL, $output).PHP_EOL); |
||
93 | |||
94 | return true; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Get environment php command |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | private function getPHP() |
||
106 | |||
107 | /** |
||
108 | * Check composer file and setup it |
||
109 | */ |
||
110 | private function setupComposer() |
||
131 | } |
||
132 |
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.