Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DoctrineDataCollector 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 DoctrineDataCollector, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class DoctrineDataCollector extends BaseCollector |
||
| 31 | { |
||
| 32 | private $registry; |
||
|
|
|||
| 33 | private $invalidEntityCount; |
||
| 34 | |||
| 35 | public function __construct(ManagerRegistry $registry) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * {@inheritdoc} |
||
| 44 | */ |
||
| 45 | public function collect(Request $request, Response $response, \Exception $exception = null) |
||
| 46 | { |
||
| 47 | parent::collect($request, $response, $exception); |
||
| 48 | |||
| 49 | $errors = array(); |
||
| 50 | $entities = array(); |
||
| 51 | $caches = array( |
||
| 52 | 'enabled' => false, |
||
| 53 | 'log_enabled' => false, |
||
| 54 | 'counts' => array( |
||
| 55 | 'puts' => 0, |
||
| 56 | 'hits' => 0, |
||
| 57 | 'misses' => 0, |
||
| 58 | ), |
||
| 59 | 'regions' => array( |
||
| 60 | 'puts' => array(), |
||
| 61 | 'hits' => array(), |
||
| 62 | 'misses' => array(), |
||
| 63 | ), |
||
| 64 | ); |
||
| 65 | |||
| 66 | foreach ($this->registry->getManagers() as $name => $em) { |
||
| 67 | $entities[$name] = array(); |
||
| 68 | /** @var $factory \Doctrine\ORM\Mapping\ClassMetadataFactory */ |
||
| 69 | $factory = $em->getMetadataFactory(); |
||
| 70 | $validator = new SchemaValidator($em); |
||
| 71 | |||
| 72 | /** @var $class \Doctrine\ORM\Mapping\ClassMetadataInfo */ |
||
| 73 | foreach ($factory->getLoadedMetadata() as $class) { |
||
| 74 | if (!isset($entities[$name][$class->getName()])) { |
||
| 75 | $classErrors = $validator->validateClass($class); |
||
| 76 | $entities[$name][$class->getName()] = $class->getName(); |
||
| 77 | |||
| 78 | if (!empty($classErrors)) { |
||
| 79 | $errors[$name][$class->getName()] = $classErrors; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | if (version_compare(Version::VERSION, '2.5.0-DEV') < 0) { |
||
| 85 | continue; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** @var $emConfig \Doctrine\ORM\Configuration */ |
||
| 89 | $emConfig = $em->getConfiguration(); |
||
| 90 | $slcEnabled = $emConfig->isSecondLevelCacheEnabled(); |
||
| 91 | |||
| 92 | if (!$slcEnabled) { |
||
| 93 | continue; |
||
| 94 | } |
||
| 95 | |||
| 96 | $caches['enabled'] = true; |
||
| 97 | |||
| 98 | /** @var $cacheConfiguration \Doctrine\ORM\Cache\CacheConfiguration */ |
||
| 99 | /** @var $cacheLoggerChain \Doctrine\ORM\Cache\Logging\CacheLoggerChain */ |
||
| 100 | $cacheConfiguration = $emConfig->getSecondLevelCacheConfiguration(); |
||
| 101 | $cacheLoggerChain = $cacheConfiguration->getCacheLogger(); |
||
| 102 | |||
| 103 | if (!$cacheLoggerChain || !$cacheLoggerChain->getLogger('statistics')) { |
||
| 104 | continue; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** @var $cacheLoggerStats \Doctrine\ORM\Cache\Logging\StatisticsCacheLogger */ |
||
| 108 | $cacheLoggerStats = $cacheLoggerChain->getLogger('statistics'); |
||
| 109 | $caches['log_enabled'] = true; |
||
| 110 | |||
| 111 | $caches['counts']['puts'] += $cacheLoggerStats->getPutCount(); |
||
| 112 | $caches['counts']['hits'] += $cacheLoggerStats->getHitCount(); |
||
| 113 | $caches['counts']['misses'] += $cacheLoggerStats->getMissCount(); |
||
| 114 | |||
| 115 | View Code Duplication | foreach ($cacheLoggerStats->getRegionsPut() as $key => $value) { |
|
| 116 | if (!isset($caches['regions']['puts'][$key])) { |
||
| 117 | $caches['regions']['puts'][$key] = 0; |
||
| 118 | } |
||
| 119 | |||
| 120 | $caches['regions']['puts'][$key] += $value; |
||
| 121 | } |
||
| 122 | |||
| 123 | View Code Duplication | foreach ($cacheLoggerStats->getRegionsHit() as $key => $value) { |
|
| 124 | if (!isset($caches['regions']['hits'][$key])) { |
||
| 125 | $caches['regions']['hits'][$key] = 0; |
||
| 126 | } |
||
| 127 | |||
| 128 | $caches['regions']['hits'][$key] += $value; |
||
| 129 | } |
||
| 130 | |||
| 131 | View Code Duplication | foreach ($cacheLoggerStats->getRegionsMiss() as $key => $value) { |
|
| 132 | if (!isset($caches['regions']['misses'][$key])) { |
||
| 133 | $caches['regions']['misses'][$key] = 0; |
||
| 134 | } |
||
| 135 | |||
| 136 | $caches['regions']['misses'][$key] += $value; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | // HttpKernel < 3.2 compatibility layer |
||
| 141 | if (method_exists($this, 'cloneVar')) { |
||
| 142 | // Might be good idea to replicate this block in doctrine bridge so we can drop this from here after some time. |
||
| 143 | // This code is compatible with such change, because cloneVar is supposed to check if input is already cloned. |
||
| 144 | foreach ($this->data['queries'] as &$queries) { |
||
| 145 | foreach ($queries as &$query) { |
||
| 146 | $query['params'] = $this->cloneVar($query['params']); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | $this->data['entities'] = $entities; |
||
| 152 | $this->data['errors'] = $errors; |
||
| 153 | $this->data['caches'] = $caches; |
||
| 154 | } |
||
| 155 | |||
| 156 | public function getEntities() |
||
| 160 | |||
| 161 | public function getMappingErrors() |
||
| 165 | |||
| 166 | public function getCacheHitsCount() |
||
| 170 | |||
| 171 | public function getCachePutsCount() |
||
| 175 | |||
| 176 | public function getCacheMissesCount() |
||
| 180 | |||
| 181 | public function getCacheEnabled() |
||
| 185 | |||
| 186 | public function getCacheRegions() |
||
| 190 | |||
| 191 | public function getCacheCounts() |
||
| 195 | |||
| 196 | public function getInvalidEntityCount() |
||
| 204 | |||
| 205 | public function getGroupedQueries() |
||
| 247 | |||
| 248 | private function executionTimePercentage($executionTimeMS, $totalExecutionTimeMS) |
||
| 256 | |||
| 257 | public function getGroupedQueryCount() |
||
| 266 | } |
||
| 267 |