| Conditions | 18 |
| Paths | 760 |
| Total Lines | 84 |
| Code Lines | 46 |
| Lines | 6 |
| Ratio | 7.14 % |
| 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 | } |
||
| 107 | |||
| 108 | $plural = []; |
||
| 109 | foreach ($this->mapper->getSchema()->getSpaces() as $space) { |
||
| 110 | $plural[$this->pluralize($space->getName())] = $space->getName(); |
||
| 111 | } |
||
| 112 | |||
| 113 | foreach ($this->repositoryClasses as $repository) { |
||
| 114 | $spaceName = $this->getSpaceName($repository); |
||
| 115 | |||
| 116 | if (array_key_exists($spaceName, $plural)) { |
||
| 117 | $spaceName = $plural[$spaceName]; |
||
| 118 | } |
||
| 119 | |||
| 120 | if (!$schema->hasSpace($spaceName)) { |
||
| 121 | throw new Exception("Repository with no entity definition"); |
||
| 122 | } |
||
| 123 | |||
| 124 | $this->mapRepository($spaceName, $repository); |
||
| 125 | |||
| 126 | $space = $schema->getSpace($spaceName); |
||
| 127 | |||
| 128 | $class = new ReflectionClass($repository); |
||
| 129 | $properties = $class->getDefaultProperties(); |
||
| 130 | |||
| 131 | if (array_key_exists('indexes', $properties)) { |
||
| 132 | foreach ($properties['indexes'] as $index) { |
||
| 133 | if (!is_array($index)) { |
||
| 134 | $index = (array) $index; |
||
| 135 | } |
||
| 136 | if (!array_key_exists('fields', $index)) { |
||
| 137 | $index = ['fields' => $index]; |
||
| 138 | } |
||
| 139 | |||
| 140 | $index['if_not_exists'] = true; |
||
| 141 | $space->addIndex($index); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | foreach ($schema->getSpaces() as $space) { |
||
| 147 | if (!count($space->getIndexes())) { |
||
| 148 | if (!$space->hasProperty('id')) { |
||
| 149 | throw new Exception("No primary index on ". $space->getName()); |
||
| 150 | } |
||
| 151 | $space->addIndex(['id']); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | return $this; |
||
| 156 | } |
||
| 157 | |||
| 244 |