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