| Total Complexity | 40 | 
| Total Lines | 209 | 
| Duplicated Lines | 0 % | 
| Changes | 5 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like VariableService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use VariableService, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 18 | class VariableService | ||
| 19 | { | ||
| 20 | private $logger; | ||
| 21 | private $DatasetMapper; | ||
| 22 | private $IDateTimeFormatter; | ||
| 23 | |||
| 24 | public function __construct( | ||
| 33 | } | ||
| 34 | |||
| 35 | /** | ||
| 36 | * replace %*% text variables in thresholds | ||
| 37 | * | ||
| 38 | * @param array $thresholds | ||
| 39 | * @return array | ||
| 40 | */ | ||
| 41 | public function replaceThresholdsVariables($thresholds) | ||
| 53 | } | ||
| 54 | |||
| 55 | /** | ||
| 56 | * replace %*% text variables in name and subheader | ||
| 57 | * | ||
| 58 | * @param array $datasetMetadata | ||
| 59 | * @return array | ||
| 60 | */ | ||
| 61 | public function replaceTextVariables($datasetMetadata) | ||
| 62 |     { | ||
| 63 | $fields = ['name', 'subheader']; | ||
| 64 |         foreach ($fields as $field) { | ||
| 65 | isset($datasetMetadata[$field]) ? $name = $datasetMetadata[$field] : $name = ''; | ||
| 66 | |||
| 67 |             preg_match_all("/%.*?%/", $name, $matches); | ||
| 68 |             if (count($matches[0]) > 0) { | ||
| 69 |                 foreach ($matches[0] as $match) { | ||
| 70 | $replace = null; | ||
| 71 |                     if ($match === '%currentDate%') { | ||
| 72 | $replace = $this->IDateTimeFormatter->formatDate(time(), 'short'); | ||
| 73 |                     } elseif ($match === '%currentTime%') { | ||
| 74 | $replace = $this->IDateTimeFormatter->formatTime(time(), 'short'); | ||
| 75 |                     } elseif ($match === '%now%') { | ||
| 76 | $replace = time(); | ||
| 77 |                     } elseif ($match === '%lastUpdateDate%') { | ||
| 78 | $timestamp = $this->DatasetMapper->getLastUpdate($datasetMetadata['dataset']); | ||
| 79 | $replace = $this->IDateTimeFormatter->formatDate($timestamp, 'short'); | ||
| 80 |                     } elseif ($match === '%lastUpdateTime%') { | ||
| 81 | $timestamp = $this->DatasetMapper->getLastUpdate($datasetMetadata['dataset']); | ||
| 82 | $replace = $this->IDateTimeFormatter->formatTime($timestamp, 'short'); | ||
| 83 |                     } elseif ($match === '%owner%') { | ||
| 84 | $owner = $this->DatasetMapper->getOwner($datasetMetadata['dataset']); | ||
| 85 | $replace = $owner; | ||
| 86 | } | ||
| 87 |                     if ($replace !== null) { | ||
| 88 |                         $datasetMetadata[$field] = preg_replace('/' . $match . '/', $replace, $datasetMetadata[$field]); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | return $datasetMetadata; | ||
| 94 | } | ||
| 95 | |||
| 96 | /** | ||
| 97 | * replace variables in single field | ||
| 98 | * used in: API | ||
| 99 | * | ||
| 100 | * @param $field | ||
| 101 | * @return array | ||
| 102 | */ | ||
| 103 | public function replaceTextVariablesSingle($field) | ||
| 104 |     { | ||
| 105 |         preg_match_all("/%.*?%/", $field, $matches); | ||
| 106 |         if (count($matches[0]) > 0) { | ||
| 107 |             foreach ($matches[0] as $match) { | ||
| 108 | $replace = null; | ||
| 109 |                 if ($match === '%currentDate%') { | ||
| 110 | $replace = $this->IDateTimeFormatter->formatDate(time(), 'short'); | ||
| 111 |                 } elseif ($match === '%currentTime%') { | ||
| 112 | $replace = $this->IDateTimeFormatter->formatTime(time(), 'short'); | ||
| 113 |                 } elseif ($match === '%now%') { | ||
| 114 | $replace = time(); | ||
| 115 | } | ||
| 116 |                 if ($replace !== null) { | ||
| 117 |                     $field = preg_replace('/' . $match . '/', $replace, $field); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | return $field; | ||
| 122 | } | ||
| 123 | |||
| 124 | /** | ||
| 125 | * replace variables in filters and apply format | ||
| 126 | * | ||
| 127 | * @param $reportMetadata | ||
| 128 | * @return array | ||
| 129 | */ | ||
| 130 | public function replaceFilterVariables($reportMetadata) | ||
| 131 |     { | ||
| 132 | $filteroptions = json_decode($reportMetadata['filteroptions'], true); | ||
| 133 |         if (isset($filteroptions['filter'])) { | ||
| 134 |             foreach ($filteroptions['filter'] as $key => $value) { | ||
| 135 | $parsed = $this->parseFilter($value['value']); | ||
| 136 | $format = $this->parseFormat($value['value']); | ||
| 137 | |||
| 138 | if (!$parsed) break; | ||
| 139 | $filteroptions['filter'][$key]['option'] = $parsed['option']; | ||
| 140 | $filteroptions['filter'][$key]['value'] = date($format, $parsed['value']); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | $reportMetadata['filteroptions'] = json_encode($filteroptions); | ||
| 144 | return $reportMetadata; | ||
| 145 | } | ||
| 146 | |||
| 147 | /** | ||
| 148 | * parsing of %*% variables | ||
| 149 | * | ||
| 150 | * @param $filter | ||
| 151 | * @return array|bool | ||
| 152 | */ | ||
| 153 |     private function parseFilter($filter) { | ||
| 213 | } | ||
| 214 | |||
| 215 | /** | ||
| 216 | * parsing of ( ) format instructions | ||
| 217 | * | ||
| 218 | * @param $filter | ||
| 219 | * @return string | ||
| 220 | */ | ||
| 221 |     private function parseFormat($filter) { | ||
| 227 | } | ||
| 228 | } | ||
| 229 | } | 
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.