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 |
||
| 19 | class TestSocket implements ManagementSocketInterface |
||
| 20 | { |
||
| 21 | /** @var bool */ |
||
| 22 | private $connectFail; |
||
| 23 | |||
| 24 | /** @var string|null */ |
||
| 25 | private $socketAddress; |
||
| 26 | |||
| 27 | public function __construct($connectFail = false) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Open the socket. |
||
| 35 | * |
||
| 36 | * @param string $socketAddress the socket to connect to, e.g.: |
||
| 37 | * "tcp://localhost:7505" |
||
| 38 | * @param int $timeOut the amount of time to wait before |
||
| 39 | * giving up on trying to connect |
||
| 40 | * |
||
| 41 | * @throws \SURFnet\VPN\Server\OpenVpn\Exception\ServerSocketException if the socket cannot be opened |
||
| 42 | * within timeout |
||
| 43 | */ |
||
| 44 | public function open($socketAddress, $timeOut = 5) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Send an OpenVPN command and get the response. |
||
| 54 | * |
||
| 55 | * @param string $command a OpenVPN management command and parameters |
||
| 56 | * |
||
| 57 | * @return array the response lines as array values |
||
| 58 | * |
||
| 59 | * @throws \SURFnet\VPN\Server\OpenVpn\Exception\ServerSocketException in case read/write fails or |
||
| 60 | * socket is not open |
||
| 61 | */ |
||
| 62 | public function command($command) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Close the socket connection. |
||
| 82 | * |
||
| 83 | * @throws \SURFnet\VPN\Server\OpenVpn\Exception\ServerSocketException if socket is not open |
||
| 84 | */ |
||
| 85 | public function close() |
||
| 89 | } |
||
| 90 |
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.