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 |
||
22 | class VendaApi extends AbstractAPI |
||
23 | { |
||
24 | /** |
||
25 | * @var PedidoApi |
||
26 | */ |
||
27 | private $pedidoApi; |
||
28 | |||
29 | /** |
||
30 | * VenderApi constructor. |
||
31 | */ |
||
32 | public function __construct(Client $client = null) |
||
37 | |||
38 | /** |
||
39 | * @param Contracts\Venda\VenderRequest $venderRequest |
||
40 | * @return Contracts\Venda\VenderResponse |
||
41 | * @throws \Exception |
||
42 | */ |
||
43 | public function vender(Contracts\Venda\VenderRequest $venderRequest) |
||
44 | { |
||
45 | try{ |
||
46 | |||
47 | if(empty($venderRequest->getTerminalId())) |
||
48 | $venderRequest->setTerminalId( |
||
49 | $this->_client->getParameter(ControlPayParameterConst::CONTROLPAY_DEFAULT_TERMINAL_ID) |
||
50 | ); |
||
51 | |||
52 | if(empty($venderRequest->getFormaPagamentoId())) |
||
53 | $venderRequest->setFormaPagamentoId( |
||
54 | $this->_client->getParameter(ControlPayParameterConst::CONTROLPAY_DEFAULT_FORMA_PAGAMENTO_ID) |
||
55 | ); |
||
56 | |||
57 | if(empty($venderRequest->isAguardarTefIniciarTransacao())) |
||
58 | $venderRequest->setAguardarTefIniciarTransacao( |
||
59 | boolval( |
||
60 | $this->_client->getParameter(ControlPayParameterConst::CONTROLPAY_DEFAULT_FORMA_AGUARDA_TEF) |
||
61 | ) |
||
62 | ); |
||
63 | |||
64 | View Code Duplication | foreach ($venderRequest->getProdutosVendidos() as $key => $produto) |
|
65 | { |
||
66 | if(empty($produto->getId())) |
||
67 | $venderRequest->getProdutosVendidos()[$key]->setId( |
||
68 | $this->_client->getParameter(ControlPayParameterConst::CONTROLPAY_DEFAULT_PRODUTO_ID) |
||
69 | ); |
||
70 | } |
||
71 | |||
72 | $this->response = $this->_httpClient->post(__FUNCTION__,[ |
||
73 | 'body' => json_encode($venderRequest), |
||
74 | ]); |
||
75 | |||
76 | return SerializerHelper::denormalize( |
||
77 | $this->response->json(), |
||
78 | Contracts\Venda\VenderResponse::class |
||
79 | ); |
||
80 | }catch (RequestException $ex) { |
||
81 | $this->requestException($ex); |
||
82 | }catch (\Exception $ex){ |
||
83 | throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param Contracts\Venda\VenderComPedidoRequest $venderComPedidoRequest |
||
89 | * @return Contracts\Venda\VenderResponse |
||
90 | * @throws \Exception |
||
91 | */ |
||
92 | public function venderComPedido(Contracts\Venda\VenderComPedidoRequest $venderComPedidoRequest) |
||
111 | |||
112 | /** |
||
113 | * @param Contracts\Venda\CancelarVendaRequest $cancelarVendaRequest |
||
114 | * @return Contracts\Venda\CancelarVendaResponse |
||
115 | * @throws \Exception |
||
116 | */ |
||
117 | public function cancelarVenda(Contracts\Venda\CancelarVendaRequest $cancelarVendaRequest) |
||
153 | |||
154 | // /** |
||
155 | // * API Provavelmente descontinuada |
||
156 | // * |
||
157 | // * @return Contracts\Venda\ConsultarVendasResponse |
||
158 | // * @throws \Exception |
||
159 | // */ |
||
160 | // public function consultarVendas(array $ids = []) |
||
161 | // { |
||
162 | // try{ |
||
163 | // $this->response = $this->_httpClient->post(__FUNCTION__, [ |
||
164 | // 'body' => json_encode(array_map(function($id){ |
||
165 | // return [ |
||
166 | // 'Id' => (string)$id |
||
167 | // ]; |
||
168 | // },$ids)) |
||
169 | // ]); |
||
170 | // |
||
171 | // return SerializerHelper::denormalize( |
||
172 | // $this->response->json(), |
||
173 | // Contracts\Venda\ConsultarVendasResponse::class |
||
174 | // ); |
||
175 | // }catch (RequestException $ex) { |
||
176 | // $this->response = $ex->getResponse(); |
||
177 | // $responseBody = $ex->getResponse()->json(); |
||
178 | // throw new \Exception($responseBody['message']); |
||
179 | // }catch (\Exception $ex){ |
||
180 | // throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
||
181 | // } |
||
182 | // } |
||
183 | |||
184 | } |
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):