| Total Complexity | 81 |
| Total Lines | 443 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Complex classes like ColumnExtractionTrait 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 ColumnExtractionTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | trait ColumnExtractionTrait |
||
| 21 | { |
||
| 22 | /** @var Reader|null */ |
||
| 23 | protected $reader; |
||
| 24 | |||
| 25 | /** @var string|null */ |
||
| 26 | protected $cacheDir; |
||
| 27 | |||
| 28 | /** @var bool */ |
||
| 29 | protected $debug = false; |
||
| 30 | |||
| 31 | /** @var string */ |
||
| 32 | protected $annotationCacheFilename; |
||
| 33 | |||
| 34 | /** @var array|null */ |
||
| 35 | protected $annotationColumns; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var array|null |
||
| 39 | */ |
||
| 40 | protected $annotationSort; |
||
| 41 | |||
| 42 | public function setDebug($flag) |
||
| 43 | { |
||
| 44 | $this->debug = $flag; |
||
| 45 | } |
||
| 46 | |||
| 47 | public function setAnnotationReader(Reader $reader) |
||
| 48 | { |
||
| 49 | $this->reader = $reader; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param string|null $cacheDir |
||
| 54 | */ |
||
| 55 | public function setCacheDir($cacheDir) |
||
| 56 | { |
||
| 57 | $this->cacheDir = $cacheDir; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @return ClassMetadataInfo|ClassMetadataInfo |
||
| 62 | */ |
||
| 63 | abstract public function getClassMetadata(); |
||
| 64 | |||
| 65 | public function autoDiscoverColumns() |
||
| 66 | { |
||
| 67 | $annotationColumns = $this->getAnnotationColumns(); |
||
| 68 | if ($annotationColumns) { |
||
| 69 | $this->setColumns($annotationColumns); |
||
| 70 | |||
| 71 | return; |
||
| 72 | } |
||
| 73 | |||
| 74 | $this->setColumns($this->getReflectionColumns()); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @return array|null |
||
| 79 | */ |
||
| 80 | public function getDefaultSort() |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Populates the filename for the annotationCache. |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | * |
||
| 94 | * @throws \Exception |
||
| 95 | */ |
||
| 96 | protected function populateAnnotationCacheFilename() |
||
| 97 | { |
||
| 98 | if (isset($this->annotationCacheFilename)) { |
||
| 99 | return $this->annotationCacheFilename; |
||
| 100 | } |
||
| 101 | $directory = $this->cacheDir.'/DtcGridBundle'; |
||
| 102 | $metadata = $this->getClassMetadata(); |
||
| 103 | $reflectionClass = $metadata->getReflectionClass(); |
||
| 104 | $name = $reflectionClass->getName(); |
||
| 105 | $namespace = $reflectionClass->getNamespaceName(); |
||
| 106 | $namespace = str_replace('\\', DIRECTORY_SEPARATOR, $namespace); |
||
| 107 | $namespaceDir = $directory.DIRECTORY_SEPARATOR.$namespace; |
||
| 108 | |||
| 109 | $umask = decoct(umask()); |
||
| 110 | $umask = str_pad($umask, 4, '0', STR_PAD_LEFT); |
||
| 111 | |||
| 112 | // Is there a better way to do this? |
||
| 113 | $permissions = '0777'; |
||
| 114 | $permissions[1] = intval($permissions[1]) - intval($umask[1]); |
||
| 115 | $permissions[2] = intval($permissions[2]) - intval($umask[2]); |
||
| 116 | $permissions[3] = intval($permissions[3]) - intval($umask[3]); |
||
| 117 | |||
| 118 | if (!is_dir($namespaceDir) && !mkdir($namespaceDir, octdec($permissions), true)) { |
||
| 119 | throw new \Exception("Can't create: ".$namespaceDir); |
||
| 120 | } |
||
| 121 | |||
| 122 | $name = str_replace('\\', DIRECTORY_SEPARATOR, $name); |
||
| 123 | $this->annotationCacheFilename = $directory.DIRECTORY_SEPARATOR.$name.'.php'; |
||
| 124 | |||
| 125 | return $this->annotationCacheFilename; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Attempt to discover columns using the GridColumn annotation. |
||
| 130 | * |
||
| 131 | * @throws \Exception |
||
| 132 | */ |
||
| 133 | protected function getAnnotationColumns() |
||
| 134 | { |
||
| 135 | if (!isset($this->reader)) { |
||
| 136 | return null; |
||
| 137 | } |
||
| 138 | |||
| 139 | if (!isset($this->cacheDir)) { |
||
| 140 | return null; |
||
| 141 | } |
||
| 142 | |||
| 143 | if (!isset($this->annotationCacheFilename)) { |
||
| 144 | $this->populateAnnotationCacheFilename(); |
||
| 145 | } |
||
| 146 | |||
| 147 | if (!$this->debug && null !== $this->annotationColumns) { |
||
| 148 | return $this->annotationColumns ?: null; |
||
| 149 | } |
||
| 150 | |||
| 151 | // Check mtime of class |
||
| 152 | if (is_file($this->annotationCacheFilename)) { |
||
| 153 | $result = $this->tryIncludeAnnotationCache(); |
||
| 154 | if ($result) { |
||
| 155 | return $this->annotationColumns; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | // cache annotation |
||
| 160 | $this->populateAndCacheAnnotationColumns(); |
||
| 161 | |||
| 162 | return $this->annotationColumns ?: null; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Cached annotation info from the file, if the mtime of the file has not changed (or if not in debug). |
||
| 167 | * |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | protected function tryIncludeAnnotationCache() |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Retrieves the cached annotations from the cache file. |
||
| 199 | */ |
||
| 200 | protected function includeAnnotationCache() |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Caches the annotation columns result into a file. |
||
| 212 | */ |
||
| 213 | protected function populateAndCacheAnnotationColumns() |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Generates a list of property name and labels based on finding the GridColumn annotation. |
||
| 254 | * |
||
| 255 | * @return array Hash of grid annotation results: ['columns' => array, 'sort' => string] |
||
| 256 | */ |
||
| 257 | protected function readGridAnnotations() |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @throws \InvalidArgumentException |
||
| 333 | */ |
||
| 334 | protected function validateSortInfo(array $sortInfo, array $gridColumns) |
||
| 335 | { |
||
| 336 | if ($sortInfo['direction']) { |
||
| 337 | switch ($sortInfo['direction']) { |
||
| 338 | case 'ASC': |
||
| 339 | case 'DESC': |
||
| 340 | break; |
||
| 341 | default: |
||
| 342 | throw new \InvalidArgumentException("Grid's sort annotation direction '{$sortInfo['direction']}' is invalid"); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | if (isset($sortInfo['column'])) { |
||
| 347 | $column = $sortInfo['column']; |
||
| 348 | |||
| 349 | if (!isset($sortInfo['direction'])) { |
||
| 350 | throw new \InvalidArgumentException("Grid's sort annotation column '$column' specified but a sort direction was not"); |
||
| 351 | } |
||
| 352 | foreach (array_keys($gridColumns) as $name) { |
||
| 353 | if ($name === $column) { |
||
| 354 | return; |
||
| 355 | } |
||
| 356 | } |
||
| 357 | throw new \InvalidArgumentException("Grid's sort annotation column '$column' not in list of columns (".implode(', ', array_keys($gridColumns)).')'); |
||
| 358 | } |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param Sort|null $sortAnnotation |
||
| 363 | * |
||
| 364 | * @return array |
||
| 365 | */ |
||
| 366 | protected function extractSortInfo($sortAnnotation) |
||
| 377 | } |
||
| 378 | |||
| 379 | protected function sortGridColumns(array &$columnDefs) |
||
| 380 | { |
||
| 381 | $unordered = []; |
||
| 382 | $ordered = []; |
||
| 383 | foreach ($columnDefs as $name => $columnDef) { |
||
| 384 | $columnParts = $columnDef['arguments']; |
||
| 385 | if (!isset($columnParts[5]) || null === $columnParts[5]) { |
||
| 386 | $unordered[$name] = $columnDef; |
||
| 387 | continue; |
||
| 388 | } |
||
| 389 | $ordered[$name] = $columnDef; |
||
| 390 | } |
||
| 391 | |||
| 392 | if (empty($ordered)) { |
||
| 393 | return; |
||
| 394 | } |
||
| 395 | |||
| 396 | uasort($ordered, function ($columnDef1, $columnDef2) { |
||
| 397 | $columnParts1 = $columnDef1['arguments']; |
||
| 398 | $columnParts2 = $columnDef2['arguments']; |
||
| 399 | $order1 = $columnParts1[5]; |
||
| 400 | $order2 = $columnParts2[5]; |
||
| 401 | |||
| 402 | return $order1 > $order2; |
||
| 403 | }); |
||
| 404 | |||
| 405 | if ($unordered) { |
||
| 406 | foreach ($unordered as $name => $columnDef) { |
||
| 407 | $ordered[$name] = $columnDef; |
||
| 408 | } |
||
| 409 | } |
||
| 410 | $columnDefs = $ordered; |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Generate Columns based on document's Metadata. |
||
| 415 | */ |
||
| 416 | protected function getReflectionColumns() |
||
| 417 | { |
||
| 418 | $metadata = $this->getClassMetadata(); |
||
| 419 | $fields = $metadata->getFieldNames(); |
||
| 420 | $identifier = $metadata->getIdentifier(); |
||
| 421 | $identifier = isset($identifier[0]) ? $identifier[0] : null; |
||
| 422 | |||
| 423 | $columns = []; |
||
| 424 | foreach ($fields as $field) { |
||
| 425 | $mapping = $metadata->getFieldMapping($field); |
||
| 426 | if (isset($mapping['options']) && isset($mapping['options']['label'])) { |
||
| 427 | $label = $mapping['options']['label']; |
||
| 428 | } else { |
||
| 429 | $label = CamelCase::fromCamelCase($field); |
||
| 430 | } |
||
| 431 | |||
| 432 | if ($identifier === $field) { |
||
| 433 | if (isset($mapping['strategy']) && 'auto' == $mapping['strategy']) { |
||
| 434 | continue; |
||
| 435 | } |
||
| 436 | } |
||
| 437 | $columns[$field] = new GridColumn($field, $label); |
||
| 438 | } |
||
| 439 | |||
| 440 | return $columns; |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @return string|null |
||
| 445 | */ |
||
| 446 | protected function getIdColumn() |
||
| 447 | { |
||
| 448 | static $identifier = false; |
||
| 449 | if (false !== $identifier) { |
||
| 450 | return $identifier; |
||
| 451 | } |
||
| 452 | |||
| 453 | $metadata = $this->getClassMetadata(); |
||
| 454 | $identifier = $metadata->getIdentifier(); |
||
| 455 | $identifier = isset($identifier[0]) ? $identifier[0] : null; |
||
| 456 | |||
| 457 | return $identifier; |
||
| 458 | } |
||
| 459 | |||
| 460 | public function hasIdColumn() |
||
| 463 | } |
||
| 464 | } |
||
| 465 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths