Conditions | 14 |
Paths | 440 |
Total Lines | 68 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
32 | public function fetch(array $configuration): Data |
||
33 | { |
||
34 | $data = new Data(); |
||
35 | |||
36 | /** @var \DateTime $endDate */ |
||
37 | $endDate = $configuration['timePeriod']['end']; |
||
38 | |||
39 | //There is added 23 hours 59 minutes 59 seconds to the end date to provide records for whole end date |
||
40 | $configuration['timePeriod']['end'] = $endDate?$endDate->add(new \DateInterval('PT23H59M59S')):null; |
||
|
|||
41 | |||
42 | switch ($configuration['timePeriod']['period']) { |
||
43 | case self::PERIOD_DAY: |
||
44 | $this->setExtraConfiguration($configuration, 'P1D', '%a', 'Y-m-d', ['date']); |
||
45 | break; |
||
46 | case self::PERIOD_MONTH: |
||
47 | $this->setExtraConfiguration($configuration, 'P1M', '%m', 'F Y', ['month', 'year']); |
||
48 | break; |
||
49 | case self::PERIOD_YEAR: |
||
50 | $this->setExtraConfiguration($configuration, 'P1Y', '%y', 'Y', ['year']); |
||
51 | break; |
||
52 | default: |
||
53 | throw new \InvalidArgumentException('Wrong data fetcher period'); |
||
54 | } |
||
55 | |||
56 | $rawData = $this->getData($configuration); |
||
57 | |||
58 | if (empty($rawData)) { |
||
59 | return $data; |
||
60 | } |
||
61 | |||
62 | $labelsAux = array_keys($rawData[0]); |
||
63 | $labels = []; |
||
64 | foreach ($labelsAux as $label) { |
||
65 | if (!in_array($label, ['MonthDate', 'YearDate', 'DateDate'])) { |
||
66 | $labels[] = $label; |
||
67 | } |
||
68 | } |
||
69 | $data->setLabels($labels); |
||
70 | |||
71 | $fetched = []; |
||
72 | |||
73 | if ($configuration['empty_records']) { |
||
74 | $fetched = $this->fillEmptyRecords($fetched, $configuration); |
||
75 | } |
||
76 | foreach ($rawData as $row) { |
||
77 | $rowFetched = []; |
||
78 | foreach ($labels as $i => $label) { |
||
79 | if ($i === 0) { |
||
80 | $date = new \DateTime($row[$labels[0]]); |
||
81 | $rowFetched[] = $date->format($configuration['timePeriod']['presentationFormat']); |
||
82 | } else { |
||
83 | $rowFetched[] = $row[$labels[$i]]; |
||
84 | } |
||
85 | } |
||
86 | $fetched[] = $rowFetched; |
||
87 | } |
||
88 | |||
89 | $data->setData($fetched); |
||
90 | |||
91 | $labels = []; |
||
92 | foreach ($labelsAux as $label) { |
||
93 | if (!in_array($label, ['MonthDate', 'YearDate', 'DateDate'])) { |
||
94 | $labels[] = preg_replace('/(?!^)[A-Z]{2,}(?=[A-Z][a-z])|[A-Z][a-z]/', ' $0', $label); |
||
95 | } |
||
96 | } |
||
97 | $data->setLabels($labels); |
||
98 | |||
99 | return $data; |
||
100 | } |
||
183 |