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 |
||
| 32 | class PowerRepository |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * The Eloquent Model associated with the requested server. |
||
| 36 | * |
||
| 37 | * @var \Pterodactyl\Models\Server |
||
| 38 | */ |
||
| 39 | protected $server; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The Eloquent Model associated with the user to run the request as. |
||
| 43 | * |
||
| 44 | * @var \Pterodactyl\Models\User|null |
||
| 45 | */ |
||
| 46 | protected $user; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Constuctor for repository. |
||
| 50 | * |
||
| 51 | * @param \Pterodactyl\Models\Server $server |
||
| 52 | * @param \Pterodactyl\Models\User|null $user |
||
| 53 | * @return void |
||
|
|
|||
| 54 | */ |
||
| 55 | public function __construct(Server $server, User $user = null) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Sends a power option to the daemon. |
||
| 63 | * |
||
| 64 | * @param string $action |
||
| 65 | * @return string |
||
| 66 | * |
||
| 67 | * @throws \GuzzleHttp\Exception\RequestException |
||
| 68 | * @throws \Pterodactyl\Exceptions\DisplayException |
||
| 69 | */ |
||
| 70 | View Code Duplication | public function do($action) |
|
| 71 | { |
||
| 72 | try { |
||
| 73 | $response = $this->server->guzzleClient($this->user)->request('PUT', '/server/power', [ |
||
| 74 | 'http_errors' => false, |
||
| 75 | 'json' => [ |
||
| 76 | 'action' => $action, |
||
| 77 | ], |
||
| 78 | ]); |
||
| 79 | |||
| 80 | if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) { |
||
| 81 | throw new DisplayException('Power toggle endpoint responded with a non-200 error code (HTTP/' . $response->getStatusCode() . ').'); |
||
| 82 | } |
||
| 83 | |||
| 84 | return $response->getBody(); |
||
| 85 | } catch (ConnectException $ex) { |
||
| 86 | throw $ex; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Starts a server. |
||
| 92 | * |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | public function start() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Stops a server. |
||
| 102 | * |
||
| 103 | * @return void |
||
| 104 | */ |
||
| 105 | public function stop() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Restarts a server. |
||
| 112 | * |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | public function restart() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Kills a server. |
||
| 122 | * |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | public function kill() |
||
| 129 | } |
||
| 130 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.