Conditions | 24 |
Paths | 4032 |
Total Lines | 178 |
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 |
||
119 | public function getMetadata(Metadata $documentMetadata, StructureMetadata $structure) |
||
120 | { |
||
121 | $classMetadata = $this->factory->createClassMetadata($documentMetadata->getClass()); |
||
122 | $class = $documentMetadata->getReflectionClass(); |
||
123 | |||
124 | $indexMeta = $this->factory->createIndexMetadata(); |
||
125 | $indexMeta->setIdField($this->factory->createMetadataField('uuid')); |
||
126 | $indexMeta->setLocaleField($this->factory->createMetadataField('originalLocale')); |
||
127 | |||
128 | $indexName = 'page'; |
||
129 | $decorate = false; |
||
130 | |||
131 | // See if the mapping overrides the default index and category name |
||
132 | foreach ($this->mapping as $className => $mapping) { |
||
133 | if ($documentMetadata->getAlias() !== $className && |
||
134 | $class->name !== $className && |
||
135 | false === $class->isSubclassOf($className) |
||
136 | ) { |
||
137 | continue; |
||
138 | } |
||
139 | |||
140 | $indexName = $mapping['index']; |
||
141 | if ($mapping['decorate_index']) { |
||
142 | $decorate = true; |
||
143 | } |
||
144 | } |
||
145 | |||
146 | $indexMeta->setIndexName($this->createIndexNameField($documentMetadata, $indexName, $decorate)); |
||
147 | |||
148 | foreach ($structure->getProperties() as $property) { |
||
149 | if ($property instanceof BlockMetadata) { |
||
150 | $propertyMapping = new ComplexMetadata(); |
||
151 | foreach ($property->getComponents() as $component) { |
||
152 | foreach ($component->getChildren() as $componentProperty) { |
||
153 | if (false === $componentProperty->hasTag('sulu.search.field')) { |
||
154 | continue; |
||
155 | } |
||
156 | |||
157 | $tag = $componentProperty->getTag('sulu.search.field'); |
||
158 | $tagAttributes = $tag['attributes']; |
||
159 | |||
160 | if (!isset($tagAttributes['index']) || 'false' !== $tagAttributes['index']) { |
||
161 | $propertyMapping->addFieldMapping( |
||
162 | $property->getName() . '.' . $componentProperty->getName(), |
||
163 | [ |
||
164 | 'type' => isset($tagAttributes['type']) ? $tagAttributes['type'] : 'string', |
||
165 | 'field' => $this->factory->createMetadataProperty( |
||
166 | '[' . $componentProperty->getName() . ']' |
||
167 | ), |
||
168 | 'aggregate' => true, |
||
169 | 'indexed' => false, |
||
170 | ] |
||
171 | ); |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | |||
176 | $indexMeta->addFieldMapping( |
||
177 | $property->getName(), |
||
178 | [ |
||
179 | 'type' => 'complex', |
||
180 | 'mapping' => $propertyMapping, |
||
181 | 'field' => $this->getContentField($property), |
||
182 | ] |
||
183 | ); |
||
184 | } else { |
||
185 | $this->mapProperty($property, $indexMeta); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | if ($class->isSubclassOf(ExtensionBehavior::class)) { |
||
190 | $extensions = $this->extensionManager->getExtensions($structure->getName()); |
||
191 | foreach ($extensions as $extension) { |
||
192 | foreach ($extension->getFieldMapping() as $name => $mapping) { |
||
193 | $indexMeta->addFieldMapping($name, $mapping); |
||
194 | } |
||
195 | } |
||
196 | } |
||
197 | |||
198 | if ($class->isSubclassOf(ResourceSegmentBehavior::class)) { |
||
199 | $field = $this->factory->createMetadataField('resourceSegment'); |
||
200 | if ($class->isSubclassOf(RedirectTypeBehavior::class)) { |
||
201 | $expression = <<<'EOT' |
||
202 | (object.getRedirectType() === %s |
||
203 | ? (object.getRedirectTarget() ? object.getRedirectTarget().getResourceSegment()) |
||
204 | : (object.getRedirectType() === %s |
||
205 | ? object.getRedirectExternal() |
||
206 | : object.getResourceSegment() |
||
207 | ) |
||
208 | ) |
||
209 | EOT; |
||
210 | |||
211 | $field = new Expression( |
||
212 | sprintf( |
||
213 | $expression, |
||
214 | RedirectType::INTERNAL, |
||
215 | RedirectType::EXTERNAL |
||
216 | ) |
||
217 | ); |
||
218 | } |
||
219 | |||
220 | $indexMeta->setUrlField($field); |
||
221 | } |
||
222 | |||
223 | if (!$indexMeta->getTitleField()) { |
||
224 | if ($class->isSubclassOf(TitleBehavior::class)) { |
||
225 | $indexMeta->setTitleField($this->factory->createMetadataProperty('title')); |
||
226 | |||
227 | $indexMeta->addFieldMapping( |
||
228 | 'title', |
||
229 | [ |
||
230 | 'type' => 'string', |
||
231 | 'field' => $this->factory->createMetadataField('title'), |
||
232 | 'aggregate' => true, |
||
233 | 'indexed' => false, |
||
234 | ] |
||
235 | ); |
||
236 | } |
||
237 | } |
||
238 | |||
239 | if ($class->isSubclassOf(WebspaceBehavior::class)) { |
||
240 | // index the webspace |
||
241 | $indexMeta->addFieldMapping( |
||
242 | 'webspace_key', |
||
243 | [ |
||
244 | 'type' => 'string', |
||
245 | 'field' => $this->factory->createMetadataProperty('webspaceName'), |
||
246 | ] |
||
247 | ); |
||
248 | } |
||
249 | |||
250 | if ($class->isSubclassOf(WorkflowStageBehavior::class)) { |
||
251 | $indexMeta->addFieldMapping( |
||
252 | 'state', |
||
253 | [ |
||
254 | 'type' => 'string', |
||
255 | 'field' => $this->factory->createMetadataExpression( |
||
256 | 'object.getWorkflowStage() == 1 ? "test" : "published"' |
||
257 | ), |
||
258 | ] |
||
259 | ); |
||
260 | $indexMeta->addFieldMapping( |
||
261 | 'published', |
||
262 | [ |
||
263 | 'type' => 'date', |
||
264 | 'field' => $this->factory->createMetadataExpression( |
||
265 | 'object.getPublished()' |
||
266 | ), |
||
267 | ] |
||
268 | ); |
||
269 | } |
||
270 | |||
271 | if ($class->isSubclassOf(LocalizedAuthorBehavior::class)) { |
||
272 | $indexMeta->addFieldMapping( |
||
273 | 'authored', |
||
274 | [ |
||
275 | 'type' => 'date', |
||
276 | 'field' => $this->factory->createMetadataExpression( |
||
277 | 'object.getAuthored()' |
||
278 | ), |
||
279 | ] |
||
280 | ); |
||
281 | } |
||
282 | |||
283 | $indexMeta->addFieldMapping( |
||
284 | self::FIELD_STRUCTURE_TYPE, |
||
285 | [ |
||
286 | 'type' => 'string', |
||
287 | 'stored' => true, |
||
288 | 'indexed' => true, |
||
289 | 'field' => $this->factory->createMetadataProperty('structureType'), |
||
290 | ] |
||
291 | ); |
||
292 | |||
293 | $classMetadata->addIndexMetadata('_default', $indexMeta); |
||
294 | |||
295 | return $classMetadata; |
||
296 | } |
||
297 | |||
463 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..