Complex classes like DoctrineExtension 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 DoctrineExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 52 | class DoctrineExtension extends CompilerExtension |
||
| 53 | { |
||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | public const DOCTRINE_SQL_PANEL = DoctrineSQLPanel::class; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | public static $defaults = [ |
||
| 63 | 'debug' => TRUE, |
||
| 64 | 'dbal' => [ |
||
| 65 | 'type_overrides' => [], |
||
| 66 | 'types' => [], |
||
| 67 | 'schema_filter' => NULL, |
||
| 68 | ], |
||
| 69 | 'prefix' => 'doctrine.default', |
||
| 70 | 'proxyDir' => '%tempDir%/cache/proxies', |
||
| 71 | 'sourceDir' => NULL, |
||
| 72 | 'targetEntityMappings' => [], |
||
| 73 | 'metadata' => [], |
||
| 74 | 'functions' => [], |
||
| 75 | // caches |
||
| 76 | 'metadataCache' => 'default', |
||
| 77 | 'queryCache' => 'default', |
||
| 78 | 'resultCache' => 'default', |
||
| 79 | 'hydrationCache' => 'default', |
||
| 80 | 'secondLevelCache' => [ |
||
| 81 | 'enabled' => FALSE, |
||
| 82 | 'factoryClass' => DefaultCacheFactory::class, |
||
| 83 | 'driver' => 'default', |
||
| 84 | 'regions' => [ |
||
| 85 | 'defaultLifetime' => 3600, |
||
| 86 | 'defaultLockLifetime' => 60, |
||
| 87 | ], |
||
| 88 | 'fileLockRegionDirectory' => '%tempDir%/cache/Doctrine.Cache.Locks', |
||
| 89 | 'logging' => '%debugMode%', |
||
| 90 | ], |
||
| 91 | ]; |
||
| 92 | |||
| 93 | private $entitySources = []; |
||
| 94 | |||
| 95 | private $classMappings = []; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritdoc} |
||
| 99 | */ |
||
| 100 | 1 | public function loadConfiguration(): void |
|
| 101 | { |
||
| 102 | 1 | $config = $this->parseConfig(); |
|
| 103 | |||
| 104 | 1 | $builder = $this->getContainerBuilder(); |
|
| 105 | 1 | $name = $config['prefix']; |
|
| 106 | |||
| 107 | 1 | $configurationDefinition = $builder->addDefinition($name . '.config') |
|
| 108 | 1 | ->setType(Configuration::class) |
|
| 109 | 1 | ->addSetup('setFilterSchemaAssetsExpression', [$config['dbal']['schema_filter']]); |
|
| 110 | |||
| 111 | 1 | if ($config['metadataCache'] !== FALSE) { |
|
| 112 | 1 | $configurationDefinition->addSetup( |
|
| 113 | 1 | 'setMetadataCacheImpl', |
|
| 114 | 1 | [$this->getCache($name . '.metadata', $builder, $config['metadataCache'])] |
|
| 115 | ); |
||
| 116 | } |
||
| 117 | |||
| 118 | 1 | if ($config['queryCache'] !== FALSE) { |
|
| 119 | 1 | $configurationDefinition->addSetup( |
|
| 120 | 1 | 'setQueryCacheImpl', |
|
| 121 | 1 | [$this->getCache($name . '.query', $builder, $config['queryCache'])] |
|
| 122 | ); |
||
| 123 | } |
||
| 124 | |||
| 125 | 1 | if ($config['resultCache'] !== FALSE) { |
|
| 126 | 1 | $configurationDefinition->addSetup( |
|
| 127 | 1 | 'setResultCacheImpl', |
|
| 128 | 1 | [$this->getCache($name . '.ormResult', $builder, $config['resultCache'])] |
|
| 129 | ); |
||
| 130 | } |
||
| 131 | |||
| 132 | 1 | if ($config['hydrationCache'] !== FALSE) { |
|
| 133 | 1 | $configurationDefinition->addSetup( |
|
| 134 | 1 | 'setHydrationCacheImpl', |
|
| 135 | 1 | [$this->getCache($name . '.hydration', $builder, $config['hydrationCache'])] |
|
| 136 | ); |
||
| 137 | } |
||
| 138 | |||
| 139 | 1 | $this->processSecondLevelCache($name, $config['secondLevelCache']); |
|
| 140 | |||
| 141 | 1 | $builder->addDefinition($name . '.connection') |
|
| 142 | 1 | ->setType(Connection::class) |
|
| 143 | 1 | ->setFactory('@' . $name . '.entityManager::getConnection'); |
|
| 144 | |||
| 145 | 1 | $builder->addDefinition($name . '.entityManager') |
|
| 146 | 1 | ->setType(EntityManager::class) |
|
| 147 | 1 | ->setFactory( |
|
| 148 | 1 | '\Doctrine\ORM\EntityManager::create', |
|
| 149 | [ |
||
| 150 | 1 | $config['connection'], |
|
| 151 | 1 | '@' . $name . '.config', |
|
| 152 | 1 | '@Doctrine\Common\EventManager', |
|
| 153 | ] |
||
| 154 | ); |
||
| 155 | |||
| 156 | 1 | $builder->addDefinition($name . '.namingStrategy') |
|
| 157 | 1 | ->setType(UnderscoreNamingStrategy::class); |
|
| 158 | |||
| 159 | 1 | $builder->addDefinition($name . '.resolver') |
|
| 160 | 1 | ->setType(ResolveTargetEntityListener::class); |
|
| 161 | |||
| 162 | 1 | if ($this->hasIBarPanelInterface()) { |
|
| 163 | 1 | $builder->addDefinition($this->prefix($name . '.diagnosticsPanel')) |
|
| 164 | 1 | ->setType(self::DOCTRINE_SQL_PANEL); |
|
| 165 | } |
||
| 166 | 1 | } |
|
| 167 | |||
| 168 | /** |
||
| 169 | * {@inheritdoc} |
||
| 170 | */ |
||
| 171 | 1 | public function beforeCompile(): void |
|
| 172 | { |
||
| 173 | 1 | $config = $this->getConfig(self::$defaults); |
|
| 174 | 1 | $name = $config['prefix']; |
|
| 175 | |||
| 176 | 1 | $builder = $this->getContainerBuilder(); |
|
| 177 | 1 | $cache = $this->getCache($name, $builder, 'default'); |
|
| 178 | |||
| 179 | 1 | $configDefinition = $builder->getDefinition($name . '.config') |
|
| 180 | 1 | ->setFactory( |
|
| 181 | 1 | '\Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration', |
|
| 182 | [ |
||
| 183 | 1 | array_values($this->entitySources), |
|
| 184 | 1 | $config['debug'], |
|
| 185 | 1 | $config['proxyDir'], |
|
| 186 | 1 | $cache, |
|
| 187 | FALSE, |
||
| 188 | ] |
||
| 189 | ) |
||
| 190 | 1 | ->addSetup('setNamingStrategy', ['@' . $name . '.namingStrategy']); |
|
| 191 | |||
| 192 | 1 | foreach ($config['functions'] as $functionName => $function) { |
|
| 193 | 1 | $configDefinition->addSetup('addCustomStringFunction', [$functionName, $function]); |
|
| 194 | } |
||
| 195 | |||
| 196 | 1 | foreach ($this->classMappings as $source => $target) { |
|
| 197 | $builder->getDefinition($name . '.resolver') |
||
| 198 | ->addSetup('addResolveTargetEntity', [$source, $target, []]); |
||
| 199 | } |
||
| 200 | |||
| 201 | 1 | $this->processDbalTypes($name, $config['dbal']['types']); |
|
| 202 | 1 | $this->processDbalTypeOverrides($name, $config['dbal']['type_overrides']); |
|
| 203 | 1 | $this->processEventSubscribers($name); |
|
| 204 | 1 | $this->processFilters($name); |
|
| 205 | |||
| 206 | // import Doctrine commands into Portiny/Console |
||
| 207 | 1 | if ($this->hasPortinyConsole()) { |
|
| 208 | $commands = [ |
||
| 209 | ConvertMappingCommand::class, |
||
| 210 | CreateCommand::class, |
||
| 211 | DropCommand::class, |
||
| 212 | GenerateEntitiesCommand::class, |
||
| 213 | GenerateProxiesCommand::class, |
||
| 214 | ImportCommand::class, |
||
| 215 | MetadataCommand::class, |
||
| 216 | QueryCommand::class, |
||
| 217 | ResultCommand::class, |
||
| 218 | UpdateCommand::class, |
||
| 219 | ValidateSchemaCommand::class, |
||
| 220 | ]; |
||
| 221 | foreach ($commands as $index => $command) { |
||
| 222 | $builder->addDefinition($name . '.command.' . $index) |
||
| 223 | ->setType($command); |
||
| 224 | } |
||
| 225 | |||
| 226 | $helperSets = $builder->findByType(HelperSet::class); |
||
| 227 | if ($helperSets) { |
||
|
|
|||
| 228 | $helperSet = reset($helperSets); |
||
| 229 | $helperSet->addSetup('set', [new Statement(EntityManagerHelper::class), 'em']); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | 1 | } |
|
| 233 | |||
| 234 | /** |
||
| 235 | * {@inheritdoc} |
||
| 236 | */ |
||
| 237 | 1 | public function afterCompile(ClassType $classType): void |
|
| 252 | |||
| 253 | 1 | protected function processSecondLevelCache($name, array $config): void |
|
| 297 | |||
| 298 | 1 | private function getCache(string $prefix, ContainerBuilder $containerBuilder, string $cacheType): string |
|
| 320 | |||
| 321 | /** |
||
| 322 | * @throws AssertionException |
||
| 323 | */ |
||
| 324 | 1 | private function parseConfig(): array |
|
| 350 | |||
| 351 | 1 | private function hasIBarPanelInterface(): bool |
|
| 355 | |||
| 356 | 1 | private function hasPortinyConsole(): bool |
|
| 360 | |||
| 361 | 1 | private function hasEventManager(ContainerBuilder $containerBuilder): bool |
|
| 366 | |||
| 367 | 1 | private function processDbalTypes(string $name, array $types): void |
|
| 379 | |||
| 380 | 1 | private function processDbalTypeOverrides(string $name, array $types): void |
|
| 389 | |||
| 390 | 1 | private function processEventSubscribers(string $name): void |
|
| 407 | |||
| 408 | 1 | private function processFilters(string $name): void |
|
| 417 | } |
||
| 418 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.