Conditions | 8 |
Paths | 61 |
Total Lines | 60 |
Code Lines | 40 |
Lines | 7 |
Ratio | 11.67 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
15 | public function getTransactionList($merchantList = null, \DateTime $dStartDate = null, \DateTime $dEndDate = null) |
||
16 | { |
||
17 | try { |
||
18 | |||
19 | $totalTransactions = array(); |
||
20 | |||
21 | $merchantIdList = \Oara\Utilities::getMerchantIdMapFromMerchantList($merchantList); |
||
22 | |||
23 | $url = 'http://api.effiliation.com/apiv2/transaction.csv?key=' . $this->_credentials["apiPassword"] . '&start=' . $dStartDate->format("d/m/Y") . '&end=' . $dEndDate->format("d/m/Y") . '&type=date&all=yes'; |
||
24 | // Set timeout to 300 secs. due to api delays - <PN> 2017-06-20 |
||
25 | $ctx = stream_context_create(array( |
||
26 | 'http' => array( |
||
27 | 'timeout' => 300 |
||
28 | ) |
||
29 | ) |
||
30 | ); |
||
31 | // Log Debug info |
||
32 | echo (New \DateTime())->format("d/m/Y H:i:s") . " - EffiliationEx getTransactionList from " . $dStartDate->format("d/m/Y") . " to " . $dEndDate->format("d/m/Y"),PHP_EOL; |
||
33 | |||
34 | $content = \utf8_encode(\file_get_contents($url, 0, $ctx)); |
||
35 | $exportData = \str_getcsv($content, "\n"); |
||
36 | $num = \count($exportData); |
||
37 | |||
38 | for ($i = 1; $i < $num; $i++) { |
||
39 | $transactionExportArray = \str_getcsv($exportData[$i], "|"); |
||
40 | if (isset($merchantIdList[(int)$transactionExportArray[2]])) { |
||
41 | |||
42 | $transaction = Array(); |
||
43 | $merchantId = (int)$transactionExportArray[2]; |
||
44 | $transaction['merchantId'] = $merchantId; |
||
45 | $transaction['date'] = $transactionExportArray[10]; |
||
46 | $transaction['unique_id'] = $transactionExportArray[0]; |
||
47 | $transaction['custom_id'] = ''; |
||
48 | $transaction['status'] = \Oara\Utilities::STATUS_PENDING; |
||
49 | if ($transactionExportArray[4] != null) { |
||
50 | $transaction['custom_id'] = $transactionExportArray[4]; |
||
51 | } |
||
52 | |||
53 | if ($transactionExportArray[9] == 'Valide') { |
||
54 | $transaction['status'] = \Oara\Utilities::STATUS_CONFIRMED; |
||
55 | View Code Duplication | } else |
|
56 | if ($transactionExportArray[9] == 'Attente') { |
||
57 | $transaction['status'] = \Oara\Utilities::STATUS_PENDING; |
||
58 | } else |
||
59 | if ($transactionExportArray[9] == 'Refusé') { |
||
60 | $transaction['status'] = \Oara\Utilities::STATUS_DECLINED; |
||
61 | } |
||
62 | $transaction['amount'] = \Oara\Utilities::parseDouble($transactionExportArray[7]); |
||
63 | $transaction['commission'] = \Oara\Utilities::parseDouble($transactionExportArray[8]); |
||
64 | $totalTransactions[] = $transaction; |
||
65 | } |
||
66 | } |
||
67 | } catch (\Exception $e) { |
||
68 | // Avoid lost of transactions if one date failed - <PN> - 2017-06-20 |
||
69 | echo PHP_EOL."EffiliationEx - getTransactionList err: ".$e->getMessage().PHP_EOL; |
||
70 | throw new \Exception($e); |
||
71 | } |
||
72 | echo (New \DateTime())->format("d/m/Y H:i:s") . " - EffiliationEx getTransactionList - return " . count($totalTransactions) . " transactions",PHP_EOL; |
||
73 | return $totalTransactions; |
||
74 | } |
||
75 | } |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: