| Total Complexity | 76 |
| Total Lines | 385 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ColumnSource 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 ColumnSource, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class ColumnSource |
||
| 19 | { |
||
| 20 | /** @var string|null */ |
||
| 21 | private $cacheDir; |
||
| 22 | |||
| 23 | /** @var bool */ |
||
| 24 | private $debug = false; |
||
| 25 | |||
| 26 | public function __construct($cacheDir, $debug) |
||
| 27 | { |
||
| 28 | $this->debug = $debug; |
||
| 29 | $this->cacheDir = $cacheDir; |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array|null |
||
| 34 | */ |
||
| 35 | private $cachedSort; |
||
| 36 | |||
| 37 | public function setDebug($flag) |
||
| 38 | { |
||
| 39 | $this->debug = $flag; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param string|null $cacheDir |
||
| 44 | */ |
||
| 45 | public function setCacheDir($cacheDir) |
||
| 46 | { |
||
| 47 | $this->cacheDir = $cacheDir; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata|\Doctrine\ORM\Mapping\ClassMetadata $classMetadata |
||
| 52 | * |
||
| 53 | * @return mixed|null |
||
| 54 | */ |
||
| 55 | public static function getIdColumn($classMetadata) |
||
| 56 | { |
||
| 57 | $identifier = $classMetadata->getIdentifier(); |
||
| 58 | |||
| 59 | return isset($identifier[0]) ? $identifier[0] : null; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata|\Doctrine\ORM\Mapping\ClassMetadata $classMetadata |
||
| 64 | * @param $cacheFilename |
||
| 65 | * |
||
| 66 | * @return array|null |
||
| 67 | * |
||
| 68 | * @throws \Exception |
||
| 69 | */ |
||
| 70 | private function getCachedColumnInfo($cacheFilename, $classMetadata, Reader $reader = null) |
||
| 71 | { |
||
| 72 | $params = [$classMetadata, $cacheFilename]; |
||
| 73 | if ($reader) { |
||
| 74 | $params[] = $reader; |
||
| 75 | } |
||
| 76 | if (call_user_func_array([$this, 'shouldIncludeColumnCache'], $params)) { |
||
| 77 | $columnInfo = include $cacheFilename; |
||
| 78 | if (!isset($columnInfo['columns'])) { |
||
| 79 | throw new \Exception("Bad column cache, missing columns: {$cacheFilename}"); |
||
| 80 | } |
||
| 81 | if (!isset($columnInfo['sort'])) { |
||
| 82 | throw new \Exception("Bad column cache, missing sort: {$cacheFilename}"); |
||
| 83 | } |
||
| 84 | if ($columnInfo['sort']) { |
||
| 85 | self::validateSortInfo($columnInfo['sort'], $columnInfo['columns']); |
||
| 86 | } |
||
| 87 | |||
| 88 | return $columnInfo; |
||
| 89 | } |
||
| 90 | |||
| 91 | return null; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @return ColumnSourceInfo|null |
||
| 96 | * |
||
| 97 | * @throws Exception |
||
| 98 | */ |
||
| 99 | public function getColumnSourceInfo($objectManager, $objectName, $allowReflection, Reader $reader = null) |
||
| 100 | { |
||
| 101 | $metadataFactory = $objectManager->getMetadataFactory(); |
||
| 102 | $classMetadata = $metadataFactory->getMetadataFor($objectName); |
||
| 103 | $reflectionClass = $classMetadata->getReflectionClass(); |
||
| 104 | $name = $reflectionClass->getName(); |
||
| 105 | $cacheFilename = ColumnUtil::createCacheFilename($this->cacheDir, $name); |
||
| 106 | |||
| 107 | // Try to include them from the cached file if exists. |
||
| 108 | $params = [$cacheFilename, $classMetadata]; |
||
| 109 | if ($reader) { |
||
| 110 | $params[] = $reader; |
||
| 111 | } |
||
| 112 | $columnInfo = call_user_func_array([$this, 'getCachedColumnInfo'], $params); |
||
| 113 | |||
| 114 | if (!$columnInfo && $reader) { |
||
| 115 | $columnInfo = $this->readAndCacheGridAnnotations($cacheFilename, $reader, $classMetadata, $allowReflection); |
||
| 116 | } |
||
| 117 | |||
| 118 | if (!$columnInfo && $allowReflection) { |
||
| 119 | $columns = self::getReflectionColumns($classMetadata); |
||
| 120 | $columnInfo = ['columns' => $columns, 'sort' => []]; |
||
| 121 | } |
||
| 122 | |||
| 123 | if (!$columnInfo) { |
||
| 124 | return null; |
||
| 125 | } |
||
| 126 | |||
| 127 | $columnSourceInfo = new ColumnSourceInfo(); |
||
| 128 | $columnSourceInfo->columns = $columnInfo['columns']; |
||
| 129 | $columnSourceInfo->sort = $columnInfo['sort']; |
||
| 130 | $columnSourceInfo->idColumn = self::getIdColumn($classMetadata); |
||
| 131 | |||
| 132 | return $columnSourceInfo; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Cached annotation info from the file, if the mtime of the file has not changed (or if not in debug). |
||
| 137 | * |
||
| 138 | * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata|\Doctrine\ORM\Mapping\ClassMetadata $metadata |
||
| 139 | * |
||
| 140 | * @return bool |
||
| 141 | * |
||
| 142 | * @throws Exception |
||
| 143 | */ |
||
| 144 | private function shouldIncludeColumnCache($metadata, $columnCacheFilename, Reader $reader = null) |
||
| 145 | { |
||
| 146 | // In production, or if we're sure there's no annotaitons, just include the cache. |
||
| 147 | if (!$this->debug || !isset($reader)) { |
||
| 148 | if (!is_file($columnCacheFilename) || !is_readable($columnCacheFilename)) { |
||
| 149 | return false; |
||
| 150 | } |
||
| 151 | |||
| 152 | return true; |
||
| 153 | } |
||
| 154 | |||
| 155 | return self::checkTimestamps($metadata, $columnCacheFilename); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Check timestamps of the file pointed to by the class metadata, and the columnCacheFilename and see if any |
||
| 160 | * are newer (meaning we . |
||
| 161 | * |
||
| 162 | * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata|\Doctrine\ORM\Mapping\ClassMetadata $metadata |
||
| 163 | * @param $columnCacheFilename |
||
| 164 | * |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | public static function checkTimestamps($metadata, $columnCacheFilename) |
||
| 168 | { |
||
| 169 | $reflectionClass = $metadata->getReflectionClass(); |
||
| 170 | $filename = $reflectionClass->getFileName(); |
||
| 171 | if ($filename && is_file($filename)) { |
||
| 172 | $mtime = filemtime($filename); |
||
| 173 | if (($currentfileMtime = filemtime(__FILE__)) > $mtime) { |
||
| 174 | $mtime = $currentfileMtime; |
||
| 175 | } |
||
| 176 | $mtimeAnnotation = file_exists($columnCacheFilename) ? filemtime($columnCacheFilename) : null; |
||
| 177 | if ($mtime && $mtimeAnnotation && $mtime <= $mtimeAnnotation) { |
||
| 178 | return true; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | return false; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Generates a list of property name and labels based on finding the GridColumn annotation. |
||
| 187 | * |
||
| 188 | * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata|\Doctrine\ORM\Mapping\ClassMetadata $metadata |
||
| 189 | * |
||
| 190 | * @throws \Exception |
||
| 191 | * |
||
| 192 | * @return array|null Hash of grid annotation results: ['columns' => array, 'sort' => string] |
||
| 193 | */ |
||
| 194 | private function readAndCacheGridAnnotations($cacheFilename, Reader $reader, $metadata, $allowReflection) |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @throws InvalidArgumentException |
||
| 292 | */ |
||
| 293 | private static function validateSortInfo(array $sortInfo, array $gridColumns) |
||
| 294 | { |
||
| 295 | if (isset($sortInfo['direction'])) { |
||
| 296 | switch ($sortInfo['direction']) { |
||
| 297 | case 'ASC': |
||
| 298 | case 'DESC': |
||
| 299 | break; |
||
| 300 | default: |
||
| 301 | throw new InvalidArgumentException("Grid's sort annotation direction '{$sortInfo['direction']}' is invalid"); |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | if (isset($sortInfo['column'])) { |
||
| 306 | $column = $sortInfo['column']; |
||
| 307 | |||
| 308 | if (!isset($sortInfo['direction'])) { |
||
| 309 | throw new InvalidArgumentException("Grid's sort annotation column '$column' specified but a sort direction was not"); |
||
| 310 | } |
||
| 311 | if (isset($gridColumns[$column])) { |
||
| 312 | return; |
||
| 313 | } |
||
| 314 | throw new InvalidArgumentException("Grid's sort annotation column '$column' not in list of columns (".implode(', ', array_keys($gridColumns)).')'); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param Sort|null $sortAnnotation |
||
| 320 | * |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | private static function extractSortInfo($sortAnnotation) |
||
| 324 | { |
||
| 325 | $sortInfo = ['direction' => null, 'column' => null]; |
||
| 326 | if ($sortAnnotation) { |
||
| 327 | $direction = $sortAnnotation->direction; |
||
| 328 | $sortInfo['direction'] = $direction; |
||
| 329 | $column = $sortAnnotation->column; |
||
| 330 | $sortInfo['column'] = $column; |
||
| 331 | } |
||
| 332 | |||
| 333 | return $sortInfo; |
||
| 334 | } |
||
| 335 | |||
| 336 | private function sortGridColumns(array &$columnDefs) |
||
| 337 | { |
||
| 338 | $unordered = []; |
||
| 339 | $ordered = []; |
||
| 340 | foreach ($columnDefs as $name => $columnDef) { |
||
| 341 | $columnParts = $columnDef['arguments']; |
||
| 342 | if (!isset($columnParts[5]) || null === $columnParts[5]) { |
||
| 343 | $unordered[$name] = $columnDef; |
||
| 344 | continue; |
||
| 345 | } |
||
| 346 | $ordered[$name] = $columnDef; |
||
| 347 | } |
||
| 348 | |||
| 349 | if (empty($ordered)) { |
||
| 350 | return; |
||
| 351 | } |
||
| 352 | |||
| 353 | uasort($ordered, function ($columnDef1, $columnDef2) { |
||
| 354 | $columnParts1 = $columnDef1['arguments']; |
||
| 355 | $columnParts2 = $columnDef2['arguments']; |
||
| 356 | $order1 = $columnParts1[5]; |
||
| 357 | $order2 = $columnParts2[5]; |
||
| 358 | |||
| 359 | return $order1 > $order2; |
||
| 360 | }); |
||
| 361 | |||
| 362 | if ($unordered) { |
||
| 363 | foreach ($unordered as $name => $columnDef) { |
||
| 364 | $ordered[$name] = $columnDef; |
||
| 365 | } |
||
| 366 | } |
||
| 367 | $columnDefs = $ordered; |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Generate Columns based on document's Metadata. |
||
| 372 | * |
||
| 373 | * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata|\Doctrine\ORM\Mapping\ClassMetadata $metadata |
||
| 374 | */ |
||
| 375 | private static function getReflectionColumns($metadata) |
||
| 403 | } |
||
| 404 | } |
||
| 405 |
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