| Conditions | 26 |
| Paths | 10890 |
| Total Lines | 129 |
| Code Lines | 79 |
| Lines | 6 |
| Ratio | 4.65 % |
| 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 |
||
| 73 | public function migrate() |
||
| 74 | { |
||
| 75 | $factory = DocBlockFactory::createInstance(); |
||
| 76 | $contextFactory = new ContextFactory(); |
||
| 77 | |||
| 78 | $schema = $this->mapper->getSchema(); |
||
| 79 | |||
| 80 | $computes = []; |
||
| 81 | foreach ($this->entityClasses as $entity) { |
||
| 82 | |||
| 83 | $spaceName = $this->getSpaceName($entity); |
||
| 84 | |||
| 85 | $engine = 'memtx'; |
||
| 86 | if (array_key_exists($spaceName, $this->repositoryMapping)) { |
||
| 87 | $repositoryClass = $this->repositoryMapping[$spaceName]; |
||
| 88 | $repositoryReflection = new ReflectionClass($repositoryClass); |
||
| 89 | $repositoryProperties = $repositoryReflection->getDefaultProperties(); |
||
| 90 | if (array_key_exists('engine', $repositoryProperties)) { |
||
| 91 | $engine = $repositoryProperties['engine']; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | if ($schema->hasSpace($spaceName)) { |
||
| 96 | $space = $schema->getSpace($spaceName); |
||
| 97 | if ($space->getEngine() != $engine) { |
||
| 98 | throw new Exception("Space engine can't be updated"); |
||
| 99 | } |
||
| 100 | } else { |
||
| 101 | $space = $schema->createSpace($spaceName, [ |
||
| 102 | 'engine' => $engine, |
||
| 103 | 'properties' => [], |
||
| 104 | ]); |
||
| 105 | } |
||
| 106 | |||
| 107 | $class = new ReflectionClass($entity); |
||
| 108 | |||
| 109 | foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { |
||
| 110 | $context = $contextFactory->createFromReflector($property); |
||
| 111 | $description = $factory->create($property->getDocComment(), $context); |
||
| 112 | $tags = $description->getTags('var'); |
||
| 113 | |||
| 114 | View Code Duplication | if (!count($tags)) { |
|
| 115 | throw new Exception("No var tag for ".$entity.'::'.$property->getName()); |
||
| 116 | } |
||
| 117 | |||
| 118 | View Code Duplication | if (count($tags) > 1) { |
|
| 119 | throw new Exception("Invalid var tag for ".$entity.'::'.$property->getName()); |
||
| 120 | } |
||
| 121 | |||
| 122 | $propertyName = $property->getName(); |
||
| 123 | $phpType = $tags[0]->getType(); |
||
| 124 | $type = $this->getTarantoolType($phpType); |
||
| 125 | |||
| 126 | if (!$space->hasProperty($propertyName)) { |
||
| 127 | if ($this->isReference($phpType)) { |
||
| 128 | $space->addProperty($propertyName, $type, true, $this->getSpaceName((string) $phpType)); |
||
| 129 | } else { |
||
| 130 | $space->addProperty($propertyName, $type); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | if ($this->mapper->hasPlugin(NestedSet::class)) { |
||
| 135 | $nested = $this->mapper->getPlugin(NestedSet::class); |
||
| 136 | if ($nested->isNested($space)) { |
||
| 137 | $nested->addIndexes($space); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | if ($class->hasMethod('compute')) { |
||
| 142 | $computes[] = $spaceName; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | |||
| 147 | foreach ($this->repositoryClasses as $repository) { |
||
| 148 | $spaceName = $this->getSpaceName($repository); |
||
| 149 | |||
| 150 | if (!$schema->hasSpace($spaceName)) { |
||
| 151 | throw new Exception("Repository $spaceName has no entity definition"); |
||
| 152 | } |
||
| 153 | |||
| 154 | $this->mapRepository($spaceName, $repository); |
||
| 155 | |||
| 156 | $space = $schema->getSpace($spaceName); |
||
| 157 | |||
| 158 | $class = new ReflectionClass($repository); |
||
| 159 | $properties = $class->getDefaultProperties(); |
||
| 160 | |||
| 161 | if (array_key_exists('indexes', $properties)) { |
||
| 162 | foreach ($properties['indexes'] as $i => $index) { |
||
| 163 | if (!is_array($index)) { |
||
| 164 | $index = (array) $index; |
||
| 165 | } |
||
| 166 | if (!array_key_exists('fields', $index)) { |
||
| 167 | $index = ['fields' => $index]; |
||
| 168 | } |
||
| 169 | |||
| 170 | $index['if_not_exists'] = true; |
||
| 171 | try { |
||
| 172 | $space->addIndex($index); |
||
| 173 | } catch (Exception $e) { |
||
| 174 | $presentation = json_encode($properties['indexes'][$i]); |
||
| 175 | throw new Exception("Failed to add index $presentation. ". $e->getMessage(), 0, $e); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | foreach ($schema->getSpaces() as $space) { |
||
| 181 | if (!count($space->getIndexes())) { |
||
| 182 | if (!$space->hasProperty('id')) { |
||
| 183 | throw new Exception("No primary index on ". $space->getName()); |
||
| 184 | } |
||
| 185 | $space->addIndex(['id']); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | foreach ($computes as $spaceName) { |
||
| 190 | $method = new ReflectionMethod($this->entityMapping[$spaceName], 'compute'); |
||
| 191 | $type = (string) $method->getParameters()[0]->getType(); |
||
| 192 | $sourceSpace = array_search($type, $this->entityMapping); |
||
| 193 | if (!$sourceSpace) { |
||
| 194 | throw new Exception("Invalid compute source $type"); |
||
| 195 | } |
||
| 196 | $compute = Closure::fromCallable([$this->entityMapping[$spaceName], 'compute']); |
||
| 197 | $this->mapper->getPlugin(Compute::class)->register($sourceSpace, $spaceName, $compute); |
||
| 198 | } |
||
| 199 | |||
| 200 | return $this; |
||
| 201 | } |
||
| 202 | |||
| 279 |