| Total Complexity | 63 |
| Total Lines | 439 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Metadata 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 Metadata, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Metadata extends Component |
||
| 17 | { |
||
| 18 | // Properties |
||
| 19 | // ========================================================================= |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var array|null |
||
| 23 | */ |
||
| 24 | private $groups; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array|null |
||
| 28 | */ |
||
| 29 | private $dimensions; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array|null |
||
| 33 | */ |
||
| 34 | private $metrics; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array|null |
||
| 38 | */ |
||
| 39 | private $columns; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array|null |
||
| 43 | */ |
||
| 44 | private $selectDimensionOptions; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array|null |
||
| 48 | */ |
||
| 49 | private $selectMetricOptions; |
||
| 50 | |||
| 51 | // Public Methods |
||
| 52 | // ========================================================================= |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Checks whether the dimensions & metrics file exists |
||
| 56 | * |
||
| 57 | * @return bool |
||
| 58 | */ |
||
| 59 | public function dimmetsFileExists(): bool |
||
| 60 | { |
||
| 61 | $path = Analytics::$plugin->metadata->getDimmetsFilePath(); |
||
| 62 | |||
| 63 | if (file_exists($path)) { |
||
| 64 | return true; |
||
| 65 | } |
||
| 66 | |||
| 67 | return false; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Returns available data types for Google Analytics |
||
| 72 | * |
||
| 73 | * @param mixed |
||
| 74 | * |
||
| 75 | * @return array |
||
| 76 | */ |
||
| 77 | public function getGoogleAnalyticsDataTypes(): array |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Returns available data types |
||
| 94 | * |
||
| 95 | * @param mixed |
||
| 96 | * |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | public function getDataTypes(): array |
||
| 100 | { |
||
| 101 | return [ |
||
| 102 | 'string', |
||
| 103 | 'integer', |
||
| 104 | 'percent', |
||
| 105 | 'time', |
||
| 106 | 'currency', |
||
| 107 | 'float', |
||
| 108 | 'date' |
||
| 109 | ]; |
||
| 110 | } |
||
| 111 | |||
| 112 | public function getContinents() |
||
| 115 | } |
||
| 116 | |||
| 117 | public function getSubContinents() |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get Continent Code |
||
| 124 | * |
||
| 125 | * @param string $label |
||
| 126 | * |
||
| 127 | * @return mixed |
||
| 128 | */ |
||
| 129 | public function getContinentCode($label) |
||
| 130 | { |
||
| 131 | foreach ($this->_getData('continents') as $continent) { |
||
| 132 | if ($continent['label'] === $label) { |
||
| 133 | return $continent['code']; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | return null; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Get Sub-Continent Code |
||
| 142 | * |
||
| 143 | * @param string $label |
||
| 144 | * |
||
| 145 | * @return mixed |
||
| 146 | */ |
||
| 147 | public function getSubContinentCode($label) |
||
| 148 | { |
||
| 149 | foreach ($this->_getData('subContinents') as $subContinent) { |
||
| 150 | if ($subContinent['label'] === $label) { |
||
| 151 | return $subContinent['code']; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | return null; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get a dimension or a metric label from its id |
||
| 160 | * |
||
| 161 | * @param string $id |
||
| 162 | * |
||
| 163 | * @return mixed |
||
| 164 | */ |
||
| 165 | public function getDimMet($id) |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Returns columns based on a search string `$q` |
||
| 178 | * |
||
| 179 | * @param string $q |
||
| 180 | * |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | public function searchColumns($q): array |
||
| 184 | { |
||
| 185 | $columns = $this->getColumns(); |
||
| 186 | $results = []; |
||
| 187 | |||
| 188 | foreach ($columns as $column) { |
||
| 189 | if (stripos($column->id, $q) !== false || stripos($column->uiName, $q) !== false) { |
||
| 190 | $results[] = $column; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | return $results; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Returns columns |
||
| 199 | * |
||
| 200 | * @param null $type |
||
| 201 | * |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | public function getColumns($type = null): array |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Returns dimension columns |
||
| 227 | * |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | public function getDimensions(): array |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Returns column groups |
||
| 241 | * |
||
| 242 | * @param string|null $type |
||
| 243 | * |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | public function getColumnGroups(string $type = null): array |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Returns select dimension options |
||
| 269 | * |
||
| 270 | * @param array $filters |
||
| 271 | * |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | public function getSelectDimensionOptions(array $filters = null): array |
||
| 275 | { |
||
| 276 | if (!$this->selectDimensionOptions) { |
||
| 277 | $this->selectDimensionOptions = $this->getSelectOptions('DIMENSION'); |
||
| 278 | } |
||
| 279 | |||
| 280 | if ($filters) { |
||
| 281 | $this->selectDimensionOptions = $this->filterOptions($this->selectDimensionOptions, $filters); |
||
| 282 | } |
||
| 283 | |||
| 284 | return $this->selectDimensionOptions; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Returns select metric options |
||
| 289 | * |
||
| 290 | * @param array $filters |
||
| 291 | * |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | public function getSelectMetricOptions(array $filters = null): array |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Returns select options |
||
| 309 | * |
||
| 310 | * @param null $type |
||
| 311 | * @param array $filters |
||
| 312 | * |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | public function getSelectOptions($type = null, array $filters = null): array |
||
| 316 | { |
||
| 317 | $options = []; |
||
| 318 | |||
| 319 | foreach ($this->getColumnGroups($type) as $group) { |
||
| 320 | $options[]['optgroup'] = Craft::t('analytics', $group); |
||
| 321 | |||
| 322 | foreach ($this->getColumns($type) as $column) { |
||
| 323 | if ($column->group === $group) { |
||
| 324 | $options[$column->id] = Craft::t('analytics', $column->uiName); |
||
| 325 | } |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | if ($filters) { |
||
| 330 | $options = $this->filterOptions($options, $filters); |
||
| 331 | } |
||
| 332 | |||
| 333 | return $options; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Returns the metrics |
||
| 338 | * |
||
| 339 | * @return array |
||
| 340 | */ |
||
| 341 | public function getMetrics(): array |
||
| 342 | { |
||
| 343 | if (!$this->metrics) { |
||
| 344 | $this->metrics = $this->getColumns('METRIC'); |
||
| 345 | } |
||
| 346 | |||
| 347 | return $this->metrics; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Returns the file path of the dimensions-metrics.json file |
||
| 352 | * |
||
| 353 | * @return string|bool |
||
| 354 | */ |
||
| 355 | public function getDimmetsFilePath() |
||
| 356 | { |
||
| 357 | return Craft::getAlias('@dukt/analytics/etc/data/dimensions-metrics.json'); |
||
| 358 | } |
||
| 359 | |||
| 360 | // Private Methods |
||
| 361 | // ========================================================================= |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Loads the columns from the dimensions-metrics.json file |
||
| 365 | * |
||
| 366 | * @return array |
||
| 367 | */ |
||
| 368 | private function _loadColumns(): array |
||
| 369 | { |
||
| 370 | $cols = []; |
||
| 371 | $path = Analytics::$plugin->metadata->getDimmetsFilePath(); |
||
| 372 | $contents = file_get_contents($path); |
||
| 373 | $columnsResponse = Json::decode($contents); |
||
| 374 | |||
| 375 | if ($columnsResponse) { |
||
| 376 | foreach ($columnsResponse as $columnResponse) { |
||
| 377 | $cols[$columnResponse['id']] = new Column($columnResponse); |
||
| 378 | |||
| 379 | if ($columnResponse['id'] === 'ga:countryIsoCode') { |
||
| 380 | $cols[$columnResponse['id']]->uiName = 'Country'; |
||
| 381 | } |
||
| 382 | } |
||
| 383 | } |
||
| 384 | |||
| 385 | return $cols; |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Get Data |
||
| 390 | * |
||
| 391 | * @param $name |
||
| 392 | * |
||
| 393 | * @return array |
||
| 394 | * @internal param string $label |
||
| 395 | * |
||
| 396 | */ |
||
| 397 | private function _getData($name): array |
||
| 398 | { |
||
| 399 | $jsonData = file_get_contents(Craft::getAlias('@dukt/analytics/etc/data/'.$name.'.json')); |
||
| 400 | |||
| 401 | return json_decode($jsonData, true); |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param array $options |
||
| 406 | * @param array $filters |
||
| 407 | * |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | private function filterOptions(array $options, array $filters): array |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Get filtered options. |
||
| 421 | * |
||
| 422 | * @param array $options |
||
| 423 | * @param array $filters |
||
| 424 | * |
||
| 425 | * @return array |
||
| 426 | */ |
||
| 427 | private function getFilteredOptions(array $options, array $filters): array |
||
| 455 | } |
||
| 456 | } |
||
| 457 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: