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 |
||
| 18 | class DoctrineDataCollector extends BaseCollector |
||
| 19 | { |
||
| 20 | /** @var ManagerRegistry */ |
||
| 21 | private $registry; |
||
|
|
|||
| 22 | |||
| 23 | /** @var int|null */ |
||
| 24 | private $invalidEntityCount; |
||
| 25 | |||
| 26 | /** @var string[] */ |
||
| 27 | private $groupedQueries; |
||
| 28 | |||
| 29 | /** @var bool */ |
||
| 30 | private $shouldValidateSchema; |
||
| 31 | |||
| 32 | public function __construct(ManagerRegistry $registry, bool $shouldValidateSchema = true) |
||
| 33 | { |
||
| 34 | $this->registry = $registry; |
||
| 35 | $this->shouldValidateSchema = $shouldValidateSchema; |
||
| 36 | |||
| 37 | parent::__construct($registry); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritdoc} |
||
| 42 | */ |
||
| 43 | public function collect(Request $request, Response $response, Throwable $exception = null) |
||
| 44 | { |
||
| 45 | parent::collect($request, $response, $exception); |
||
| 46 | |||
| 47 | $errors = []; |
||
| 48 | $entities = []; |
||
| 49 | $caches = [ |
||
| 50 | 'enabled' => false, |
||
| 51 | 'log_enabled' => false, |
||
| 52 | 'counts' => [ |
||
| 53 | 'puts' => 0, |
||
| 54 | 'hits' => 0, |
||
| 55 | 'misses' => 0, |
||
| 56 | ], |
||
| 57 | 'regions' => [ |
||
| 58 | 'puts' => [], |
||
| 59 | 'hits' => [], |
||
| 60 | 'misses' => [], |
||
| 61 | ], |
||
| 62 | ]; |
||
| 63 | |||
| 64 | /** @var EntityManager $em */ |
||
| 65 | foreach ($this->registry->getManagers() as $name => $em) { |
||
| 66 | if ($this->shouldValidateSchema) { |
||
| 67 | $entities[$name] = []; |
||
| 68 | |||
| 69 | /** @var ClassMetadataFactory $factory */ |
||
| 70 | $factory = $em->getMetadataFactory(); |
||
| 71 | $validator = new SchemaValidator($em); |
||
| 72 | |||
| 73 | /** @var ClassMetadataInfo $class */ |
||
| 74 | foreach ($factory->getLoadedMetadata() as $class) { |
||
| 75 | if (isset($entities[$name][$class->getName()])) { |
||
| 76 | continue; |
||
| 77 | } |
||
| 78 | |||
| 79 | $classErrors = $validator->validateClass($class); |
||
| 80 | $entities[$name][$class->getName()] = $class->getName(); |
||
| 81 | |||
| 82 | if (empty($classErrors)) { |
||
| 83 | continue; |
||
| 84 | } |
||
| 85 | |||
| 86 | $errors[$name][$class->getName()] = $classErrors; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | /** @var Configuration $emConfig */ |
||
| 91 | $emConfig = $em->getConfiguration(); |
||
| 92 | $slcEnabled = $emConfig->isSecondLevelCacheEnabled(); |
||
| 93 | |||
| 94 | if (! $slcEnabled) { |
||
| 95 | continue; |
||
| 96 | } |
||
| 97 | |||
| 98 | $caches['enabled'] = true; |
||
| 99 | |||
| 100 | /** @var $cacheConfiguration \Doctrine\ORM\Cache\CacheConfiguration */ |
||
| 101 | /** @var CacheLoggerChain $cacheLoggerChain */ |
||
| 102 | $cacheConfiguration = $emConfig->getSecondLevelCacheConfiguration(); |
||
| 103 | $cacheLoggerChain = $cacheConfiguration->getCacheLogger(); |
||
| 104 | |||
| 105 | if (! $cacheLoggerChain || ! $cacheLoggerChain->getLogger('statistics')) { |
||
| 106 | continue; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** @var StatisticsCacheLogger $cacheLoggerStats */ |
||
| 110 | $cacheLoggerStats = $cacheLoggerChain->getLogger('statistics'); |
||
| 111 | $caches['log_enabled'] = true; |
||
| 112 | |||
| 113 | $caches['counts']['puts'] += $cacheLoggerStats->getPutCount(); |
||
| 114 | $caches['counts']['hits'] += $cacheLoggerStats->getHitCount(); |
||
| 115 | $caches['counts']['misses'] += $cacheLoggerStats->getMissCount(); |
||
| 116 | |||
| 117 | View Code Duplication | foreach ($cacheLoggerStats->getRegionsPut() as $key => $value) { |
|
| 118 | if (! isset($caches['regions']['puts'][$key])) { |
||
| 119 | $caches['regions']['puts'][$key] = 0; |
||
| 120 | } |
||
| 121 | |||
| 122 | $caches['regions']['puts'][$key] += $value; |
||
| 123 | } |
||
| 124 | |||
| 125 | View Code Duplication | foreach ($cacheLoggerStats->getRegionsHit() as $key => $value) { |
|
| 126 | if (! isset($caches['regions']['hits'][$key])) { |
||
| 127 | $caches['regions']['hits'][$key] = 0; |
||
| 128 | } |
||
| 129 | |||
| 130 | $caches['regions']['hits'][$key] += $value; |
||
| 131 | } |
||
| 132 | |||
| 133 | View Code Duplication | foreach ($cacheLoggerStats->getRegionsMiss() as $key => $value) { |
|
| 134 | if (! isset($caches['regions']['misses'][$key])) { |
||
| 135 | $caches['regions']['misses'][$key] = 0; |
||
| 136 | } |
||
| 137 | |||
| 138 | $caches['regions']['misses'][$key] += $value; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 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 | // To be removed when the required minimum version of symfony/doctrine-bridge is >= 4.4 |
||
| 148 | $query['runnable'] = $query['runnable'] ?? true; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | $this->data['entities'] = $entities; |
||
| 153 | $this->data['errors'] = $errors; |
||
| 154 | $this->data['caches'] = $caches; |
||
| 155 | $this->groupedQueries = null; |
||
| 156 | } |
||
| 157 | |||
| 158 | public function getEntities() |
||
| 159 | { |
||
| 160 | return $this->data['entities']; |
||
| 161 | } |
||
| 162 | |||
| 163 | public function getMappingErrors() |
||
| 164 | { |
||
| 165 | return $this->data['errors']; |
||
| 166 | } |
||
| 167 | |||
| 168 | public function getCacheHitsCount() |
||
| 169 | { |
||
| 170 | return $this->data['caches']['counts']['hits']; |
||
| 171 | } |
||
| 172 | |||
| 173 | public function getCachePutsCount() |
||
| 174 | { |
||
| 175 | return $this->data['caches']['counts']['puts']; |
||
| 176 | } |
||
| 177 | |||
| 178 | public function getCacheMissesCount() |
||
| 179 | { |
||
| 180 | return $this->data['caches']['counts']['misses']; |
||
| 181 | } |
||
| 182 | |||
| 183 | public function getCacheEnabled() |
||
| 184 | { |
||
| 185 | return $this->data['caches']['enabled']; |
||
| 186 | } |
||
| 187 | |||
| 188 | public function getCacheRegions() |
||
| 189 | { |
||
| 190 | return $this->data['caches']['regions']; |
||
| 191 | } |
||
| 192 | |||
| 193 | public function getCacheCounts() |
||
| 194 | { |
||
| 195 | return $this->data['caches']['counts']; |
||
| 196 | } |
||
| 197 | |||
| 198 | public function getInvalidEntityCount() |
||
| 199 | { |
||
| 200 | if ($this->invalidEntityCount === null) { |
||
| 201 | $this->invalidEntityCount = array_sum(array_map('count', $this->data['errors'])); |
||
| 202 | } |
||
| 203 | |||
| 204 | return $this->invalidEntityCount; |
||
| 205 | } |
||
| 206 | |||
| 207 | public function getGroupedQueries() |
||
| 208 | { |
||
| 209 | if ($this->groupedQueries !== null) { |
||
| 210 | return $this->groupedQueries; |
||
| 211 | } |
||
| 212 | |||
| 213 | $this->groupedQueries = []; |
||
| 214 | $totalExecutionMS = 0; |
||
| 215 | foreach ($this->data['queries'] as $connection => $queries) { |
||
| 216 | $connectionGroupedQueries = []; |
||
| 217 | foreach ($queries as $i => $query) { |
||
| 218 | $key = $query['sql']; |
||
| 219 | if (! isset($connectionGroupedQueries[$key])) { |
||
| 220 | $connectionGroupedQueries[$key] = $query; |
||
| 221 | $connectionGroupedQueries[$key]['executionMS'] = 0; |
||
| 222 | $connectionGroupedQueries[$key]['count'] = 0; |
||
| 223 | $connectionGroupedQueries[$key]['index'] = $i; // "Explain query" relies on query index in 'queries'. |
||
| 224 | } |
||
| 225 | $connectionGroupedQueries[$key]['executionMS'] += $query['executionMS']; |
||
| 226 | $connectionGroupedQueries[$key]['count']++; |
||
| 227 | $totalExecutionMS += $query['executionMS']; |
||
| 228 | } |
||
| 229 | usort($connectionGroupedQueries, static function ($a, $b) { |
||
| 230 | if ($a['executionMS'] === $b['executionMS']) { |
||
| 231 | return 0; |
||
| 232 | } |
||
| 233 | |||
| 234 | return $a['executionMS'] < $b['executionMS'] ? 1 : -1; |
||
| 235 | }); |
||
| 236 | $this->groupedQueries[$connection] = $connectionGroupedQueries; |
||
| 237 | } |
||
| 238 | |||
| 239 | foreach ($this->groupedQueries as $connection => $queries) { |
||
| 240 | foreach ($queries as $i => $query) { |
||
| 241 | $this->groupedQueries[$connection][$i]['executionPercent'] = |
||
| 242 | $this->executionTimePercentage($query['executionMS'], $totalExecutionMS); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | return $this->groupedQueries; |
||
| 247 | } |
||
| 248 | |||
| 249 | private function executionTimePercentage($executionTimeMS, $totalExecutionTimeMS) |
||
| 257 | |||
| 258 | public function getGroupedQueryCount() |
||
| 267 | } |
||
| 268 |