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 |
||
71 | public function migrate() |
||
72 | { |
||
73 | $factory = DocBlockFactory::createInstance(); |
||
74 | $contextFactory = new ContextFactory(); |
||
75 | |||
76 | $schema = $this->mapper->getSchema(); |
||
77 | |||
78 | foreach ($this->entityClasses as $entity) { |
||
79 | $spaceName = $this->getSpaceName($entity); |
||
80 | $space = $schema->hasSpace($spaceName) ? $schema->getSpace($spaceName) : $schema->createSpace($spaceName); |
||
81 | |||
82 | $this->mapEntity($spaceName, $entity); |
||
83 | |||
84 | $class = new ReflectionClass($entity); |
||
85 | |||
86 | foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { |
||
87 | $context = $contextFactory->createFromReflector($property); |
||
88 | $description = $factory->create($property->getDocComment(), $context); |
||
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 | $propertyName = $this->mapper->getSchema()->toUnderscore($property->getName()); |
||
100 | $phpType = $tags[0]->getType(); |
||
101 | $type = $this->getTarantoolType($phpType); |
||
102 | |||
103 | if (!$space->hasProperty($propertyName)) { |
||
104 | if ($this->isReference($phpType)) { |
||
105 | $space->addProperty($propertyName, $type, true, $this->getSpaceName((string) $phpType)); |
||
106 | } else { |
||
107 | $space->addProperty($propertyName, $type); |
||
108 | } |
||
109 | } |
||
110 | } |
||
111 | if ($this->mapper->hasPlugin(NestedSet::class)) { |
||
112 | $nested = $this->mapper->getPlugin(NestedSet::class); |
||
113 | if ($nested->isNested($space)) { |
||
114 | $nested->addIndexes($space); |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | |||
119 | foreach ($this->repositoryClasses as $repository) { |
||
120 | $spaceName = $this->getSpaceName($repository); |
||
121 | |||
122 | if (!$schema->hasSpace($spaceName)) { |
||
123 | throw new Exception("Repository with no entity definition"); |
||
124 | } |
||
125 | |||
126 | $this->mapRepository($spaceName, $repository); |
||
127 | |||
128 | $space = $schema->getSpace($spaceName); |
||
129 | |||
130 | $class = new ReflectionClass($repository); |
||
131 | $properties = $class->getDefaultProperties(); |
||
132 | |||
133 | if (array_key_exists('indexes', $properties)) { |
||
134 | foreach ($properties['indexes'] as $i => $index) { |
||
135 | if (!is_array($index)) { |
||
136 | $index = (array) $index; |
||
137 | } |
||
138 | if (!array_key_exists('fields', $index)) { |
||
139 | $index = ['fields' => $index]; |
||
140 | } |
||
141 | |||
142 | $index['if_not_exists'] = true; |
||
143 | try { |
||
144 | $space->addIndex($index); |
||
145 | } catch (Exception $e) { |
||
146 | $presentation = json_encode($properties['indexes'][$i]); |
||
147 | throw new Exception("Failed to add index $presentation. ". $e->getMessage(), 0, $e); |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | |||
153 | foreach ($schema->getSpaces() as $space) { |
||
154 | if (!count($space->getIndexes())) { |
||
155 | if (!$space->hasProperty('id')) { |
||
156 | throw new Exception("No primary index on ". $space->getName()); |
||
157 | } |
||
158 | $space->addIndex(['id']); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | return $this; |
||
163 | } |
||
164 | |||
241 |