Total Complexity | 47 |
Total Lines | 241 |
Duplicated Lines | 0 % |
Changes | 6 | ||
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( |
||
25 | LoggerInterface $logger, |
||
26 | DatasetMapper $DatasetMapper, |
||
27 | IDateTimeFormatter $IDateTimeFormatter |
||
28 | ) |
||
29 | { |
||
30 | $this->logger = $logger; |
||
31 | $this->DatasetMapper = $DatasetMapper; |
||
32 | $this->IDateTimeFormatter = $IDateTimeFormatter; |
||
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) |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * replace variables in single field |
||
128 | * used in: API |
||
129 | * |
||
130 | * @param $columns |
||
131 | * @return string |
||
132 | */ |
||
133 | public function replaceDatasourceColumns($columns) |
||
134 | { |
||
135 | $parsed = $this->parseFilter($columns); |
||
136 | $format = $this->parseFormat($columns); |
||
137 | if (!$parsed) return $columns; |
||
138 | return date($format, $parsed['value']); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * replace variables in filters and apply format |
||
143 | * |
||
144 | * @param $reportMetadata |
||
145 | * @return array |
||
146 | */ |
||
147 | public function replaceFilterVariables($reportMetadata) |
||
148 | { |
||
149 | if ($reportMetadata['filteroptions'] !== null) { |
||
150 | $filteroptions = json_decode($reportMetadata['filteroptions'], true); |
||
151 | if (isset($filteroptions['filter'])) { |
||
152 | foreach ($filteroptions['filter'] as $key => $value) { |
||
153 | $parsed = $this->parseFilter($value['value']); |
||
154 | $format = $this->parseFormat($value['value']); |
||
155 | |||
156 | if (!$parsed) break; |
||
157 | |||
158 | // if a parser is selected in the chart options, it should also be valid here automatically |
||
159 | if (isset($reportMetadata['chartoptions'])) { |
||
160 | $chartOptions = json_decode($reportMetadata['chartoptions'], true); |
||
161 | if(isset($chartOptions['scales']['xAxes']['time']['parser'])) { |
||
162 | $format = $chartOptions['scales']['xAxes']['time']['parser']; |
||
163 | } |
||
164 | } |
||
165 | |||
166 | // translate commonly known X timestamp format to U for php |
||
167 | if ($format === 'X') $format = 'U'; |
||
168 | |||
169 | $filteroptions['filter'][$key]['value'] = date($format, $parsed['value']); |
||
170 | //$filteroptions['filter'][$key]['option'] = $parsed['option']; |
||
171 | } |
||
172 | } |
||
173 | $reportMetadata['filteroptions'] = json_encode($filteroptions); |
||
174 | } |
||
175 | return $reportMetadata; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * parsing of %*% variables |
||
180 | * |
||
181 | * @param $filter |
||
182 | * @return array|bool |
||
183 | */ |
||
184 | private function parseFilter($filter) { |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * parsing of ( ) format instructions |
||
249 | * |
||
250 | * @param $filter |
||
251 | * @return string |
||
252 | */ |
||
253 | private function parseFormat($filter) { |
||
259 | } |
||
260 | } |
||
261 | } |
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.