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 |
||
15 | abstract class AbstractApplication implements RunInterface |
||
16 | { |
||
17 | /** Base eXpansion callbacks. */ |
||
18 | const EVENT_BEFORE_INIT = "expansion.before_init"; |
||
19 | const EVENT_SWITCH_TO_SCRIPT = "expansion.switched_to_script"; |
||
20 | const EVENT_AFTER_INIT = "expansion.after_init"; |
||
21 | const EVENT_READY = "expansion.ready"; |
||
22 | const EVENT_STOP = "expansion.stop"; |
||
23 | |||
24 | const EXPANSION_VERSION = "dev"; |
||
25 | |||
26 | /** @var Factory */ |
||
27 | protected $factory; |
||
28 | |||
29 | /** @var DispatcherInterface */ |
||
30 | protected $dispatcher; |
||
31 | |||
32 | /** @var Console */ |
||
33 | protected $console; |
||
34 | |||
35 | /** @var LoggerInterface */ |
||
36 | protected $logger; |
||
37 | |||
38 | /** @var Version */ |
||
39 | protected $version; |
||
40 | |||
41 | /** @var bool */ |
||
42 | protected $isRunning = true; |
||
43 | |||
44 | |||
45 | /** |
||
46 | * AbstractApplication constructor. |
||
47 | * |
||
48 | * @param DispatcherInterface $dispatcher |
||
49 | * @param Factory $factory |
||
50 | * @param Console $output |
||
51 | * @param LoggerInterface $logger |
||
52 | * @param Version $version |
||
53 | */ |
||
54 | 10 | View Code Duplication | public function __construct( |
67 | |||
68 | /** |
||
69 | * Initialize eXpansion. |
||
70 | * |
||
71 | * @param OutputInterface $console |
||
72 | * |
||
73 | * @return $this|mixed |
||
74 | * @throws \Maniaplanet\DedicatedServer\Xmlrpc\TransportException |
||
75 | */ |
||
76 | 1 | public function init(OutputInterface $console) |
|
110 | |||
111 | 1 | protected function checkPhpExtensions(OutputInterface $console) |
|
112 | { |
||
113 | $extensions = array( |
||
114 | 1 | 'openssl' => 'extension=php_openssl.dll', |
|
115 | 'curl' => 'extension=curl.dll', |
||
116 | ); |
||
117 | 1 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
|
118 | $extensions['com_dotnet'] = 'extension=php_com_dotnet.dll'; |
||
119 | } |
||
120 | |||
121 | 1 | $status = true; |
|
122 | 1 | $showIni = false; |
|
123 | 1 | View Code Duplication | foreach ($extensions as $extension => $description) { |
124 | 1 | if (!extension_loaded($extension)) { |
|
125 | $console->writeln( |
||
126 | "<error>eXpansion needs PHP extension $extension to run. Enable it to run eXpansion => " . $description . "</error>" |
||
127 | ); |
||
128 | $status = false; |
||
129 | $showIni = true; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | $recommend = array( |
||
134 | 1 | 'xmlrpc' => "It will have better performances !", |
|
135 | ); |
||
136 | 1 | View Code Duplication | foreach ($recommend as $extension => $reason) { |
137 | 1 | if (!extension_loaded($extension)) { |
|
138 | $console->writeln( |
||
139 | "<error>eXpansion works better with PHP extension</error> <info>$extension</info>: " . $reason . "" |
||
140 | ); |
||
141 | $showIni = true; |
||
142 | } |
||
143 | } |
||
144 | |||
145 | 1 | if ($showIni) { |
|
146 | $console->writeln('<info>[PHP] PHP is using fallowing ini file :</info> "'. php_ini_loaded_file() .'"'); |
||
147 | sleep(5); |
||
148 | } |
||
149 | 1 | return $status; |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * Run eXpansion |
||
154 | * |
||
155 | * @inheritdoc |
||
156 | */ |
||
157 | 2 | public function run() |
|
214 | |||
215 | /** |
||
216 | * Stop eXpansion. |
||
217 | */ |
||
218 | 2 | public function stopApplication() |
|
223 | |||
224 | abstract protected function executeRun(); |
||
225 | } |
||
226 |
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.