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 |
||
27 | class PayOutQuery extends AbstractQuery implements PayOutQueryInterface |
||
28 | { |
||
29 | /** |
||
30 | * @var PayOutTranslator |
||
31 | */ |
||
32 | private $payOutTranslator; |
||
33 | |||
34 | public function __construct( |
||
45 | |||
46 | public function createBankWire(PayOutBankWire $payOutBankWire) : PayOutBankWire |
||
47 | { |
||
48 | try { |
||
49 | $payout = $this->payOutTranslator->translatePayOutDtoToMango($payOutBankWire); |
||
50 | $createdPayout = $this->mangoPayApi->PayOuts->Create($payout); |
||
51 | |||
52 | if ($createdPayout instanceof PayOut) { |
||
53 | |||
54 | if ($createdPayout->Status == PayOutStatus::Created) { |
||
55 | return $this->payOutTranslator->translateMangoToPayOutDto($createdPayout); |
||
56 | } |
||
57 | $this->logger->addCritical( |
||
58 | "Pay-Out has been created with status: ".$createdPayout->Status . ' (result code: ' . $createdPayout->ResultCode . ' '. $createdPayout->ResultMessage.')' |
||
59 | ); |
||
60 | throw new PartFireException( |
||
61 | "Pay-Out has been created with status: ".$createdPayout->Status . ' (result code: ' . $createdPayout->ResultCode . ' '. $createdPayout->ResultMessage.')' |
||
62 | ); |
||
63 | } |
||
64 | $this->logger->addCritical("Failed to create PayOut"); |
||
65 | throw new PartFireException("Failed to create PayOut"); |
||
66 | } catch (ResponseException $e) { |
||
67 | $this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]); |
||
68 | throw new PartFireException($e->getMessage(), $e->getCode(), $e); |
||
69 | } catch (Exception $e) { |
||
70 | $this->logger->addError($e->getMessage()); |
||
71 | throw new PartFireException($e->getMessage(), $e->getCode(), $e); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | View Code Duplication | public function getBankWire(string $payOutId): PayOutBankWire |
|
92 | } |
||
93 |
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.