| Conditions | 70 |
| Paths | > 20000 |
| Total Lines | 225 |
| Code Lines | 167 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 80 | public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadata |
||
| 81 | { |
||
| 82 | $configured = false; |
||
| 83 | |||
| 84 | $classMetadata = new ClassMetadata($name = $class->name); |
||
| 85 | $fileResource = $class->getFilename(); |
||
| 86 | |||
| 87 | if (false !== $fileResource) { |
||
| 88 | $classMetadata->fileResources[] = $fileResource; |
||
| 89 | } |
||
| 90 | |||
| 91 | $propertiesMetadata = []; |
||
| 92 | $propertiesAnnotations = []; |
||
| 93 | |||
| 94 | $exclusionPolicy = ExclusionPolicy::NONE; |
||
| 95 | $excludeAll = false; |
||
| 96 | $classAccessType = PropertyMetadata::ACCESS_TYPE_PROPERTY; |
||
| 97 | $readOnlyClass = false; |
||
| 98 | |||
| 99 | foreach ($this->getClassAnnotations($class) as $annot) { |
||
| 100 | $configured = true; |
||
| 101 | |||
| 102 | if ($annot instanceof ExclusionPolicy) { |
||
| 103 | $exclusionPolicy = $annot->policy; |
||
| 104 | } elseif ($annot instanceof XmlRoot) { |
||
| 105 | $classMetadata->xmlRootName = $annot->name; |
||
| 106 | $classMetadata->xmlRootNamespace = $annot->namespace; |
||
| 107 | $classMetadata->xmlRootPrefix = $annot->prefix; |
||
| 108 | } elseif ($annot instanceof XmlNamespace) { |
||
| 109 | $classMetadata->registerNamespace($annot->uri, $annot->prefix); |
||
|
|
|||
| 110 | } elseif ($annot instanceof Exclude) { |
||
| 111 | if (null !== $annot->if) { |
||
| 112 | $classMetadata->excludeIf = $this->parseExpression($annot->if); |
||
| 113 | } else { |
||
| 114 | $excludeAll = true; |
||
| 115 | } |
||
| 116 | } elseif ($annot instanceof AccessType) { |
||
| 117 | $classAccessType = $annot->type; |
||
| 118 | } elseif ($annot instanceof ReadOnlyProperty) { |
||
| 119 | $readOnlyClass = true; |
||
| 120 | } elseif ($annot instanceof AccessorOrder) { |
||
| 121 | $classMetadata->setAccessorOrder($annot->order, $annot->custom); |
||
| 122 | } elseif ($annot instanceof Discriminator) { |
||
| 123 | if ($annot->disabled) { |
||
| 124 | $classMetadata->discriminatorDisabled = true; |
||
| 125 | } else { |
||
| 126 | $classMetadata->setDiscriminator($annot->field, $annot->map, $annot->groups); |
||
| 127 | } |
||
| 128 | } elseif ($annot instanceof XmlDiscriminator) { |
||
| 129 | $classMetadata->xmlDiscriminatorAttribute = (bool) $annot->attribute; |
||
| 130 | $classMetadata->xmlDiscriminatorCData = (bool) $annot->cdata; |
||
| 131 | $classMetadata->xmlDiscriminatorNamespace = $annot->namespace ? (string) $annot->namespace : null; |
||
| 132 | } elseif ($annot instanceof VirtualProperty) { |
||
| 133 | $virtualPropertyMetadata = new ExpressionPropertyMetadata( |
||
| 134 | $name, |
||
| 135 | $annot->name, |
||
| 136 | $this->parseExpression($annot->exp), |
||
| 137 | ); |
||
| 138 | $propertiesMetadata[] = $virtualPropertyMetadata; |
||
| 139 | $propertiesAnnotations[] = $annot->options; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | foreach ($class->getMethods() as $method) { |
||
| 144 | if ($method->class !== $name) { |
||
| 145 | continue; |
||
| 146 | } |
||
| 147 | |||
| 148 | $methodAnnotations = $this->getMethodAnnotations($method); |
||
| 149 | |||
| 150 | foreach ($methodAnnotations as $annot) { |
||
| 151 | $configured = true; |
||
| 152 | |||
| 153 | if ($annot instanceof PreSerialize) { |
||
| 154 | $classMetadata->addPreSerializeMethod(new MethodMetadata($name, $method->name)); |
||
| 155 | continue 2; |
||
| 156 | } elseif ($annot instanceof PostDeserialize) { |
||
| 157 | $classMetadata->addPostDeserializeMethod(new MethodMetadata($name, $method->name)); |
||
| 158 | continue 2; |
||
| 159 | } elseif ($annot instanceof PostSerialize) { |
||
| 160 | $classMetadata->addPostSerializeMethod(new MethodMetadata($name, $method->name)); |
||
| 161 | continue 2; |
||
| 162 | } elseif ($annot instanceof VirtualProperty) { |
||
| 163 | $virtualPropertyMetadata = new VirtualPropertyMetadata($name, $method->name); |
||
| 164 | $propertiesMetadata[] = $virtualPropertyMetadata; |
||
| 165 | $propertiesAnnotations[] = $methodAnnotations; |
||
| 166 | continue 2; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | if (!$excludeAll) { |
||
| 172 | foreach ($class->getProperties() as $property) { |
||
| 173 | if ($property->class !== $name || (isset($property->info) && $property->info['class'] !== $name)) { |
||
| 174 | continue; |
||
| 175 | } |
||
| 176 | |||
| 177 | $propertiesMetadata[] = new PropertyMetadata($name, $property->getName()); |
||
| 178 | $propertiesAnnotations[] = $this->getPropertyAnnotations($property); |
||
| 179 | } |
||
| 180 | |||
| 181 | foreach ($propertiesMetadata as $propertyKey => $propertyMetadata) { |
||
| 182 | $isExclude = false; |
||
| 183 | $isExpose = $propertyMetadata instanceof VirtualPropertyMetadata |
||
| 184 | || $propertyMetadata instanceof ExpressionPropertyMetadata; |
||
| 185 | $propertyMetadata->readOnly = $propertyMetadata->readOnly || $readOnlyClass; |
||
| 186 | $accessType = $classAccessType; |
||
| 187 | $accessor = [null, null]; |
||
| 188 | |||
| 189 | $propertyAnnotations = $propertiesAnnotations[$propertyKey]; |
||
| 190 | |||
| 191 | foreach ($propertyAnnotations as $annot) { |
||
| 192 | $configured = true; |
||
| 193 | |||
| 194 | if ($annot instanceof Since) { |
||
| 195 | $propertyMetadata->sinceVersion = $annot->version; |
||
| 196 | } elseif ($annot instanceof Until) { |
||
| 197 | $propertyMetadata->untilVersion = $annot->version; |
||
| 198 | } elseif ($annot instanceof SerializedName) { |
||
| 199 | $propertyMetadata->serializedName = $annot->name; |
||
| 200 | } elseif ($annot instanceof SkipWhenEmpty) { |
||
| 201 | $propertyMetadata->skipWhenEmpty = true; |
||
| 202 | } elseif ($annot instanceof Expose) { |
||
| 203 | $isExpose = true; |
||
| 204 | if (null !== $annot->if) { |
||
| 205 | $propertyMetadata->excludeIf = $this->parseExpression('!(' . $annot->if . ')'); |
||
| 206 | } |
||
| 207 | } elseif ($annot instanceof Exclude) { |
||
| 208 | if (null !== $annot->if) { |
||
| 209 | $propertyMetadata->excludeIf = $this->parseExpression($annot->if); |
||
| 210 | } else { |
||
| 211 | $isExclude = true; |
||
| 212 | } |
||
| 213 | } elseif ($annot instanceof Type) { |
||
| 214 | $propertyMetadata->setType($this->typeParser->parse($annot->name)); |
||
| 215 | } elseif ($annot instanceof XmlElement) { |
||
| 216 | $propertyMetadata->xmlAttribute = false; |
||
| 217 | $propertyMetadata->xmlElementCData = $annot->cdata; |
||
| 218 | $propertyMetadata->xmlNamespace = $annot->namespace; |
||
| 219 | } elseif ($annot instanceof XmlList) { |
||
| 220 | $propertyMetadata->xmlCollection = true; |
||
| 221 | $propertyMetadata->xmlCollectionInline = $annot->inline; |
||
| 222 | $propertyMetadata->xmlEntryName = $annot->entry; |
||
| 223 | $propertyMetadata->xmlEntryNamespace = $annot->namespace; |
||
| 224 | $propertyMetadata->xmlCollectionSkipWhenEmpty = $annot->skipWhenEmpty; |
||
| 225 | } elseif ($annot instanceof XmlMap) { |
||
| 226 | $propertyMetadata->xmlCollection = true; |
||
| 227 | $propertyMetadata->xmlCollectionInline = $annot->inline; |
||
| 228 | $propertyMetadata->xmlEntryName = $annot->entry; |
||
| 229 | $propertyMetadata->xmlEntryNamespace = $annot->namespace; |
||
| 230 | $propertyMetadata->xmlKeyAttribute = $annot->keyAttribute; |
||
| 231 | } elseif ($annot instanceof XmlKeyValuePairs) { |
||
| 232 | $propertyMetadata->xmlKeyValuePairs = true; |
||
| 233 | } elseif ($annot instanceof XmlAttribute) { |
||
| 234 | $propertyMetadata->xmlAttribute = true; |
||
| 235 | $propertyMetadata->xmlNamespace = $annot->namespace; |
||
| 236 | } elseif ($annot instanceof XmlValue) { |
||
| 237 | $propertyMetadata->xmlValue = true; |
||
| 238 | $propertyMetadata->xmlElementCData = $annot->cdata; |
||
| 239 | } elseif ($annot instanceof AccessType) { |
||
| 240 | $accessType = $annot->type; |
||
| 241 | } elseif ($annot instanceof ReadOnlyProperty) { |
||
| 242 | $propertyMetadata->readOnly = $annot->readOnly; |
||
| 243 | } elseif ($annot instanceof Accessor) { |
||
| 244 | $accessor = [$annot->getter, $annot->setter]; |
||
| 245 | } elseif ($annot instanceof Groups) { |
||
| 246 | $propertyMetadata->groups = $annot->groups; |
||
| 247 | foreach ((array) $propertyMetadata->groups as $groupName) { |
||
| 248 | if (false !== strpos($groupName, ',')) { |
||
| 249 | throw new InvalidMetadataException(sprintf( |
||
| 250 | 'Invalid group name "%s" on "%s", did you mean to create multiple groups?', |
||
| 251 | implode(', ', $propertyMetadata->groups), |
||
| 252 | $propertyMetadata->class . '->' . $propertyMetadata->name, |
||
| 253 | )); |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } elseif ($annot instanceof Inline) { |
||
| 257 | $propertyMetadata->inline = true; |
||
| 258 | } elseif ($annot instanceof XmlAttributeMap) { |
||
| 259 | $propertyMetadata->xmlAttributeMap = true; |
||
| 260 | } elseif ($annot instanceof MaxDepth) { |
||
| 261 | $propertyMetadata->maxDepth = $annot->depth; |
||
| 262 | } elseif ($annot instanceof UnionDiscriminator) { |
||
| 263 | $propertyMetadata->setType([ |
||
| 264 | 'name' => 'union', |
||
| 265 | 'params' => [null, $annot->field, $annot->map], |
||
| 266 | ]); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | if ($propertyMetadata->inline) { |
||
| 271 | $classMetadata->isList = $classMetadata->isList || PropertyMetadata::isCollectionList($propertyMetadata->type); |
||
| 272 | $classMetadata->isMap = $classMetadata->isMap || PropertyMetadata::isCollectionMap($propertyMetadata->type); |
||
| 273 | |||
| 274 | if ($classMetadata->isMap && $classMetadata->isList) { |
||
| 275 | throw new InvalidMetadataException('Cannot have an inline map and inline map on the same class'); |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | if (!$propertyMetadata->serializedName) { |
||
| 280 | $propertyMetadata->serializedName = $this->namingStrategy->translateName($propertyMetadata); |
||
| 281 | } |
||
| 282 | |||
| 283 | foreach ($propertyAnnotations as $annot) { |
||
| 284 | if ($annot instanceof VirtualProperty && null !== $annot->name) { |
||
| 285 | $propertyMetadata->name = $annot->name; |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | if ( |
||
| 290 | (ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude) |
||
| 291 | || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose) |
||
| 292 | ) { |
||
| 293 | $propertyMetadata->setAccessor($accessType, $accessor[0], $accessor[1]); |
||
| 294 | $classMetadata->addPropertyMetadata($propertyMetadata); |
||
| 295 | } |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | // if (!$configured) { |
||
| 300 | // return null; |
||
| 301 | // uncomment the above line after a couple of months |
||
| 302 | // } |
||
| 303 | |||
| 304 | return $classMetadata; |
||
| 305 | } |
||
| 370 |