| Conditions | 14 |
| Paths | 66 |
| Total Lines | 72 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 18 | public function process(ContainerBuilder $container) |
||
| 19 | { |
||
| 20 | if (!$container->hasParameter('doctrine.entity_managers')) { |
||
| 21 | return; |
||
| 22 | } |
||
| 23 | |||
| 24 | $entityManagers = $container->getParameter('doctrine.entity_managers'); |
||
| 25 | $customRepositories = []; |
||
| 26 | |||
| 27 | foreach ($entityManagers as $name => $serviceName) { |
||
| 28 | $metadataDriverService = sprintf('doctrine.orm.%s_metadata_driver', $name); |
||
| 29 | |||
| 30 | if (!$container->has($metadataDriverService)) { |
||
| 31 | continue; |
||
| 32 | } |
||
| 33 | |||
| 34 | /** @var MappingDriver $metadataDriver */ |
||
| 35 | $metadataDriver = $container->get($metadataDriverService); |
||
| 36 | $entityClassNames = $metadataDriver->getAllClassNames(); |
||
| 37 | |||
| 38 | foreach ($entityClassNames as $entityClassName) { |
||
| 39 | $classMetadata = new ClassMetadata($entityClassName); |
||
| 40 | $metadataDriver->loadMetadataForClass($entityClassName, $classMetadata); |
||
| 41 | |||
| 42 | if ($classMetadata->customRepositoryClassName) { |
||
| 43 | $customRepositories[$classMetadata->customRepositoryClassName][] = [ |
||
| 44 | 0 => $entityClassName, |
||
| 45 | 1 => $name, |
||
| 46 | ]; |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | $rootConflicts = []; |
||
| 51 | foreach ($customRepositories as $repositoryClass => $entities) { |
||
| 52 | $repoConflicts = $this->findConflictingServices($container, $repositoryClass); |
||
| 53 | |||
| 54 | if (count($repoConflicts)) { |
||
| 55 | $rootConflicts[$repositoryClass] = $repoConflicts; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | foreach ($customRepositories as $repositoryClass => $entities) { |
||
| 60 | if ($container->has($repositoryClass)) { |
||
| 61 | continue; |
||
| 62 | } |
||
| 63 | |||
| 64 | if (count($entities) !== 1) { |
||
| 65 | $this->log($container, "Cannot auto-register repository \"".$repositoryClass."\": Entity belongs to multiple entity managers."); |
||
| 66 | continue; |
||
| 67 | } |
||
| 68 | |||
| 69 | if (isset($rootConflicts[$repositoryClass])) { |
||
| 70 | $this->log($container, "Cannot auto-register repository \"".$repositoryClass."\": There are already services for the repository class."); |
||
| 71 | continue; |
||
| 72 | } |
||
| 73 | |||
| 74 | foreach ($rootConflicts as $conflictingRepo => $conflicts) { |
||
| 75 | if (is_subclass_of($repositoryClass, $conflictingRepo, true)) { |
||
|
|
|||
| 76 | $this->log($container, "Cannot auto-register repository \"".$repositoryClass."\": There are already services for a superclass of the repository class."); |
||
| 77 | continue 2; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | $container->register($repositoryClass, $repositoryClass) |
||
| 82 | ->setFactory([new Reference('doctrine'), 'getRepository']) |
||
| 83 | ->setArguments($entities[0]) |
||
| 84 | ->setShared(false) |
||
| 85 | ->setPublic(false) |
||
| 86 | ; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 120 |