| Conditions | 20 |
| Paths | 829 |
| Total Lines | 93 |
| Code Lines | 55 |
| Lines | 6 |
| Ratio | 6.45 % |
| 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 | foreach ($this->entityClasses as $entity) { |
||
| 81 | $spaceName = $this->getSpaceName($entity); |
||
| 82 | $space = $schema->hasSpace($spaceName) ? $schema->getSpace($spaceName) : $schema->createSpace($spaceName); |
||
| 83 | |||
| 84 | $this->mapEntity($spaceName, $entity); |
||
| 85 | |||
| 86 | $class = new ReflectionClass($entity); |
||
| 87 | |||
| 88 | foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { |
||
| 89 | $context = $contextFactory->createFromReflector($property); |
||
| 90 | $description = $factory->create($property->getDocComment(), $context); |
||
| 91 | $tags = $description->getTags('var'); |
||
| 92 | |||
| 93 | View Code Duplication | if (!count($tags)) { |
|
| 94 | throw new Exception("No var tag for ".$entity.'::'.$property->getName()); |
||
| 95 | } |
||
| 96 | |||
| 97 | View Code Duplication | if (count($tags) > 1) { |
|
| 98 | throw new Exception("Invalid var tag for ".$entity.'::'.$property->getName()); |
||
| 99 | } |
||
| 100 | |||
| 101 | $propertyName = $this->toUnderscore($property->getName()); |
||
| 102 | $phpType = $tags[0]->getType(); |
||
| 103 | $type = $this->getTarantoolType($phpType); |
||
| 104 | |||
| 105 | if (!$space->hasProperty($propertyName)) { |
||
| 106 | if ($this->isReference($phpType)) { |
||
| 107 | $space->addProperty($propertyName, $type, $this->getSpaceName((string) $phpType)); |
||
| 108 | } else { |
||
| 109 | $space->addProperty($propertyName, $type); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | } |
||
| 113 | if ($this->mapper->hasPlugin(NestedSet::class)) { |
||
| 114 | $nested = $this->mapper->getPlugin(NestedSet::class); |
||
| 115 | if ($nested->isNested($space)) { |
||
| 116 | $nested->addIndexes($space); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | foreach ($this->repositoryClasses as $repository) { |
||
| 122 | $spaceName = $this->getSpaceName($repository); |
||
| 123 | |||
| 124 | if (!$schema->hasSpace($spaceName)) { |
||
| 125 | throw new Exception("Repository with no entity definition"); |
||
| 126 | } |
||
| 127 | |||
| 128 | $this->mapRepository($spaceName, $repository); |
||
| 129 | |||
| 130 | $space = $schema->getSpace($spaceName); |
||
| 131 | |||
| 132 | $class = new ReflectionClass($repository); |
||
| 133 | $properties = $class->getDefaultProperties(); |
||
| 134 | |||
| 135 | if (array_key_exists('indexes', $properties)) { |
||
| 136 | foreach ($properties['indexes'] as $i => $index) { |
||
| 137 | if (!is_array($index)) { |
||
| 138 | $index = (array) $index; |
||
| 139 | } |
||
| 140 | if (!array_key_exists('fields', $index)) { |
||
| 141 | $index = ['fields' => $index]; |
||
| 142 | } |
||
| 143 | |||
| 144 | $index['if_not_exists'] = true; |
||
| 145 | try { |
||
| 146 | $space->addIndex($index); |
||
| 147 | } catch (Exception $e) { |
||
| 148 | $presentation = json_encode($properties['indexes'][$i]); |
||
| 149 | throw new Exception("Failed to add index $presentation. ". $e->getMessage(), 0, $e); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | foreach ($schema->getSpaces() as $space) { |
||
| 156 | if (!count($space->getIndexes())) { |
||
| 157 | if (!$space->hasProperty('id')) { |
||
| 158 | throw new Exception("No primary index on ". $space->getName()); |
||
| 159 | } |
||
| 160 | $space->addIndex(['id']); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | return $this; |
||
| 165 | } |
||
| 166 | |||
| 258 |