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); |
||
| 46 | class GlobalScaleController extends BaseController { |
||
| 47 | |||
| 48 | |||
| 49 | use TStringTools; |
||
| 50 | use TAsync; |
||
| 51 | |||
| 52 | |||
| 53 | /** |
||
| 54 | * Event is generated by any instance of GS and sent to the instance that owns the Circles, that |
||
| 55 | * will broadcast the event to other if ok |
||
| 56 | * |
||
| 57 | * @PublicPage |
||
| 58 | * @NoCSRFRequired |
||
| 59 | * |
||
| 60 | * @return DataResponse |
||
| 61 | */ |
||
| 62 | View Code Duplication | public function event(): DataResponse { |
|
|
|
|||
| 63 | $data = file_get_contents('php://input'); |
||
| 64 | |||
| 65 | try { |
||
| 66 | $event = new GSEvent(); |
||
| 67 | $event->importFromJson($data); |
||
| 68 | $this->gsDownstreamService->requestedEvent($event); |
||
| 69 | |||
| 70 | return $this->success(['success' => $event]); |
||
| 71 | } catch (Exception $e) { |
||
| 72 | return $this->fail(['data' => $data, 'error' => $e->getMessage()]); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * Async process and broadcast the event to every instances of GS |
||
| 79 | * This should be initiated by the instance that owns the Circles. |
||
| 80 | * |
||
| 81 | * @PublicPage |
||
| 82 | * @NoCSRFRequired |
||
| 83 | * |
||
| 84 | * @param string $token |
||
| 85 | * |
||
| 86 | * @throws Exception |
||
| 87 | */ |
||
| 88 | public function asyncBroadcast(string $token) { |
||
| 89 | try { |
||
| 90 | $wrappers = $this->gsUpstreamService->getEventsByToken($token); |
||
| 91 | } catch (Exception $e) { |
||
| 92 | $this->miscService->log( |
||
| 93 | 'exception during async: ' . ['token' => $token, 'error' => $e->getMessage()] |
||
| 94 | ); |
||
| 95 | $this->fail(['token' => $token, 'error' => $e->getMessage()]); |
||
| 96 | } |
||
| 97 | |||
| 98 | $this->async(); |
||
| 99 | foreach ($wrappers as $wrapper) { |
||
| 100 | try { |
||
| 101 | $this->gsUpstreamService->broadcastWrapper($wrapper, $this->request->getServerProtocol()); |
||
| 102 | } catch (GSStatusException $e) { |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | $this->gsUpstreamService->manageResults($token); |
||
| 107 | |||
| 108 | exit(); |
||
| 109 | } |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * Event is sent by instance that owns the Circles. |
||
| 114 | * |
||
| 115 | * @PublicPage |
||
| 116 | * @NoCSRFRequired |
||
| 117 | * |
||
| 118 | * @return DataResponse |
||
| 119 | */ |
||
| 120 | View Code Duplication | public function broadcast(): DataResponse { |
|
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * Status Event. This is an event to check status of items between instances. |
||
| 138 | * |
||
| 139 | * @PublicPage |
||
| 140 | * @NoCSRFRequired |
||
| 141 | * |
||
| 142 | * @return DataResponse |
||
| 143 | */ |
||
| 144 | View Code Duplication | public function status(): DataResponse { |
|
| 157 | |||
| 158 | } |
||
| 159 | |||
| 160 |
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.