| Total Complexity | 43 |
| Total Lines | 221 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| 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) |
||
| 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) |
||
| 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 | if ($reportMetadata['filteroptions'] !== null) { |
||
| 133 | $filteroptions = json_decode($reportMetadata['filteroptions'], true); |
||
| 134 | if (isset($filteroptions['filter'])) { |
||
| 135 | foreach ($filteroptions['filter'] as $key => $value) { |
||
| 136 | $parsed = $this->parseFilter($value['value']); |
||
| 137 | $format = $this->parseFormat($value['value']); |
||
| 138 | |||
| 139 | if (!$parsed) break; |
||
| 140 | |||
| 141 | // if a parser is selected in the chart options, it should also be valid here automatically |
||
| 142 | $chartOptions = json_decode($reportMetadata['chartoptions'], true); |
||
| 143 | if(isset($chartOptions['scales']['xAxes']['time']['parser'])) { |
||
| 144 | $format = $chartOptions['scales']['xAxes']['time']['parser']; |
||
| 145 | } |
||
| 146 | |||
| 147 | // translate commonly known X timestamp format to U for php |
||
| 148 | if ($format === 'X') $format = 'U'; |
||
| 149 | |||
| 150 | $filteroptions['filter'][$key]['value'] = date($format, $parsed['value']); |
||
| 151 | $filteroptions['filter'][$key]['option'] = $parsed['option']; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | $reportMetadata['filteroptions'] = json_encode($filteroptions); |
||
| 155 | } |
||
| 156 | return $reportMetadata; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * parsing of %*% variables |
||
| 161 | * |
||
| 162 | * @param $filter |
||
| 163 | * @return array|bool |
||
| 164 | */ |
||
| 165 | private function parseFilter($filter) { |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * parsing of ( ) format instructions |
||
| 229 | * |
||
| 230 | * @param $filter |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | private function parseFormat($filter) { |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
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.