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