| Conditions | 11 |
| Paths | 112 |
| Total Lines | 64 |
| Code Lines | 37 |
| 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->add(new \DateInterval('PT23H59M59S')); |
||
| 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 | { |
||
| 66 | if(!in_array($label, ['MonthDate', 'YearDate', 'DateDate'])) |
||
| 67 | { |
||
| 68 | $labels[] = $label; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | $data->setLabels($labels); |
||
| 72 | |||
| 73 | $fetched = []; |
||
| 74 | |||
| 75 | if ($configuration['empty_records']) { |
||
| 76 | $fetched = $this->fillEmptyRecords($fetched, $configuration); |
||
| 77 | } |
||
| 78 | foreach ($rawData as $row) { |
||
| 79 | $date = new \DateTime($row[$labels[0]]); |
||
| 80 | $fetched[$date->format($configuration['timePeriod']['presentationFormat'])] = $row[$labels[1]]; |
||
| 81 | } |
||
| 82 | |||
| 83 | $data->setData($fetched); |
||
| 84 | |||
| 85 | $labels = []; |
||
| 86 | foreach ($labelsAux as $label) |
||
| 87 | { |
||
| 88 | if(!in_array($label, ['MonthDate', 'YearDate', 'DateDate'])) |
||
| 89 | { |
||
| 90 | $labels[] = preg_replace('/(?!^)[A-Z]{2,}(?=[A-Z][a-z])|[A-Z][a-z]/', ' $0', $label); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | $data->setLabels($labels); |
||
| 94 | |||
| 95 | return $data; |
||
| 96 | } |
||
| 181 |