| Conditions | 39 |
| Paths | > 20000 |
| Total Lines | 182 |
| 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 |
||
| 106 | public function migrate($extensionInstances = true) : self |
||
| 107 | { |
||
| 108 | $factory = DocBlockFactory::createInstance(); |
||
| 109 | $contextFactory = new ContextFactory(); |
||
| 110 | |||
| 111 | $schema = $this->mapper->getSchema(); |
||
| 112 | |||
| 113 | $computes = []; |
||
| 114 | foreach ($this->entityClasses as $entity) { |
||
| 115 | if ($this->isExtension($entity)) { |
||
| 116 | continue; |
||
| 117 | } |
||
| 118 | |||
| 119 | $spaceName = $this->getSpaceName($entity); |
||
| 120 | |||
| 121 | $engine = 'memtx'; |
||
| 122 | if (array_key_exists($spaceName, $this->repositoryMapping)) { |
||
| 123 | $repositoryClass = $this->repositoryMapping[$spaceName]; |
||
| 124 | $repositoryReflection = new ReflectionClass($repositoryClass); |
||
| 125 | $repositoryProperties = $repositoryReflection->getDefaultProperties(); |
||
| 126 | if (array_key_exists('engine', $repositoryProperties)) { |
||
| 127 | $engine = $repositoryProperties['engine']; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($schema->hasSpace($spaceName)) { |
||
| 132 | $space = $schema->getSpace($spaceName); |
||
| 133 | if ($space->getEngine() != $engine) { |
||
| 134 | throw new Exception("Space engine can't be updated"); |
||
| 135 | } |
||
| 136 | } else { |
||
| 137 | $space = $schema->createSpace($spaceName, [ |
||
| 138 | 'engine' => $engine, |
||
| 139 | 'properties' => [], |
||
| 140 | ]); |
||
| 141 | } |
||
| 142 | |||
| 143 | $class = new ReflectionClass($entity); |
||
| 144 | |||
| 145 | foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { |
||
| 146 | $context = $contextFactory->createFromReflector($property); |
||
| 147 | $description = $factory->create($property->getDocComment(), $context); |
||
| 148 | $tags = $description->getTags('var'); |
||
| 149 | |||
| 150 | if (!count($tags)) { |
||
| 151 | throw new Exception("No var tag for ".$entity.'::'.$property->getName()); |
||
| 152 | } |
||
| 153 | |||
| 154 | $byTypes = []; |
||
| 155 | foreach ($tags as $candidate) { |
||
| 156 | $byTypes[$candidate->getName()] = $candidate; |
||
| 157 | } |
||
| 158 | |||
| 159 | if (!array_key_exists('var', $byTypes)) { |
||
| 160 | throw new Exception("No var tag for ".$entity.'::'.$property->getName()); |
||
| 161 | } |
||
| 162 | |||
| 163 | $propertyName = $property->getName(); |
||
| 164 | $phpType = $byTypes['var']->getType(); |
||
| 165 | |||
| 166 | if (array_key_exists('type', $byTypes)) { |
||
| 167 | $type = (string) $byTypes['type']->getDescription(); |
||
| 168 | } else { |
||
| 169 | $type = $this->getTarantoolType((string) $phpType); |
||
| 170 | } |
||
| 171 | |||
| 172 | $isNullable = true; |
||
| 173 | |||
| 174 | if (array_key_exists('required', $byTypes)) { |
||
| 175 | $isNullable = false; |
||
| 176 | } |
||
| 177 | |||
| 178 | if (!$space->hasProperty($propertyName)) { |
||
| 179 | $opts = [ |
||
| 180 | 'is_nullable' => $isNullable, |
||
| 181 | ]; |
||
| 182 | if ($this->isReference((string) $phpType)) { |
||
| 183 | $opts['reference'] = $this->getSpaceName((string) $phpType); |
||
| 184 | } |
||
| 185 | if (array_key_exists('default', $byTypes)) { |
||
| 186 | $opts['default'] = $schema->formatValue($type, (string) $byTypes['default']); |
||
| 187 | } |
||
| 188 | $space->addProperty($propertyName, $type, $opts); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | if ($this->mapper->hasPlugin(NestedSet::class)) { |
||
| 192 | $nested = $this->mapper->getPlugin(NestedSet::class); |
||
| 193 | if ($nested->isNested($space)) { |
||
| 194 | $nested->addIndexes($space); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | if ($class->hasMethod('compute')) { |
||
| 199 | $computes[] = $spaceName; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | foreach ($this->repositoryClasses as $repository) { |
||
| 204 | $spaceName = $this->getSpaceName($repository); |
||
| 205 | |||
| 206 | if (!$schema->hasSpace($spaceName)) { |
||
| 207 | throw new Exception("Repository $spaceName has no entity definition"); |
||
| 208 | } |
||
| 209 | |||
| 210 | $this->mapRepository($spaceName, $repository); |
||
| 211 | |||
| 212 | $space = $schema->getSpace($spaceName); |
||
| 213 | |||
| 214 | $class = new ReflectionClass($repository); |
||
| 215 | $properties = $class->getDefaultProperties(); |
||
| 216 | |||
| 217 | if (array_key_exists('indexes', $properties)) { |
||
| 218 | foreach ($properties['indexes'] as $i => $index) { |
||
| 219 | if (!is_array($index)) { |
||
| 220 | $index = (array) $index; |
||
| 221 | } |
||
| 222 | if (!array_key_exists('fields', $index)) { |
||
| 223 | $index = ['fields' => $index]; |
||
| 224 | } |
||
| 225 | |||
| 226 | $index['if_not_exists'] = true; |
||
| 227 | try { |
||
| 228 | $space->addIndex($index); |
||
| 229 | } catch (Exception $e) { |
||
| 230 | $presentation = json_encode($properties['indexes'][$i]); |
||
| 231 | throw new Exception("Failed to add index $presentation. ". $e->getMessage(), 0, $e); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | } |
||
| 236 | foreach ($schema->getSpaces() as $space) { |
||
| 237 | if ($space->isSystem()) { |
||
| 238 | continue; |
||
| 239 | } |
||
| 240 | if (!count($space->getIndexes())) { |
||
| 241 | if (!$space->hasProperty('id')) { |
||
| 242 | throw new Exception("No primary index on ". $space->getName()); |
||
| 243 | } |
||
| 244 | $space->addIndex(['id']); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | foreach ($computes as $spaceName) { |
||
| 249 | $method = new ReflectionMethod($this->entityMapping[$spaceName], 'compute'); |
||
| 250 | $type = (string) $method->getParameters()[0]->getType(); |
||
| 251 | $sourceSpace = array_search($type, $this->entityMapping); |
||
| 252 | if (!$sourceSpace) { |
||
| 253 | throw new Exception("Invalid compute source $type"); |
||
| 254 | } |
||
| 255 | $compute = Closure::fromCallable([$this->entityMapping[$spaceName], 'compute']); |
||
| 256 | $this->mapper->getPlugin(Compute::class)->register($sourceSpace, $spaceName, $compute); |
||
| 257 | } |
||
| 258 | |||
| 259 | foreach ($this->entityClasses as $entity) { |
||
| 260 | if ($this->isExtension($entity)) { |
||
| 261 | continue; |
||
| 262 | } |
||
| 263 | if (in_array($entity, $this->extensions)) { |
||
| 264 | $spaceName = $this->getSpaceName($entity); |
||
| 265 | $space = $schema->getSpace($spaceName); |
||
| 266 | if (!$space->hasProperty('class')) { |
||
| 267 | throw new Exception("$entity has extensions, but not class property is defined"); |
||
| 268 | } |
||
| 269 | if ($space->castIndex(['class' => ''], true) === null) { |
||
| 270 | $space->addIndex('class', [ |
||
| 271 | 'unique' => true, |
||
| 272 | ]); |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | if ($extensionInstances) { |
||
| 278 | foreach ($this->extensions as $class => $target) { |
||
| 279 | $space = $this->getSpaceName($target); |
||
| 280 | $this->mapper->findOrCreate($space, [ |
||
| 281 | 'class' => $class, |
||
| 282 | ]); |
||
| 283 | } |
||
| 284 | } |
||
| 285 | |||
| 286 | return $this; |
||
| 287 | } |
||
| 288 | |||
| 370 |