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