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 |
||
28 | class PayInQuery extends AbstractQuery implements PayInQueryInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var PayInTranslator |
||
32 | */ |
||
33 | private $payInTranslator; |
||
34 | |||
35 | public function __construct( |
||
46 | |||
47 | public function createPayInCardDirect(CardDirectPayIn $cardDirectPayIn) : CardDirectPayIn |
||
66 | |||
67 | public function createBankWireDirectPayIn(BankwireDirectPayIn $bankwireDirectPayIn) : BankwireDirectPayIn |
||
68 | { |
||
69 | try { |
||
70 | $payIn = $this->payInTranslator->translateDtoToMangoPayInForBankwireDirectPayIn($bankwireDirectPayIn); |
||
71 | $createdPayIn = $this->mangoPayApi->PayIns->Create($payIn); |
||
72 | |||
73 | if ($createdPayIn instanceof PayIn) { |
||
74 | |||
75 | if ($createdPayIn->Status == PayInStatus::Created) { |
||
76 | return $this->payInTranslator->translateMangoPayBankwireDirectPayInToDto($createdPayIn); |
||
77 | } |
||
78 | $this->logger->addCritical( |
||
79 | "Pay-In has been created with status: ".$createdPayIn->Status . ' (result code: ' . $createdPayIn->ResultCode . ')' |
||
80 | ); |
||
81 | throw new PartFireException( |
||
82 | "Pay-In has been created with status: ".$createdPayIn->Status . ' (result code: ' . $createdPayIn->ResultCode . ')' |
||
83 | ); |
||
84 | } |
||
85 | $this->logger->addCritical("Failed to create PayIn"); |
||
86 | throw new PartFireException("Failed to create PayIn"); |
||
87 | } catch (ResponseException $e) { |
||
88 | $this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]); |
||
89 | throw new PartFireException($e->getMessage(), $e->getCode(), $e); |
||
90 | } catch (Exception $e) { |
||
91 | $this->logger->addError($e->getMessage()); |
||
92 | throw new PartFireException($e->getMessage(), $e->getCode(), $e); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | /* |
||
97 | * TODO: Sort the return type for this |
||
98 | * |
||
99 | */ |
||
100 | |||
101 | View Code Duplication | public function get($id) |
|
123 | } |
||
124 |
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.