| Conditions | 10 | 
| Paths | 9 | 
| Total Lines | 87 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 0 | ||
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 | ||
| 57 | private function handleDirectoryMapping(ContainerBuilder $container, string $dir): void | ||
| 58 |     { | ||
| 59 | /** @var DocumentParser $parser */ | ||
| 60 | $parser = $container->get(DocumentParser::class); | ||
| 61 | $indexesOverride = $container->getParameter(Configuration::ONGR_INDEXES_OVERRIDE); | ||
| 62 | $converterDefinition = $container->getDefinition(Converter::class); | ||
| 63 | |||
| 64 |         foreach ($this->getNamespaces($dir) as $namespace) { | ||
| 65 | $class = new \ReflectionClass($namespace); | ||
| 66 | |||
| 67 |             if (isset($indexesOverride[$namespace]['alias']) && $indexesOverride[$namespace]['alias']) { | ||
| 68 | $indexAlias = $indexesOverride[$namespace]['alias']; | ||
| 69 |             } else { | ||
| 70 | $indexAlias = $parser->getIndexAliasName($class); | ||
| 71 | } | ||
| 72 | |||
| 73 | /** @var Index $document */ | ||
| 74 | $document = $parser->getIndexAnnotation($class); | ||
| 75 | $indexMetadata = $parser->getIndexMetadata($class); | ||
| 76 | |||
| 77 |             if (!empty($indexMetadata)) { | ||
| 78 | $indexMetadata['settings'] = array_filter( | ||
| 79 | array_replace_recursive( | ||
| 80 | $indexMetadata['settings'] ?? [], | ||
| 81 | [ | ||
| 82 | 'number_of_replicas' => $document->numberOfReplicas, | ||
| 83 | 'number_of_shards' => $document->numberOfShards, | ||
| 84 | ], | ||
| 85 | $indexesOverride[$namespace]['settings'] ?? [] | ||
| 86 | ), | ||
| 87 |                     function ($value) { | ||
| 88 |                         if (0 === $value) { | ||
| 89 | return true; | ||
| 90 | } | ||
| 91 | |||
| 92 | return (bool)$value; | ||
| 93 | } | ||
| 94 | ); | ||
| 95 | |||
| 96 | $indexSettings = new Definition( | ||
| 97 | IndexSettings::class, | ||
| 98 | [ | ||
| 99 | $namespace, | ||
| 100 | $indexAlias, | ||
| 101 | $indexAlias, | ||
| 102 | $indexMetadata, | ||
| 103 | $indexesOverride[$namespace]['hosts'] ?? $document->hosts, | ||
| 104 | $indexesOverride[$namespace]['default'] ?? $document->default, | ||
| 105 | ] | ||
| 106 | ); | ||
| 107 | |||
| 108 | $indexServiceDefinition = new Definition(IndexService::class, [ | ||
| 109 | $namespace, | ||
| 110 | $converterDefinition, | ||
| 111 |                     $container->getDefinition('event_dispatcher'), | ||
| 112 | $indexSettings, | ||
| 113 | $container->getParameter(Configuration::ONGR_PROFILER_CONFIG) | ||
| 114 |                         ? $container->getDefinition('ongr.esb.tracer') : null | ||
| 115 | ]); | ||
| 116 | $indexServiceDefinition->setPublic(true); | ||
| 117 | $converterDefinition->addMethodCall( | ||
| 118 | 'addClassMetadata', | ||
| 119 | [ | ||
| 120 | $namespace, | ||
| 121 | $parser->getPropertyMetadata($class) | ||
| 122 | ] | ||
| 123 | ); | ||
| 124 | |||
| 125 | $container->setDefinition($namespace, $indexServiceDefinition); | ||
| 126 | $this->indexes[$indexAlias] = $namespace; | ||
| 127 | $isCurrentIndexDefault = $parser->isDefaultIndex($class); | ||
| 128 |                 if ($this->defaultIndex && $isCurrentIndexDefault) { | ||
| 129 | throw new \RuntimeException( | ||
| 130 | sprintf( | ||
| 131 | 'Only one index can be set as default. We found 2 indexes as default ones `%s` and `%s`', | ||
| 132 | $this->defaultIndex, | ||
| 133 | $indexAlias | ||
| 134 | ) | ||
| 135 | ); | ||
| 136 | } | ||
| 137 | |||
| 138 |                 if ($isCurrentIndexDefault) { | ||
| 139 | $this->defaultIndex = $indexAlias; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 184 |